From 56e50564e87cc764fc5b815f19aa56560f78e4d3 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 12 Sep 2022 14:03:12 +0200 Subject: Keep saved filepath in the database --- src/models/model.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/models/model.rs') diff --git a/src/models/model.rs b/src/models/model.rs index f31c057..0336c20 100644 --- a/src/models/model.rs +++ b/src/models/model.rs @@ -54,6 +54,14 @@ pub struct ModelUser { author: Option, } +#[derive(Deserialize, Serialize)] +pub struct ModelUpload { + id: i32, + model_id: i32, + filepath: String, + created: NaiveDateTime, +} + impl Model { pub fn new( name: String, @@ -173,3 +181,36 @@ impl ModelUser { } } } + +impl ModelUpload { + pub fn new(filepath: String, model_id: i32) -> Self { + let now = Local::now().naive_utc(); + Self { + id: 0, + filepath, + model_id, + created: now, + } + } + + /// Create a new upload for model + pub async fn create(file: ModelUpload) -> Result { + let pool = unsafe { get_client() }; + + let rec = sqlx::query_as!( + ModelUpload, + r#" + INSERT INTO uploads (filepath, model_id, created) + VALUES ( $1, $2, $3) + RETURNING * + "#, + file.filepath, + file.model_id, + file.created, + ) + .fetch_one(pool) + .await?; + + Ok(rec) + } +} -- cgit v1.2.3-71-g8e6c