backend/service/
drawings.rs

1//! Service layer for user drawings.
2
3use uuid::Uuid;
4
5use crate::config::data::SharedPool;
6use crate::error::ServiceError;
7use crate::model::dto::drawings::{DrawingDto, UpdateDrawingsDto};
8use crate::model::entity::drawings::Drawing;
9
10/// Get all drawings from one map.
11///
12/// # Errors
13/// If the connection to the database could not be established.
14pub async fn find(map_id: i32, pool: &SharedPool) -> Result<Vec<DrawingDto>, ServiceError> {
15    let mut conn = pool.get().await?;
16    let results = Drawing::find(map_id, &mut conn).await?;
17    results.into_iter().map(TryFrom::try_from).collect()
18}
19
20/// Save new drawing.
21///
22/// # Errors
23/// If the connection to the database could not be established.
24pub async fn create(
25    dtos: Vec<DrawingDto>,
26    pool: &SharedPool,
27) -> Result<Vec<DrawingDto>, ServiceError> {
28    let mut conn = pool.get().await?;
29    let drawing_updates = dtos
30        .into_iter()
31        .map(Drawing::try_from)
32        .collect::<Result<Vec<Drawing>, ServiceError>>()?;
33    let result = Drawing::create(drawing_updates, &mut conn).await?;
34    result.into_iter().map(TryFrom::try_from).collect()
35}
36
37/// Update the drawing in the database.
38///
39/// # Errors
40/// If the connection to the database could not be established.
41pub async fn update(
42    dto: UpdateDrawingsDto,
43    pool: &SharedPool,
44) -> Result<Vec<DrawingDto>, ServiceError> {
45    let mut conn = pool.get().await?;
46    let result = Drawing::update(dto.try_into()?, &mut conn).await?;
47    result.into_iter().map(TryFrom::try_from).collect()
48}
49
50/// Delete drawings from the databse.
51///
52/// # Errors
53/// If the connection to the database could not be established.
54pub async fn delete_by_ids(ids: Vec<Uuid>, pool: &SharedPool) -> Result<(), ServiceError> {
55    let mut conn = pool.get().await?;
56    let _ = Drawing::delete_by_ids(ids, &mut conn).await?;
57    Ok(())
58}