67 lines
3.4 KiB
Rust
67 lines
3.4 KiB
Rust
use super::Database;
|
|
use crate::models::*;
|
|
use sqlx::Row;
|
|
use uuid::Uuid;
|
|
|
|
impl Database {
|
|
pub async fn get_user_favorites(&self, user_id: &str) -> Result<Vec<FavoriteWithImage>, sqlx::Error> {
|
|
let rows = sqlx::query(
|
|
r#"SELECT f.id, f.user_id, f.image_id, f.album_id, f.created_at,
|
|
i.album_id as img_album_id, i.filename, i.original_url, i.title, i.description,
|
|
i.file_size, i.width, i.height, i.mime_type, i.created_at as img_created_at,
|
|
a.title as album_title
|
|
FROM favorites f
|
|
JOIN images i ON f.image_id = i.id
|
|
JOIN albums a ON f.album_id = a.id
|
|
WHERE f.user_id = ? ORDER BY f.created_at DESC"#
|
|
).bind(user_id).fetch_all(&self.pool).await?;
|
|
|
|
Ok(rows.iter().map(|r| {
|
|
let fav_id = Uuid::parse_str(&r.get::<String, _>("id")).unwrap_or_else(|_| Uuid::nil());
|
|
let img_id = Uuid::parse_str(&r.get::<String, _>("image_id")).unwrap_or_else(|_| Uuid::nil());
|
|
let aid = Uuid::parse_str(&r.get::<String, _>("album_id")).unwrap_or_else(|_| Uuid::nil());
|
|
let img_aid = Uuid::parse_str(&r.get::<String, _>("img_album_id")).unwrap_or_else(|_| Uuid::nil());
|
|
FavoriteWithImage {
|
|
favorite: Favorite { id: fav_id, user_id: r.get("user_id"), image_id: img_id, album_id: aid, created_at: r.get("created_at") },
|
|
image: crate::models::image::Image {
|
|
id: img_id, album_id: img_aid,
|
|
filename: r.get("filename"), original_url: r.get("original_url"),
|
|
title: r.get("title"), description: r.get("description"),
|
|
file_size: r.get("file_size"), width: r.get("width"),
|
|
height: r.get("height"), mime_type: r.get("mime_type"),
|
|
created_at: r.get("img_created_at"),
|
|
},
|
|
album_title: r.get("album_title"),
|
|
}
|
|
}).collect())
|
|
}
|
|
|
|
pub async fn get_favorite_status(&self, user_id: &str, image_id: Uuid) -> Result<FavoriteStatus, sqlx::Error> {
|
|
let row = sqlx::query("SELECT id FROM favorites WHERE user_id = ? AND image_id = ?")
|
|
.bind(user_id).bind(image_id.to_string()).fetch_optional(&self.pool).await?;
|
|
Ok(FavoriteStatus {
|
|
is_favorited: row.is_some(),
|
|
favorite_id: row.map(|r| Uuid::parse_str(&r.get::<String, _>("id")).unwrap_or_else(|_| Uuid::nil())),
|
|
})
|
|
}
|
|
|
|
pub async fn add_favorite(&self, user_id: &str, fav: CreateFavorite) -> Result<Uuid, sqlx::Error> {
|
|
let id = Uuid::new_v4();
|
|
let now = chrono::Utc::now().to_rfc3339();
|
|
sqlx::query("INSERT INTO favorites (id, user_id, image_id, album_id, created_at) VALUES (?, ?, ?, ?, ?)")
|
|
.bind(id.to_string()).bind(user_id).bind(fav.image_id.to_string()).bind(fav.album_id.to_string()).bind(&now)
|
|
.execute(&self.pool).await?;
|
|
Ok(id)
|
|
}
|
|
|
|
pub async fn remove_favorite(&self, favorite_id: Uuid) -> Result<(), sqlx::Error> {
|
|
sqlx::query("DELETE FROM favorites WHERE id = ?").bind(favorite_id.to_string()).execute(&self.pool).await?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn remove_favorite_by_image(&self, user_id: &str, image_id: Uuid) -> Result<(), sqlx::Error> {
|
|
sqlx::query("DELETE FROM favorites WHERE user_id = ? AND image_id = ?")
|
|
.bind(user_id).bind(image_id.to_string()).execute(&self.pool).await?;
|
|
Ok(())
|
|
}
|
|
} |