summaryrefslogtreecommitdiff
path: root/src/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/database')
-rw-r--r--src/database/album.rs35
-rw-r--r--src/database/artist.rs11
-rw-r--r--src/database/mod.rs10
-rw-r--r--src/database/song.rs18
-rw-r--r--src/database/user.rs5
5 files changed, 50 insertions, 29 deletions
diff --git a/src/database/album.rs b/src/database/album.rs
index d7ffae0..6b2b46d 100644
--- a/src/database/album.rs
+++ b/src/database/album.rs
@@ -1,28 +1,34 @@
use crate::database::DatabaseWrapper;
use serde::{Deserialize, Serialize};
use sqlx::mysql::MySqlQueryResult;
+use utoipa::ToSchema;
-#[derive(Serialize)]
+#[derive(Serialize, ToSchema)]
pub struct Album {
+ #[schema(example = "album name")]
name: Option<String>,
+ #[schema(example = "1")]
id: Option<i32>,
- cover: Option<String>,
+ #[schema(example = "just ignore this one")]
artist_name: Option<String>,
+ #[schema(example = "1")]
artist_id: Option<i32>,
}
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, ToSchema)]
pub struct AlbumPost {
+ #[schema(example = "album name")]
name: Option<String>,
- cover: Option<String>,
+ #[schema(example = "just ignore this one")]
artist_id: Option<String>,
}
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, ToSchema)]
pub struct AlbumPut {
id: Option<String>,
+ #[schema(example = "album name")]
name: Option<String>,
- cover: Option<String>,
+ #[schema(example = "just ignore this one")]
artist_id: Option<String>,
}
@@ -30,7 +36,7 @@ impl DatabaseWrapper {
pub async fn select_albums(&self) -> Result<Vec<Album>, sqlx::Error> {
sqlx::query_as!(
Album,
- "SELECT album.name, album.id, album.cover,
+ "SELECT album.name, album.id,
artist.name as artist_name, artist.id as artist_id
FROM album
INNER JOIN artist ON album.artist_id = artist.id
@@ -43,7 +49,7 @@ impl DatabaseWrapper {
pub async fn select_album_by_id(&self, id: &str) -> Result<Option<Album>, sqlx::Error> {
sqlx::query_as!(
Album,
- "SELECT album.name, album.id, album.cover,
+ "SELECT album.name, album.id,
artist.name as artist_name, artist.id as artist_id
FROM album
INNER JOIN artist ON album.artist_id = artist.id
@@ -58,7 +64,7 @@ impl DatabaseWrapper {
let name: String = format!("{}{}{}", "%", name_raw, "%");
sqlx::query_as!(
Album,
- "SELECT album.name, album.id, album.cover,
+ "SELECT album.name, album.id,
artist.name as artist_name, artist.id as artist_id
FROM album
INNER JOIN artist ON album.artist_id = artist.id
@@ -76,7 +82,7 @@ impl DatabaseWrapper {
) -> Result<Vec<Album>, sqlx::Error> {
sqlx::query_as!(
Album,
- "SELECT album.name, album.id, album.cover,
+ "SELECT album.name, album.id,
artist.name as artist_name, artist.id as artist_id
FROM album
INNER JOIN artist ON album.artist_id = artist.id
@@ -103,10 +109,9 @@ impl DatabaseWrapper {
}
sqlx::query!(
- "INSERT INTO album (name, cover, artist_id)
- VALUE (?, ?, ?)",
+ "INSERT INTO album (name, artist_id)
+ VALUE (?, ?)",
data.name,
- data.cover.unwrap_or(String::default()),
data.artist_id,
)
.execute(&self.db_pool)
@@ -125,11 +130,9 @@ impl DatabaseWrapper {
Err(_) => return Err(sqlx::Error::RowNotFound),
};
sqlx::query!(
- "UPDATE album SET name=?, cover=? WHERE id=?",
+ "UPDATE album SET name=? 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.id,
)
.execute(&self.db_pool)
diff --git a/src/database/artist.rs b/src/database/artist.rs
index 1d4e1cd..0bdc7c1 100644
--- a/src/database/artist.rs
+++ b/src/database/artist.rs
@@ -5,20 +5,23 @@ use utoipa::ToSchema;
#[derive(Serialize, ToSchema)]
pub struct Artist {
- #[schema(example = "Attempt", required = true)]
+ #[schema(example = "artist name")]
name: Option<String>,
- #[schema(example = 3, required = true)]
+ #[schema(example = "1")]
id: Option<i32>,
}
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, ToSchema)]
pub struct ArtistPost {
+ #[schema(example = "artist name", required = true)]
name: Option<String>,
}
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, ToSchema)]
pub struct ArtistPut {
+ #[schema(example = "1", required = true)]
id: Option<String>,
+ #[schema(example = "new name", required = true)]
name: Option<String>,
}
diff --git a/src/database/mod.rs b/src/database/mod.rs
index 884a597..ab30884 100644
--- a/src/database/mod.rs
+++ b/src/database/mod.rs
@@ -7,9 +7,11 @@ pub mod user;
use serde::{Deserialize, Serialize};
use sqlx::mysql::{MySqlPool, MySqlPoolOptions};
use std::env;
+use utoipa::ToSchema;
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, ToSchema)]
pub struct Delete {
+ #[schema(example = "1", required = true)]
pub id: Option<String>,
}
@@ -21,11 +23,7 @@ impl DatabaseWrapper {
pub async fn new() -> Result<DatabaseWrapper, sqlx::Error> {
let pool: MySqlPool = MySqlPoolOptions::new()
.max_connections(10)
- .connect(
- env::var("DATABASE_URL")
- .expect("Environment variable DATABASE_URL is *probably not setted up!!")
- .as_str(),
- )
+ .connect(env!("DATABASE_URL"))
.await
.unwrap(); /* This will break in case of error. It's intended. */
diff --git a/src/database/song.rs b/src/database/song.rs
index 95b05ba..a4c860c 100644
--- a/src/database/song.rs
+++ b/src/database/song.rs
@@ -5,27 +5,41 @@ use utoipa::ToSchema;
#[derive(Serialize, ToSchema)]
pub struct Song {
+ #[schema(example = "song name")]
name: Option<String>,
+ #[schema(example = "1")]
id: Option<i32>,
+ #[schema(example = "song lyrics...")]
lyrics: Option<String>,
+ #[schema(example = "album name")]
album_name: Option<String>,
+ #[schema(example = "1")]
album_id: Option<i32>,
+ #[schema(example = "artist name")]
artist_name: Option<String>,
+ #[schema(example = "1")]
artist_id: Option<i32>,
}
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, ToSchema)]
pub struct SongPost {
+ #[schema(example = "song name")]
name: Option<String>,
+ #[schema(example = "song lyrics...")]
lyrics: Option<String>,
+ #[schema(example = "1")]
album_id: Option<String>,
}
-#[derive(Serialize, Deserialize)]
+#[derive(Serialize, Deserialize, ToSchema)]
pub struct SongPut {
+ #[schema(example = "1")]
id: Option<String>,
+ #[schema(example = "song name")]
name: Option<String>,
+ #[schema(example = "song lyrics...")]
lyrics: Option<String>,
+ #[schema(example = "1")]
album_id: Option<String>,
}
diff --git a/src/database/user.rs b/src/database/user.rs
index ff7b1ab..d3202ce 100644
--- a/src/database/user.rs
+++ b/src/database/user.rs
@@ -1,6 +1,7 @@
use crate::database::DatabaseWrapper;
use serde::{Deserialize, Serialize};
use sqlx::mysql::MySqlQueryResult;
+use utoipa::ToSchema;
#[derive(Deserialize, Serialize)]
pub struct User {
@@ -9,9 +10,11 @@ pub struct User {
password: Option<String>,
}
-#[derive(Deserialize, Serialize)]
+#[derive(Deserialize, Serialize, ToSchema)]
pub struct UserForm {
+ #[schema(example = "username")]
name: Option<String>,
+ #[schema(example = "password")]
password: Option<String>,
}