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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
use crate::config::CONFIG;
use crate::db::get_client;
use crate::errors::AppError;
use serde_json::json;
use sqlx::types::JsonValue;
use sqlx::Row;
use chrono::{Local, NaiveDateTime};
use serde::{Deserialize, Serialize};
use validator::Validate;
/// Model for models.
#[derive(Deserialize, Serialize, Validate, sqlx::FromRow)]
pub struct Model {
id: i32,
#[validate(length(min = 2, message = "Can not be empty"))]
name: String,
description: Option<String>,
duration: i32,
height: i32,
weight: i32,
printer: Option<String>,
material: Option<String>,
author_id: i32,
created: NaiveDateTime,
updated: NaiveDateTime,
}
/// Payload used for model creation
#[derive(Deserialize)]
pub struct ModelCreate {
pub name: String,
pub description: Option<String>,
pub duration: i32,
pub height: i32,
pub weight: i32,
pub printer: Option<String>,
pub material: Option<String>,
}
#[derive(Serialize, sqlx::FromRow)]
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>,
uploads: Option<JsonValue>,
}
#[derive(Deserialize, Serialize, sqlx::FromRow)]
pub struct ModelUpload {
id: i32,
model_id: i32,
filepath: String,
created: NaiveDateTime,
}
impl Model {
pub fn new(
name: String,
description: Option<String>,
duration: i32,
height: i32,
weight: i32,
printer: Option<String>,
material: Option<String>,
author_id: i32,
) -> Self {
let now = Local::now().naive_utc();
Self {
id: 0,
name,
description,
duration,
height,
weight,
printer,
material,
author_id,
created: now,
updated: now,
}
}
/// Create a new model
pub async fn create(model: Model) -> Result<Model, AppError> {
let pool = unsafe { get_client() };
model
.validate()
.map_err(|error| AppError::BadRequest(error.to_string()))?;
let rec: Model = sqlx::query_as(
r#"
INSERT INTO models (name, description, duration, height, weight, printer, material, author_id, created, updated)
VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING *
"#)
.bind(model.name)
.bind(model.description)
.bind(model.duration)
.bind(model.height)
.bind(model.weight)
.bind(model.printer)
.bind(model.material)
.bind(model.author_id)
.bind(model.created)
.bind(model.updated)
.fetch_one(pool)
.await?;
Ok(rec)
}
/// Returns the model with id = `model_id`
pub async fn find_by_id(model_id: i32) -> Result<ModelUser, AppError> {
let pool = unsafe { get_client() };
let rec: ModelUser = sqlx::query_as(
r#"
SELECT
models.*,
json_build_object('id', users.id, 'email', users.email, 'username', users.username, 'is_staff', users.is_staff, 'avatar', users.avatar) as author,
json_agg(uploads.*) filter (where uploads.* is not null) as uploads
FROM models
JOIN users ON users.id = models.author_id
LEFT JOIN uploads ON uploads.model_id = models.id
WHERE models.id = $1
GROUP BY models.id, users.id
"#)
.bind(model_id)
.fetch_one(pool)
.await?;
Ok(rec)
}
/// List all models
pub async fn list(page: i64) -> Result<Vec<ModelUser>, AppError> {
let pool = unsafe { get_client() };
let rows: Vec<ModelUser>= sqlx::query_as(
r#"
SELECT
models.*,
json_build_object('id', users.id, 'email', users.email, 'username', users.username, 'is_staff', users.is_staff, 'avatar', users.avatar) as author,
json_agg(uploads.*) filter (where uploads.* is not null) as uploads
FROM models
JOIN users ON users.id = models.author_id
LEFT JOIN uploads ON uploads.model_id = models.id
GROUP BY models.id, users.id
LIMIT $1 OFFSET $2
"#)
.bind(CONFIG.page_limit)
.bind(CONFIG.page_limit * page)
.fetch_all(pool)
.await?;
Ok(rows)
}
/// Return the number of models.
pub async fn count() -> Result<i64, AppError> {
let pool = unsafe { get_client() };
let cursor = sqlx::query(r#"SELECT COUNT(id) as count FROM models"#)
.fetch_one(pool)
.await?;
let count: i64 = cursor.try_get(0).unwrap();
Ok(count)
}
}
impl ModelUser {
pub fn author_id(&self) -> JsonValue {
match &self.author {
Some(json) => json.get("id").unwrap().clone(),
None => json!(0),
}
}
}
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: ModelUpload = sqlx::query_as(
r#"
INSERT INTO uploads (filepath, model_id, created)
VALUES ( $1, $2, $3)
RETURNING *
"#,
)
.bind(file.filepath)
.bind(file.model_id)
.bind(file.created)
.fetch_one(pool)
.await?;
Ok(rec)
}
}
|