create_rust_app/storage/
attachment_blob.rs

1use serde::{Deserialize, Serialize};
2
3use crate::diesel::{
4    insert_into, AsChangeset, ExpressionMethods, Identifiable, Insertable, QueryDsl, QueryResult,
5    Queryable, RunQueryDsl,
6};
7use crate::storage::{schema, schema::attachment_blobs, Utc, ID};
8use crate::Connection;
9
10#[allow(clippy::module_name_repetitions)]
11#[derive(
12    Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Identifiable, AsChangeset,
13)]
14#[diesel(table_name = attachment_blobs)]
15pub struct AttachmentBlob {
16    pub id: ID,
17
18    pub key: String,
19    pub file_name: String,
20    pub content_type: Option<String>,
21    pub byte_size: i64,
22    pub checksum: String,
23    pub service_name: String,
24
25    pub created_at: Utc,
26}
27
28#[allow(clippy::module_name_repetitions)]
29#[derive(Debug, Serialize, Deserialize, Clone, Insertable, AsChangeset)]
30#[diesel(table_name = attachment_blobs)]
31pub struct AttachmentBlobChangeset {
32    pub key: String,
33    pub file_name: String,
34    pub content_type: Option<String>,
35    pub byte_size: i64,
36    pub checksum: String,
37    pub service_name: String,
38}
39
40impl AttachmentBlob {
41    /// Create an entry in [`db`](`Connection`)'s `attachment_blobs` table using the data in [`item`](`AttachmentBlobChangeset`)
42    ///
43    /// # Errors
44    /// * Diesel error
45    pub fn create(db: &mut Connection, item: &AttachmentBlobChangeset) -> QueryResult<Self> {
46        use super::schema::attachment_blobs::dsl::attachment_blobs;
47
48        insert_into(attachment_blobs)
49            .values(item)
50            .get_result::<Self>(db)
51    }
52
53    /// Read from [`db`](`Connection`), querying for an entry in the `attachment_blobs` table who's primary key matches [`item_id`](`ID`)
54    ///
55    /// # Errors
56    /// * Diesel error
57    pub fn find_by_id(db: &mut Connection, item_id: ID) -> QueryResult<Self> {
58        use super::schema::attachment_blobs::dsl::attachment_blobs;
59
60        attachment_blobs
61            .filter(schema::attachment_blobs::id.eq(item_id))
62            .first::<Self>(db)
63    }
64
65    /// Read from [`db`](`Connection`), querying for an entry in the `attachment_blobs` table for each [`item_id`](`ID`) in `item_ids`
66    ///
67    /// # Errors
68    /// * Diesel error
69    pub fn find_all_by_id(db: &mut Connection, item_ids: Vec<ID>) -> QueryResult<Vec<Self>> {
70        use super::schema::attachment_blobs::dsl::attachment_blobs;
71
72        attachment_blobs
73            .filter(schema::attachment_blobs::id.eq_any(item_ids))
74            .load::<Self>(db)
75    }
76
77    // fn read_all(db: &mut Connection, pagination: &PaginationParams) -> QueryResult<Vec<Self>> {
78    //     use super::schema::attachment_blobs::dsl::*;
79    //
80    //     attachment_blobs
81    //         .order(created_at)
82    //         .limit(pagination.page_size)
83    //         .offset(pagination.page * std::cmp::max(pagination.page_size, 100))
84    //         .load::<AttachmentBlob>(db)
85    // }
86
87    // fn update(db: &mut Connection, item_id: ID, item: &AttachmentBlobChangeset) -> QueryResult<Self> {
88    //     use super::schema::attachment_blobs::dsl::*;
89    //
90    //     diesel::update(attachment_blobs.filter(id.eq(item_id)))
91    //         .set(item)
92    //         .get_result(db)
93    // }
94
95    /// Delete the entry in [`db`](`Connection`)'s `attachment_blobs` table who's primary key matches [`item_id`](`ID`)
96    ///
97    /// # Errors
98    /// * Diesel error
99    pub fn delete(db: &mut Connection, item_id: ID) -> QueryResult<usize> {
100        let query =
101            schema::attachment_blobs::table.filter(schema::attachment_blobs::id.eq(item_id));
102
103        diesel::delete(query).execute(db)
104    }
105
106    /// Delete every entry in [`db`](`Connection`)'s `attachment_blobs` table who's primary key matches [`item_id`](`ID`)
107    ///
108    /// # Errors
109    /// * Diesel error
110    pub fn delete_all(db: &mut Connection, item_ids: Vec<ID>) -> QueryResult<usize> {
111        let query =
112            schema::attachment_blobs::table.filter(schema::attachment_blobs::id.eq_any(item_ids));
113
114        diesel::delete(query).execute(db)
115    }
116}