create_rust_app/storage/
attachment_blob.rs1use 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 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 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 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 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 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}