1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use serde::{Deserialize, Serialize};
use crate::Connection;
use crate::diesel::*;
use crate::storage::{ID, schema, schema::*, UTC};
#[derive(
Debug, Serialize, Deserialize, Clone, Queryable, Insertable, Identifiable, AsChangeset,
)]
#[diesel(table_name = attachment_blobs)]
pub struct AttachmentBlob {
pub id: ID,
pub key: String,
pub file_name: String,
pub content_type: Option<String>,
pub byte_size: i64,
pub checksum: String,
pub service_name: String,
pub created_at: UTC,
}
#[derive(Debug, Serialize, Deserialize, Clone, Insertable, AsChangeset)]
#[diesel(table_name = attachment_blobs)]
pub struct AttachmentBlobChangeset {
pub key: String,
pub file_name: String,
pub content_type: Option<String>,
pub byte_size: i64,
pub checksum: String,
pub service_name: String,
}
impl AttachmentBlob {
pub fn create(db: &mut Connection, item: &AttachmentBlobChangeset) -> QueryResult<Self> {
use super::schema::attachment_blobs::dsl::*;
insert_into(attachment_blobs)
.values(item)
.get_result::<AttachmentBlob>(db)
}
pub fn find_by_id(db: &mut Connection, item_id: ID) -> QueryResult<Self> {
use super::schema::attachment_blobs::dsl::*;
attachment_blobs
.filter(schema::attachment_blobs::id.eq(item_id))
.first::<AttachmentBlob>(db)
}
pub fn find_all_by_id(db: &mut Connection, item_ids: Vec<ID>) -> QueryResult<Vec<Self>> {
use super::schema::attachment_blobs::dsl::*;
attachment_blobs
.filter(schema::attachment_blobs::id.eq_any(item_ids))
.load::<AttachmentBlob>(db)
}
pub fn delete(db: &mut Connection, item_id: ID) -> QueryResult<usize> {
let query =
schema::attachment_blobs::table.filter(schema::attachment_blobs::id.eq(item_id));
diesel::delete(query).execute(db)
}
pub fn delete_all(db: &mut Connection, item_ids: Vec<ID>) -> QueryResult<usize> {
let query =
schema::attachment_blobs::table.filter(schema::attachment_blobs::id.eq_any(item_ids));
diesel::delete(query).execute(db)
}
}