Improve tag endpoint
This commit is contained in:
parent
8215461e0d
commit
bb76e3db4d
7 changed files with 147 additions and 12 deletions
|
@ -1,6 +1,8 @@
|
|||
use actix_web::{get, web, HttpResponse};
|
||||
use actix_web::{delete, get, post, web, HttpResponse};
|
||||
use emgauwa_lib::db::DbTag;
|
||||
use emgauwa_lib::errors::EmgauwaError;
|
||||
use emgauwa_lib::errors::{DatabaseError, EmgauwaError};
|
||||
use emgauwa_lib::models::{FromDbModel, Tag};
|
||||
use emgauwa_lib::types::RequestCreateTag;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
#[get("/api/v1/tags")]
|
||||
|
@ -13,3 +15,47 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, Emgauw
|
|||
|
||||
Ok(HttpResponse::Ok().json(tags))
|
||||
}
|
||||
|
||||
#[get("/api/v1/tags/{tag_name}")]
|
||||
pub async fn show(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
path: web::Path<(String,)>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let (tag_name,) = path.into_inner();
|
||||
|
||||
let tag = DbTag::get_by_tag(&mut pool_conn, &tag_name)
|
||||
.await?
|
||||
.ok_or(DatabaseError::NotFound)?;
|
||||
|
||||
let return_tag = Tag::from_db_model(&mut pool_conn, tag)?;
|
||||
Ok(HttpResponse::Ok().json(return_tag))
|
||||
}
|
||||
|
||||
#[delete("/api/v1/tags/{tag_name}")]
|
||||
pub async fn delete(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
path: web::Path<(String,)>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let (tag_name,) = path.into_inner();
|
||||
|
||||
DbTag::delete_by_tag(&mut pool_conn, &tag_name).await?;
|
||||
Ok(HttpResponse::Ok().json("tag got deleted"))
|
||||
}
|
||||
|
||||
#[post("/api/v1/tags")]
|
||||
pub async fn add(
|
||||
pool: web::Data<Pool<Sqlite>>,
|
||||
data: web::Json<RequestCreateTag>,
|
||||
) -> Result<HttpResponse, EmgauwaError> {
|
||||
let mut pool_conn = pool.acquire().await?;
|
||||
|
||||
let new_tag = DbTag::create(&mut pool_conn, &data.tag).await?;
|
||||
|
||||
let cache = (Vec::new(), Vec::new()); // a new tag can't have any relays or schedules
|
||||
let return_tag = Tag::from_db_model_cache(&mut pool_conn, new_tag, cache)?;
|
||||
Ok(HttpResponse::Created().json(return_tag))
|
||||
}
|
||||
|
|
|
@ -85,6 +85,9 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
.service(handlers::v1::schedules::update)
|
||||
.service(handlers::v1::schedules::delete)
|
||||
.service(handlers::v1::tags::index)
|
||||
.service(handlers::v1::tags::show)
|
||||
.service(handlers::v1::tags::delete)
|
||||
.service(handlers::v1::tags::add)
|
||||
.service(handlers::v1::ws::ws_controllers)
|
||||
})
|
||||
.listen(listener)?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue