Add update and get-by-tag for schedules
This commit is contained in:
parent
2af71a8d84
commit
e157a5d02d
10 changed files with 107 additions and 18 deletions
src/handlers/v1
|
@ -1,9 +1,11 @@
|
|||
use std::borrow::Borrow;
|
||||
use std::convert::TryFrom;
|
||||
use actix_web::{HttpResponse, Responder, web, get, delete};
|
||||
use actix_web::{HttpResponse, Responder, web, get, post, put, delete};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
use crate::db::models::Periods;
|
||||
use crate::db::schedule::*;
|
||||
use crate::db::schedules::*;
|
||||
use crate::db::tag::get_tag;
|
||||
use crate::handlers::errors::HandlerError;
|
||||
use crate::return_models::ReturnSchedule;
|
||||
use crate::types::EmgauwaUid;
|
||||
|
@ -15,12 +17,27 @@ pub struct RequestSchedule {
|
|||
tags: Vec<String>,
|
||||
}
|
||||
|
||||
#[get("/api/v1/schedules")]
|
||||
pub async fn index() -> impl Responder {
|
||||
let schedules = get_schedules();
|
||||
let return_schedules: Vec<ReturnSchedule> = schedules.into_iter().map(ReturnSchedule::from).collect();
|
||||
HttpResponse::Ok().json(return_schedules)
|
||||
}
|
||||
|
||||
#[get("/api/v1/schedules/tag/{tag}")]
|
||||
pub async fn tagged(web::Path((tag,)): web::Path<(String,)>) -> impl Responder {
|
||||
|
||||
let tag_db = get_tag(&tag);
|
||||
if tag_db.is_err() {
|
||||
return HttpResponse::from(tag_db.unwrap_err())
|
||||
}
|
||||
let tag_db = tag_db.unwrap();
|
||||
|
||||
let schedules = get_schedules_by_tag(&tag_db);
|
||||
let return_schedules: Vec<ReturnSchedule> = schedules.into_iter().map(ReturnSchedule::from).collect();
|
||||
HttpResponse::Ok().json(return_schedules)
|
||||
}
|
||||
|
||||
#[get("/api/v1/schedules/{schedule_id}")]
|
||||
pub async fn show(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Responder {
|
||||
|
||||
|
@ -38,15 +55,16 @@ pub async fn show(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Resp
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn add(post: web::Json<RequestSchedule>) -> impl Responder {
|
||||
let new_schedule = create_schedule(&post.name, &post.periods);
|
||||
#[post("/api/v1/schedules")]
|
||||
pub async fn add(data: web::Json<RequestSchedule>) -> impl Responder {
|
||||
let new_schedule = create_schedule(&data.name, &data.periods);
|
||||
|
||||
if new_schedule.is_err() {
|
||||
return HttpResponse::from(new_schedule.unwrap_err())
|
||||
}
|
||||
let new_schedule = new_schedule.unwrap();
|
||||
|
||||
let result = set_schedule_tags(&new_schedule, post.tags.as_slice());
|
||||
let result = set_schedule_tags(&new_schedule, data.tags.as_slice());
|
||||
if result.is_err() {
|
||||
return HttpResponse::from(result.unwrap_err());
|
||||
}
|
||||
|
@ -54,6 +72,35 @@ pub async fn add(post: web::Json<RequestSchedule>) -> impl Responder {
|
|||
HttpResponse::Created().json(ReturnSchedule::from(new_schedule))
|
||||
}
|
||||
|
||||
#[put("/api/v1/schedules/{schedule_id}")]
|
||||
pub async fn update(web::Path((schedule_uid,)): web::Path<(String,)>, data: web::Json<RequestSchedule>) -> impl Responder {
|
||||
|
||||
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid));
|
||||
if emgauwa_uid.is_err() {
|
||||
return HttpResponse::from(emgauwa_uid.unwrap_err());
|
||||
}
|
||||
let emgauwa_uid = emgauwa_uid.unwrap();
|
||||
|
||||
let schedule = get_schedule_by_uid(emgauwa_uid);
|
||||
if schedule.is_err() {
|
||||
return HttpResponse::from(schedule.unwrap_err());
|
||||
}
|
||||
let schedule = schedule.unwrap();
|
||||
|
||||
let schedule = update_schedule(&schedule, data.name.as_str(), data.periods.borrow());
|
||||
if schedule.is_err() {
|
||||
return HttpResponse::from(schedule.unwrap_err());
|
||||
}
|
||||
let schedule = schedule.unwrap();
|
||||
|
||||
let result = set_schedule_tags(&schedule, data.tags.as_slice());
|
||||
if result.is_err() {
|
||||
return HttpResponse::from(result.unwrap_err());
|
||||
}
|
||||
|
||||
HttpResponse::Ok().json(ReturnSchedule::from(schedule))
|
||||
}
|
||||
|
||||
#[delete("/api/v1/schedules/{schedule_id}")]
|
||||
pub async fn delete(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Responder {
|
||||
let emgauwa_uid = EmgauwaUid::try_from(schedule_uid.as_str()).or(Err(HandlerError::BadUid));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue