Initial commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
use chrono::Utc;
|
||||
use sea_orm::*;
|
||||
|
||||
use crate::entities::wanted_item::{
|
||||
self, ActiveModel, Entity as WantedItems, ItemType, Model as WantedItem, WantedStatus,
|
||||
};
|
||||
use crate::error::{DbError, DbResult};
|
||||
|
||||
pub async fn add(
|
||||
db: &DatabaseConnection,
|
||||
item_type: ItemType,
|
||||
artist_id: Option<i32>,
|
||||
album_id: Option<i32>,
|
||||
track_id: Option<i32>,
|
||||
) -> DbResult<WantedItem> {
|
||||
let now = Utc::now().naive_utc();
|
||||
let active = ActiveModel {
|
||||
item_type: Set(item_type),
|
||||
artist_id: Set(artist_id),
|
||||
album_id: Set(album_id),
|
||||
track_id: Set(track_id),
|
||||
status: Set(WantedStatus::Wanted),
|
||||
added_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
..Default::default()
|
||||
};
|
||||
Ok(active.insert(db).await?)
|
||||
}
|
||||
|
||||
pub async fn list(
|
||||
db: &DatabaseConnection,
|
||||
status_filter: Option<WantedStatus>,
|
||||
) -> DbResult<Vec<WantedItem>> {
|
||||
let mut query = WantedItems::find();
|
||||
if let Some(status) = status_filter {
|
||||
query = query.filter(wanted_item::Column::Status.eq(status));
|
||||
}
|
||||
Ok(query.all(db).await?)
|
||||
}
|
||||
|
||||
pub async fn get_by_id(db: &DatabaseConnection, id: i32) -> DbResult<WantedItem> {
|
||||
WantedItems::find_by_id(id)
|
||||
.one(db)
|
||||
.await?
|
||||
.ok_or_else(|| DbError::NotFound(format!("wanted_item id={id}")))
|
||||
}
|
||||
|
||||
pub async fn update_status(
|
||||
db: &DatabaseConnection,
|
||||
id: i32,
|
||||
status: WantedStatus,
|
||||
) -> DbResult<WantedItem> {
|
||||
let existing = get_by_id(db, id).await?;
|
||||
let mut active: ActiveModel = existing.into();
|
||||
active.status = Set(status);
|
||||
active.updated_at = Set(Utc::now().naive_utc());
|
||||
Ok(active.update(db).await?)
|
||||
}
|
||||
|
||||
pub async fn remove(db: &DatabaseConnection, id: i32) -> DbResult<()> {
|
||||
WantedItems::delete_by_id(id).exec(db).await?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user