Added auth
This commit is contained in:
+22
-15
@@ -7,23 +7,26 @@ use crate::entities::wanted_item::{
|
||||
};
|
||||
use crate::error::{DbError, DbResult};
|
||||
|
||||
pub async fn add(
|
||||
db: &DatabaseConnection,
|
||||
item_type: ItemType,
|
||||
name: &str,
|
||||
musicbrainz_id: Option<&str>,
|
||||
artist_id: Option<i32>,
|
||||
album_id: Option<i32>,
|
||||
track_id: Option<i32>,
|
||||
) -> DbResult<WantedItem> {
|
||||
pub struct AddWantedItem<'a> {
|
||||
pub item_type: ItemType,
|
||||
pub name: &'a str,
|
||||
pub musicbrainz_id: Option<&'a str>,
|
||||
pub artist_id: Option<i32>,
|
||||
pub album_id: Option<i32>,
|
||||
pub track_id: Option<i32>,
|
||||
pub user_id: Option<i32>,
|
||||
}
|
||||
|
||||
pub async fn add(db: &DatabaseConnection, item: AddWantedItem<'_>) -> DbResult<WantedItem> {
|
||||
let now = Utc::now().naive_utc();
|
||||
let active = ActiveModel {
|
||||
item_type: Set(item_type),
|
||||
name: Set(name.to_string()),
|
||||
musicbrainz_id: Set(musicbrainz_id.map(String::from)),
|
||||
artist_id: Set(artist_id),
|
||||
album_id: Set(album_id),
|
||||
track_id: Set(track_id),
|
||||
item_type: Set(item.item_type),
|
||||
name: Set(item.name.to_string()),
|
||||
musicbrainz_id: Set(item.musicbrainz_id.map(String::from)),
|
||||
artist_id: Set(item.artist_id),
|
||||
album_id: Set(item.album_id),
|
||||
track_id: Set(item.track_id),
|
||||
user_id: Set(item.user_id),
|
||||
status: Set(WantedStatus::Wanted),
|
||||
added_at: Set(now),
|
||||
updated_at: Set(now),
|
||||
@@ -35,11 +38,15 @@ pub async fn add(
|
||||
pub async fn list(
|
||||
db: &DatabaseConnection,
|
||||
status_filter: Option<WantedStatus>,
|
||||
user_id: Option<i32>,
|
||||
) -> DbResult<Vec<WantedItem>> {
|
||||
let mut query = WantedItems::find();
|
||||
if let Some(status) = status_filter {
|
||||
query = query.filter(wanted_item::Column::Status.eq(status));
|
||||
}
|
||||
if let Some(uid) = user_id {
|
||||
query = query.filter(wanted_item::Column::UserId.eq(uid));
|
||||
}
|
||||
Ok(query.all(db).await?)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user