backend/model/entity/
map_collaborator_impl.rs

1use crate::{
2    model::dto::{DeleteMapCollaboratorDto, NewMapCollaboratorDto},
3    schema::map_collaborators,
4};
5use diesel::dsl::{exists, select};
6use diesel::{debug_query, pg::Pg, ExpressionMethods, QueryDsl, QueryResult};
7use diesel_async::{AsyncPgConnection, RunQueryDsl};
8use log::debug;
9use uuid::Uuid;
10
11use super::MapCollaborator;
12
13impl MapCollaborator {
14    /// Create a new map collaborator.
15    ///
16    /// # Errors
17    /// * Unknown, diesel doesn't say why it might error.
18    pub async fn create(
19        map_and_collaborator: (i32, NewMapCollaboratorDto),
20        conn: &mut AsyncPgConnection,
21    ) -> QueryResult<Self> {
22        let new_map_collaborator = Self::from(map_and_collaborator);
23
24        let query = diesel::insert_into(map_collaborators::table).values(&new_map_collaborator);
25        debug!("{}", debug_query::<Pg, _>(&query));
26        query.get_result::<Self>(conn).await
27    }
28
29    /// Find all map collaborators of a map.
30    ///
31    /// # Errors
32    /// * Unknown, diesel doesn't say why it might error.
33    pub async fn find_by_map_id(
34        map_id: i32,
35        conn: &mut AsyncPgConnection,
36    ) -> QueryResult<Vec<Self>> {
37        let query = map_collaborators::table.filter(map_collaborators::map_id.eq(map_id));
38        debug!("{}", debug_query::<Pg, _>(&query));
39        query.get_results::<Self>(conn).await
40    }
41
42    /// Find all maps of a collaborators.
43    ///
44    /// # Errors
45    /// * Unknown, diesel doesn't say why it might error.
46    pub async fn find_by_user_id(
47        user_id: Uuid,
48        conn: &mut AsyncPgConnection,
49    ) -> QueryResult<Vec<i32>> {
50        let query = map_collaborators::table
51            .filter(map_collaborators::user_id.eq(user_id))
52            .select(map_collaborators::map_id);
53        debug!("{}", debug_query::<Pg, _>(&query));
54        query.get_results::<i32>(conn).await
55    }
56
57    /// Delete a collaborator of a map.
58    ///
59    /// # Errors
60    /// * Unknown, diesel doesn't say why it might error.
61    pub async fn delete(
62        map_and_dto: (i32, DeleteMapCollaboratorDto),
63        conn: &mut AsyncPgConnection,
64    ) -> QueryResult<()> {
65        let (map_id, dto) = map_and_dto;
66
67        let query = diesel::delete(map_collaborators::table.find((map_id, dto.user_id)));
68        debug!("{}", debug_query::<Pg, _>(&query));
69        query.execute(conn).await?;
70
71        Ok(())
72    }
73
74    /// Check if a user is a collaborator of a map.
75    ///
76    /// # Errors
77    /// * Unknown, diesel doesn't say why it might error.
78    pub async fn contains_user_as_collaborator(
79        user_id_val: Uuid,
80        map_id_val: i32,
81        conn: &mut AsyncPgConnection,
82    ) -> QueryResult<bool> {
83        let query = select(exists(
84            map_collaborators::table.find((map_id_val, user_id_val)),
85        ));
86        debug!("{}", debug_query::<Pg, _>(&query));
87        query.get_result(conn).await
88    }
89}