Skip to main content

revolt_database/models/emojis/ops/
mongodb.rs

1use bson::Document;
2use revolt_result::Result;
3
4use crate::{Emoji, PartialEmoji};
5use crate::MongoDb;
6
7use super::AbstractEmojis;
8
9static COL: &str = "emojis";
10
11#[async_trait]
12impl AbstractEmojis for MongoDb {
13    /// Insert emoji into database.
14    async fn insert_emoji(&self, emoji: &Emoji) -> Result<()> {
15        query!(self, insert_one, COL, &emoji).map(|_| ())
16    }
17
18    /// Fetch an emoji by its id
19    async fn fetch_emoji(&self, id: &str) -> Result<Emoji> {
20        query!(self, find_one_by_id, COL, id)?.ok_or_else(|| create_error!(NotFound))
21    }
22
23    /// Fetch emoji by their parent id
24    async fn fetch_emoji_by_parent_id(&self, parent_id: &str) -> Result<Vec<Emoji>> {
25        query!(
26            self,
27            find,
28            COL,
29            doc! {
30                "parent.id": parent_id
31            }
32        )
33    }
34
35    /// Fetch emoji by their parent ids
36    async fn fetch_emoji_by_parent_ids(&self, parent_ids: &[String]) -> Result<Vec<Emoji>> {
37        query!(
38            self,
39            find,
40            COL,
41            doc! {
42                "parent.id": {
43                    "$in": parent_ids
44                }
45            }
46        )
47    }
48
49    /// Update emoji with new information
50    async fn update_emoji(&self, emoji_id: &str, partial: &PartialEmoji) -> Result<()> {
51        query!(self, update_one_by_id, COL, emoji_id, partial, vec![], None).map(|_| ())
52    }
53
54    /// Detach an emoji by its id
55    async fn detach_emoji(&self, emoji: &Emoji) -> Result<()> {
56        self.col::<Document>(COL)
57            .update_one(
58                doc! {
59                    "_id": &emoji.id
60                },
61                doc! {
62                    "$set": {
63                        "parent": {
64                            "type": "Detached"
65                        }
66                    }
67                },
68            )
69            .await
70            .map(|_| ())
71            .map_err(|_| create_database_error!("update_one", COL))
72    }
73}