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