mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-26 19:57:25 +01:00
Allow to upload works
This commit is contained in:
parent
9c255d0cfe
commit
ed14988a56
12 changed files with 948 additions and 341 deletions
|
|
@ -39,6 +39,10 @@ async fn main() -> std::io::Result<()> {
|
|||
.service(update_instrument)
|
||||
.service(delete_instrument)
|
||||
.service(get_instruments)
|
||||
.service(get_work)
|
||||
.service(update_work)
|
||||
.service(delete_work)
|
||||
.service(get_works)
|
||||
});
|
||||
|
||||
server.bind("127.0.0.1:8087")?.run().await
|
||||
|
|
|
|||
|
|
@ -0,0 +1,74 @@
|
|||
use super::authenticate;
|
||||
use crate::database;
|
||||
use crate::database::{DbPool, Work};
|
||||
use crate::error::ServerError;
|
||||
use actix_web::{delete, get, post, web, HttpResponse};
|
||||
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
||||
|
||||
/// Get an existing work.
|
||||
#[get("/works/{id}")]
|
||||
pub async fn get_work(
|
||||
db: web::Data<DbPool>,
|
||||
id: web::Path<String>,
|
||||
) -> Result<HttpResponse, ServerError> {
|
||||
let data = web::block(move || {
|
||||
let conn = db.into_inner().get()?;
|
||||
database::get_work(&conn, &id.into_inner())?.ok_or(ServerError::NotFound)
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(data))
|
||||
}
|
||||
|
||||
/// Add a new work or update an existin one. The user must be authorized to do that.
|
||||
#[post("/works")]
|
||||
pub async fn update_work(
|
||||
auth: BearerAuth,
|
||||
db: web::Data<DbPool>,
|
||||
data: web::Json<Work>,
|
||||
) -> Result<HttpResponse, ServerError> {
|
||||
web::block(move || {
|
||||
let conn = db.into_inner().get()?;
|
||||
let user = authenticate(&conn, auth.token()).or(Err(ServerError::Unauthorized))?;
|
||||
|
||||
database::update_work(&conn, &data.into_inner(), &user)?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
|
||||
#[get("/persons/{id}/works")]
|
||||
pub async fn get_works(
|
||||
db: web::Data<DbPool>,
|
||||
composer_id: web::Path<String>,
|
||||
) -> Result<HttpResponse, ServerError> {
|
||||
let data = web::block(move || {
|
||||
let conn = db.into_inner().get()?;
|
||||
Ok(database::get_works(&conn, &composer_id.into_inner())?)
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(data))
|
||||
}
|
||||
|
||||
#[delete("/works/{id}")]
|
||||
pub async fn delete_work(
|
||||
auth: BearerAuth,
|
||||
db: web::Data<DbPool>,
|
||||
id: web::Path<String>,
|
||||
) -> Result<HttpResponse, ServerError> {
|
||||
web::block(move || {
|
||||
let conn = db.into_inner().get()?;
|
||||
let user = authenticate(&conn, auth.token()).or(Err(ServerError::Unauthorized))?;
|
||||
|
||||
database::delete_work(&conn, &id.into_inner(), &user)?;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue