summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-09-09 09:31:25 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-09-09 09:31:25 +0000
commite8c799cb65f58c1e9a4b2809489ef3b5760638d8 (patch)
tree44373b0b59bb868598f9012fc15c327603e4cf87
parentacc32ccc1650b99e03c390a87e71b0a85b073162 (diff)
Serialize author' info for models
-rw-r--r--Cargo.lock1
-rw-r--r--Cargo.toml2
-rw-r--r--src/models/model.rs31
-rw-r--r--src/routes/model.rs4
4 files changed, 31 insertions, 7 deletions
diff --git a/Cargo.lock b/Cargo.lock
index e711a7c..5804124 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1354,6 +1354,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
+ "serde_json",
"sha2 0.10.2",
"sqlx-core",
"sqlx-rt",
diff --git a/Cargo.toml b/Cargo.toml
index 13dee30..8744df1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,7 +16,7 @@ tokio = { version = "1.20", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tower-http = { version = "0.3.4", features = ["trace", "compression-br", "propagate-header", "sensitive-headers"] }
-sqlx = { version = "0.6", features = [ "runtime-tokio-rustls", "postgres", "chrono" ] }
+sqlx = { version = "0.6", features = [ "runtime-tokio-rustls", "postgres", "chrono", "json" ] }
sha256 = "1.0.3"
validator = { version = "0.16.0", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
diff --git a/src/models/model.rs b/src/models/model.rs
index 1fe9b61..e3fc7ac 100644
--- a/src/models/model.rs
+++ b/src/models/model.rs
@@ -1,5 +1,6 @@
use crate::db::get_client;
use crate::errors::AppError;
+use sqlx::types::JsonValue;
use chrono::{Local, NaiveDateTime};
use serde::{Deserialize, Serialize};
@@ -34,6 +35,22 @@ pub struct ModelCreate {
pub material: Option<String>,
}
+#[derive(Serialize)]
+pub struct ModelUser {
+ id: i32,
+ name: String,
+ description: Option<String>,
+ duration: i32,
+ height: i32,
+ weight: i32,
+ printer: Option<String>,
+ material: Option<String>,
+ author_id: i32,
+ created: NaiveDateTime,
+ updated: NaiveDateTime,
+ author: Option<JsonValue>,
+}
+
impl Model {
pub fn new(
name: String,
@@ -94,11 +111,17 @@ impl Model {
}
/// List all models
- pub async fn list() -> Result<Vec<Model>, AppError> {
+ pub async fn list() -> Result<Vec<ModelUser>, AppError> {
let pool = unsafe { get_client() };
- let rows = sqlx::query_as!(Model, r#"SELECT * FROM models"#)
- .fetch_all(pool)
- .await?;
+ let rows = sqlx::query_as!(
+ ModelUser,
+ r#"SELECT
+ models.*,
+ json_build_object('id', users.id, 'email', users.email, 'username', users.username, 'is_staff', users.is_staff) as author
+ FROM models JOIN users ON users.id = models.author_id"#
+ )
+ .fetch_all(pool)
+ .await?;
Ok(rows)
}
diff --git a/src/routes/model.rs b/src/routes/model.rs
index b1d4cc2..a5cd2b3 100644
--- a/src/routes/model.rs
+++ b/src/routes/model.rs
@@ -1,7 +1,7 @@
use crate::errors::AppError;
use crate::models::{
auth::Claims,
- model::{Model, ModelCreate},
+ model::{Model, ModelCreate, ModelUser},
};
use axum::{routing::get, Json, Router};
@@ -11,7 +11,7 @@ pub fn create_route() -> Router {
}
/// List models.
-async fn list_models() -> Result<Json<Vec<Model>>, AppError> {
+async fn list_models() -> Result<Json<Vec<ModelUser>>, AppError> {
let models = Model::list().await?;
Ok(Json(models))