This commit is contained in:
JMARyA 2024-09-11 11:30:05 +02:00
parent edfb2da790
commit abaa9f12c9
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 114 additions and 105 deletions

View file

@ -71,15 +71,13 @@ impl User {
}
/// Change the password of a `User`
pub async fn passwd(&mut self, old: &str, new: &str) -> Result<(), ()> {
pub async fn passwd(self, old: &str, new: &str) -> Result<(), ()> {
if self.verify_pw(old) {
self.update(&json!(
{
"password": bcrypt::hash(new, bcrypt::DEFAULT_COST).unwrap()
}
))
.await
.map_err(|_| ())?;
self.change()
.password(bcrypt::hash(new, bcrypt::DEFAULT_COST).unwrap())
.update()
.await
.map_err(|_| ())?;
return Ok(());
}

View file

@ -116,8 +116,8 @@ pub async fn playlist_tracks_route(id: &str, u: User) -> FallibleApiResponse {
#[derive(rocket::serde::Deserialize)]
pub struct PlaylistData {
pub title: String,
pub visibility: Visibility,
pub title: Option<String>,
pub visibility: Option<Visibility>,
pub tracks: Vec<String>,
}
@ -125,8 +125,14 @@ pub struct PlaylistData {
pub async fn playlist_add_route(playlist: Json<PlaylistData>, u: User) -> FallibleApiResponse {
let playlist = Playlist::create(
u,
&playlist.title,
playlist.visibility.clone(),
&playlist
.title
.clone()
.ok_or_else(|| api_error("Bad Request"))?,
playlist
.visibility
.clone()
.ok_or_else(|| api_error("Bad Request"))?,
&playlist.tracks,
)
.await
@ -142,7 +148,7 @@ pub async fn playlist_edit_route(
edit: Json<PlaylistData>,
u: User,
) -> FallibleApiResponse {
let mut playlist = Playlist::get(id)
let playlist = Playlist::get(id)
.await
.ok_or_else(|| api_error("No playlist with that ID found"))?;
@ -157,14 +163,19 @@ pub async fn playlist_edit_route(
.push(reference_of!(Track, track).ok_or_else(|| api_error("Invalid tracks found"))?);
}
playlist
.update(&json!({
"title": edit.title,
"visibility": edit.visibility,
"tracks": tracks_ref
}))
.await
.unwrap();
let playlist_id = playlist._id.clone();
Ok(json!({"edited": playlist._id}))
let mut changed = playlist.change();
if let Some(title) = &edit.title {
changed = changed.title(title);
}
if let Some(visibility) = &edit.visibility {
changed = changed.visibility(visibility.clone());
}
changed.tracks(tracks_ref).update().await.unwrap();
Ok(json!({"edited": playlist_id}))
}

View file

@ -69,7 +69,7 @@ pub struct PasswdData {
}
#[post("/passwd", data = "<passwd>")]
pub async fn passwd_route(passwd: Json<PasswdData>, mut u: User) -> FallibleApiResponse {
pub async fn passwd_route(passwd: Json<PasswdData>, u: User) -> FallibleApiResponse {
u.passwd(&passwd.old, &passwd.new)
.await
.map_err(|()| api_error("Password change failed"))?;