summaryrefslogtreecommitdiff
path: root/src/database
diff options
context:
space:
mode:
authorniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2025-01-26 12:25:13 +0100
committerniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2025-01-26 12:25:13 +0100
commit5898e5ab4359945ef52dc7460bb2937a85603fc2 (patch)
tree0ef4c760bd6e30df2897e9cc725552ef54e51983 /src/database
parente3f0f700bf2de3b8e7d7c72893a4d6cb128f000d (diff)
authentication done
Diffstat (limited to 'src/database')
-rw-r--r--src/database/album.rs13
-rw-r--r--src/database/artist.rs9
-rw-r--r--src/database/mod.rs4
-rw-r--r--src/database/search_results.rs6
-rw-r--r--src/database/song.rs12
-rw-r--r--src/database/user.rs84
6 files changed, 111 insertions, 17 deletions
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<MySqlQueryResult, sqlx::Error> {
- 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<MySqlQueryResult, sqlx::Error> {
- 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<MySqlQueryResult, sqlx::Error> {
- 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<i32>,
+ pub name: Option<String>,
+ password: Option<String>,
+}
+
+#[derive(Deserialize, Serialize)]
+pub struct UserForm {
+ name: Option<String>,
+ password: Option<String>,
+}
+
+impl DatabaseWrapper {
+ pub async fn register(&self, data: UserForm) -> Result<MySqlQueryResult, sqlx::Error> {
+ 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<Option<User>, 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<MySqlQueryResult, sqlx::Error> {
+ 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
+ }
+}