summaryrefslogtreecommitdiffstats
path: root/src/models
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-10-03 18:37:08 +0000
committerSanto Cariotti <santo@dcariotti.me>2022-10-03 18:37:08 +0000
commit09447097adcfbd31f28dc686ec43380b9006ac9e (patch)
tree19864599d3719e63cfe37688e67f20ae337d7a62 /src/models
parentc1f61c458e0e97586796a3acdbd21aedf8b2e8ad (diff)
Show likes as array
Diffstat (limited to 'src/models')
-rw-r--r--src/models/model.rs13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/models/model.rs b/src/models/model.rs
index 98ee8d4..47864d9 100644
--- a/src/models/model.rs
+++ b/src/models/model.rs
@@ -54,6 +54,7 @@ pub struct ModelUser {
updated: NaiveDateTime,
author: Option<JsonValue>,
uploads: Option<JsonValue>,
+ likes: Option<JsonValue>,
}
#[derive(Deserialize, Serialize, sqlx::FromRow)]
@@ -160,10 +161,12 @@ impl Model {
SELECT
models.*,
json_build_object('id', users.id, 'name', users.name, '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
+ json_agg(uploads.*) filter (where uploads.* is not null) as uploads,
+ json_agg(likes.*) filter (where likes.* is not null) as likes
FROM models
JOIN users ON users.id = models.author_id
LEFT JOIN uploads ON uploads.model_id = models.id
+ LEFT JOIN likes ON likes.model_id = models.id
WHERE models.id = $1
GROUP BY models.id, users.id
"#)
@@ -182,10 +185,12 @@ impl Model {
SELECT
models.*,
json_build_object('id', users.id, 'name', users.name, '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
+ json_agg(uploads.*) filter (where uploads.* is not null) as uploads,
+ json_agg(likes.*) filter (where likes.* is not null) as likes
FROM models
JOIN users ON users.id = models.author_id
LEFT JOIN uploads ON uploads.model_id = models.id
+ LEFT JOIN likes ON likes.model_id = models.id
GROUP BY models.id, users.id
ORDER BY id DESC
LIMIT $1 OFFSET $2
@@ -206,10 +211,12 @@ impl Model {
SELECT
models.*,
json_build_object('id', users.id, 'name', users.name, '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
+ json_agg(uploads.*) filter (where uploads.* is not null) as uploads,
+ json_agg(likes.*) filter (where likes.* is not null) as likes
FROM models
JOIN users ON users.id = models.author_id
LEFT JOIN uploads ON uploads.model_id = models.id
+ LEFT JOIN likes ON likes.model_id = models.id
WHERE author_id = $1
GROUP BY models.id, users.id
ORDER BY id DESC