summaryrefslogtreecommitdiff
path: root/src/api/mod.rs
blob: fc97f4781f32eb9cbf404c0e83645a06180d2f59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use actix_web::{web, HttpResponse, Scope};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

pub mod album;
pub mod artist;
pub mod search_results;
pub mod song;

/* Set up scope */
pub fn api_scope() -> Scope {
    web::scope("/api")
        .service(song::get_song)
        .service(song::post_song)
        .service(song::put_song)
        .service(song::delete_song)
        .service(album::get_album)
        .service(album::post_album)
        .service(album::put_album)
        .service(album::delete_album)
        .service(artist::get_artist)
        .service(artist::post_artist)
        .service(artist::put_artist)
        .service(artist::delete_artist)
        .service(search_results::search_results)
}

#[derive(Serialize, Deserialize, ToSchema)]
pub struct Response {
    #[schema(default = "response")]
    pub message: String,
}

pub fn get_response_from_query(
    query: Result<sqlx::mysql::MySqlQueryResult, sqlx::Error>,
    method: String,
) -> HttpResponse {
    match query {
        Ok(_) => HttpResponse::Ok().json(Response {
            message: format!("{} request executed with no errors", method).to_owned(),
        }),
        Err(e) => HttpResponse::BadRequest().json(Response {
            message: format!("There was an issue in the request: {}", e).to_owned(),
        }),
    }
}