From 5898e5ab4359945ef52dc7460bb2937a85603fc2 Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Sun, 26 Jan 2025 12:25:13 +0100 Subject: authentication done --- src/database/album.rs | 13 ++++--- src/database/artist.rs | 9 +++-- src/database/mod.rs | 4 +- src/database/search_results.rs | 6 +-- src/database/song.rs | 12 ++++-- src/database/user.rs | 84 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 111 insertions(+), 17 deletions(-) create mode 100644 src/database/user.rs (limited to 'src/database') diff --git a/src/database/album.rs b/src/database/album.rs index 87054f1..d7ffae0 100644 --- a/src/database/album.rs +++ b/src/database/album.rs @@ -114,18 +114,22 @@ impl DatabaseWrapper { } pub async fn edit_album(&self, data: AlbumPut) -> Result { - if data.id.is_none() { return Err(sqlx::Error::RowNotFound); } + if data.id.is_none() { + return Err(sqlx::Error::RowNotFound); + } let og_album: Album = match self.select_album_by_id(data.id.as_ref().unwrap()).await { Ok(res) => match res.is_some() { true => res.unwrap(), false => return Err(sqlx::Error::RowNotFound), - } + }, Err(_) => return Err(sqlx::Error::RowNotFound), }; sqlx::query!( "UPDATE album SET name=?, cover=? WHERE id=?", - data.name.unwrap_or(og_album.name.unwrap_or(String::default())), - data.cover.unwrap_or(og_album.cover.unwrap_or(String::default())), + data.name + .unwrap_or(og_album.name.unwrap_or(String::default())), + data.cover + .unwrap_or(og_album.cover.unwrap_or(String::default())), data.id, ) .execute(&self.db_pool) @@ -149,5 +153,4 @@ impl DatabaseWrapper { .execute(&self.db_pool) .await } - } diff --git a/src/database/artist.rs b/src/database/artist.rs index 5dda7a8..1d4e1cd 100644 --- a/src/database/artist.rs +++ b/src/database/artist.rs @@ -72,17 +72,20 @@ impl DatabaseWrapper { } pub async fn edit_artist(&self, data: ArtistPut) -> Result { - if data.id.is_none() { return Err(sqlx::Error::RowNotFound); } + if data.id.is_none() { + return Err(sqlx::Error::RowNotFound); + } let og_artist: Artist = match self.select_artist_by_id(data.id.as_ref().unwrap()).await { Ok(res) => match res.is_some() { true => res.unwrap(), false => return Err(sqlx::Error::RowNotFound), - } + }, Err(_) => return Err(sqlx::Error::RowNotFound), }; sqlx::query!( "UPDATE artist SET name=? WHERE id=?", - data.name.unwrap_or(og_artist.name.unwrap_or(String::default())), + data.name + .unwrap_or(og_artist.name.unwrap_or(String::default())), data.id, ) .execute(&self.db_pool) diff --git a/src/database/mod.rs b/src/database/mod.rs index d66b72b..884a597 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,7 +1,8 @@ -pub mod song; pub mod album; pub mod artist; pub mod search_results; +pub mod song; +pub mod user; use serde::{Deserialize, Serialize}; use sqlx::mysql::{MySqlPool, MySqlPoolOptions}; @@ -31,4 +32,3 @@ impl DatabaseWrapper { Ok(DatabaseWrapper { db_pool: pool }) } } - diff --git a/src/database/search_results.rs b/src/database/search_results.rs index 027d160..3f0bf01 100644 --- a/src/database/search_results.rs +++ b/src/database/search_results.rs @@ -1,7 +1,7 @@ -use crate::database::DatabaseWrapper; -use crate::database::song::Song; -use crate::database::artist::Artist; use crate::database::album::Album; +use crate::database::artist::Artist; +use crate::database::song::Song; +use crate::database::DatabaseWrapper; impl DatabaseWrapper { pub async fn search_results_by_name( diff --git a/src/database/song.rs b/src/database/song.rs index 45c217b..95b05ba 100644 --- a/src/database/song.rs +++ b/src/database/song.rs @@ -140,18 +140,22 @@ impl DatabaseWrapper { } pub async fn edit_song(&self, data: SongPut) -> Result { - if data.id.is_none() { return Err(sqlx::Error::RowNotFound); } + if data.id.is_none() { + return Err(sqlx::Error::RowNotFound); + } let og_song: Song = match self.select_song_by_id(data.id.as_ref().unwrap()).await { Ok(res) => match res.is_some() { true => res.unwrap(), false => return Err(sqlx::Error::RowNotFound), - } + }, Err(_) => return Err(sqlx::Error::RowNotFound), }; sqlx::query!( "UPDATE song SET name=?, lyrics=? WHERE id=?", - data.name.unwrap_or(og_song.name.unwrap_or(String::default())), - data.lyrics.unwrap_or(og_song.lyrics.unwrap_or(String::default())), + data.name + .unwrap_or(og_song.name.unwrap_or(String::default())), + data.lyrics + .unwrap_or(og_song.lyrics.unwrap_or(String::default())), data.id, ) .execute(&self.db_pool) diff --git a/src/database/user.rs b/src/database/user.rs new file mode 100644 index 0000000..ff7b1ab --- /dev/null +++ b/src/database/user.rs @@ -0,0 +1,84 @@ +use crate::database::DatabaseWrapper; +use serde::{Deserialize, Serialize}; +use sqlx::mysql::MySqlQueryResult; + +#[derive(Deserialize, Serialize)] +pub struct User { + pub id: Option, + pub name: Option, + password: Option, +} + +#[derive(Deserialize, Serialize)] +pub struct UserForm { + name: Option, + password: Option, +} + +impl DatabaseWrapper { + pub async fn register(&self, data: UserForm) -> Result { + if data.name.is_none() || data.password.is_none() { + return Err(sqlx::Error::RowNotFound); + } + + sqlx::query!( + "INSERT INTO user (name, password) + VALUE (?, PASSWORD(?))", + data.name, + data.password, + ) + .execute(&self.db_pool) + .await + } + + pub async fn login(&self, data: UserForm) -> Result, sqlx::Error> { + if data.name.is_none() || data.password.is_none() { + return Err(sqlx::Error::RowNotFound); + } + + sqlx::query_as!( + User, + "SELECT * FROM user + WHERE ? = name + AND password(?) = password + ", + data.name, + data.password, + ) + .fetch_optional(&self.db_pool) + .await + } + + pub async fn delete_user(&self, data: UserForm) -> Result { + if data.name.is_none() || data.password.is_none() { + return Err(sqlx::Error::RowNotFound); + } + + match sqlx::query_as!( + User, + "SELECT * FROM user + WHERE name = ? + AND password = password(?) + ", + data.name, + data.password, + ) + .fetch_one(&self.db_pool) + .await + { + Ok(_) => (), + Err(_) => return Err(sqlx::Error::RowNotFound), + }; + + sqlx::query!( + "DELETE FROM user + WHERE name = ? + AND password = password(?) + ", + data.name, + data.password, + ) + .execute(&self.db_pool) + .await + } +} -- cgit v1.2.3