summaryrefslogtreecommitdiffstats
path: root/src/models/model.rs
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-12 12:03:12 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-12 12:03:12 +0000
commit56e50564e87cc764fc5b815f19aa56560f78e4d3 (patch)
treef97899198cb722b6b9b2eab00d3a7f20fef5f354 /src/models/model.rs
parent63b49b0876457a1bce9405a571f3c9b97a270fb4 (diff)
Keep saved filepath in the database
Diffstat (limited to 'src/models/model.rs')
-rw-r--r--src/models/model.rs41
1 files changed, 41 insertions, 0 deletions
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<JsonValue>,
}
+#[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<ModelUpload, AppError> {
+ 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)
+ }
+}