BlueGit

ict rusty-teams

Files

rusty-teams / crates / store / src / attachment_images.rs

attachment_images.rs

attachment_images.rs 2.0 KiB Download Raw
1use rusqlite::{Connection, params};
2
3use crate::error::Result;
4use crate::models::AttachmentImage;
5
6pub(crate) const OUTBOX_OWNER: &str = "outbox";
7pub(crate) const DRAFT_OWNER: &str = "draft";
8pub(crate) const PROPOSAL_OWNER: &str = "proposal";
9
10pub(crate) fn replace_images(
11 connection: &Connection,
12 owner_kind: &str,
13 owner_id: &str,
14 images: &[AttachmentImage],
15) -> Result<()> {
16 delete_images(connection, owner_kind, owner_id)?;
17 let mut insert = connection.prepare_cached(
18 "INSERT INTO attachment_images (owner_kind, owner_id, position, name, format, bytes, width, height)
19 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
20 )?;
21 for (position, image) in images.iter().enumerate() {
22 insert.execute(params![
23 owner_kind,
24 owner_id,
25 position as i64,
26 image.name,
27 image.format,
28 image.bytes,
29 image.width,
30 image.height,
31 ])?;
32 }
33 Ok(())
34}
35
36pub(crate) fn load_images(
37 connection: &Connection,
38 owner_kind: &str,
39 owner_id: &str,
40) -> Result<Vec<AttachmentImage>> {
41 let mut statement = connection.prepare_cached(
42 "SELECT name, format, bytes, width, height FROM attachment_images
43 WHERE owner_kind = ?1 AND owner_id = ?2 ORDER BY position",
44 )?;
45 let images = statement
46 .query_map(params![owner_kind, owner_id], |row| {
47 Ok(AttachmentImage {
48 name: row.get(0)?,
49 format: row.get(1)?,
50 bytes: row.get(2)?,
51 width: row.get(3)?,
52 height: row.get(4)?,
53 })
54 })?
55 .collect::<rusqlite::Result<Vec<_>>>()?;
56 Ok(images)
57}
58
59pub(crate) fn delete_images(
60 connection: &Connection,
61 owner_kind: &str,
62 owner_id: &str,
63) -> Result<()> {
64 connection.execute(
65 "DELETE FROM attachment_images WHERE owner_kind = ?1 AND owner_id = ?2",
66 params![owner_kind, owner_id],
67 )?;
68 Ok(())
69}