backend/model/entity/
map_collaborator_impl.rs

1use diesel::{debug_query, pg::Pg, ExpressionMethods, QueryDsl, QueryResult};
2use diesel_async::{AsyncPgConnection, RunQueryDsl};
3use log::debug;
4
5use crate::{
6    model::dto::{DeleteMapCollaboratorDto, NewMapCollaboratorDto},
7    schema::map_collaborators,
8};
9
10use super::MapCollaborator;
11
12impl MapCollaborator {
13    /// Create a new map collaborator.
14    ///
15    /// # Errors
16    /// * Unknown, diesel doesn't say why it might error.
17    pub async fn create(
18        map_and_collaborator: (i32, NewMapCollaboratorDto),
19        conn: &mut AsyncPgConnection,
20    ) -> QueryResult<Self> {
21        let new_map_collaborator = Self::from(map_and_collaborator);
22
23        let query = diesel::insert_into(map_collaborators::table).values(&new_map_collaborator);
24        debug!("{}", debug_query::<Pg, _>(&query));
25        query.get_result::<Self>(conn).await
26    }
27
28    /// Find all map collaborators of a map.
29    ///
30    /// # Errors
31    /// * Unknown, diesel doesn't say why it might error.
32    pub async fn find_by_map_id(
33        map_id: i32,
34        conn: &mut AsyncPgConnection,
35    ) -> QueryResult<Vec<Self>> {
36        let query = map_collaborators::table.filter(map_collaborators::map_id.eq(map_id));
37        debug!("{}", debug_query::<Pg, _>(&query));
38        query.get_results::<Self>(conn).await
39    }
40
41    /// Delete a collaborator of a map.
42    ///
43    /// # Errors
44    /// * Unknown, diesel doesn't say why it might error.
45    pub async fn delete(
46        map_and_dto: (i32, DeleteMapCollaboratorDto),
47        conn: &mut AsyncPgConnection,
48    ) -> QueryResult<()> {
49        let (map_id, dto) = map_and_dto;
50
51        let query = diesel::delete(map_collaborators::table.find((map_id, dto.user_id)));
52        debug!("{}", debug_query::<Pg, _>(&query));
53        query.execute(conn).await?;
54
55        Ok(())
56    }
57}