Add saving periods

This commit is contained in:
Tobias Reisinger 2021-11-05 16:32:30 +01:00
parent 12d57d020f
commit 483fd60daa
13 changed files with 271 additions and 52 deletions
src/handlers/v1

View file

@ -1,17 +1,58 @@
use std::str::FromStr;
use actix_web::{HttpResponse, Responder, web, get};
use serde::{Serialize, Deserialize};
use uuid::Uuid;
use crate::db;
use actix_web::{HttpResponse, Responder};
use crate::db::models::Periods;
use crate::handlers::errors::HandlerError;
use crate::types::EmgauwaUid;
#[derive(Debug, Serialize, Deserialize)]
pub struct RequestSchedule {
name: String,
periods: Periods,
}
pub async fn index() -> impl Responder {
let schedules = db::get_schedules();
HttpResponse::Ok().json(schedules)
}
pub async fn get() -> impl Responder {
"hello from get schedules by id"
#[get("/api/v1/schedules/{schedule_id}")]
pub async fn show(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Responder {
let emgauwa_uid = match schedule_uid.as_str() {
"on" => Ok(EmgauwaUid::On),
"off" => Ok(EmgauwaUid::Off),
any => match Uuid::from_str(any) {
Ok(uuid) => Ok(EmgauwaUid::Any(uuid)),
Err(_) => Err(HandlerError::BadUidError)
}
};
match emgauwa_uid {
Ok(uid) => {
let schedule = db::get_schedule_by_uid(uid);
match schedule {
Ok(ok) => HttpResponse::Ok().json(ok),
Err(err) => HttpResponse::NotFound().json(err),
}
},
Err(err) => HttpResponse::BadRequest().json(err)
}
}
pub async fn add() -> impl Responder {
let new_schedule = db::create_schedule("TEST");
pub async fn add(post: web::Json<RequestSchedule>) -> impl Responder {
println!("model: {:?}", post);
for period in post.periods.0.iter() {
println!("start: {:?}; end: {:?}", period.start, period.end);
}
let new_schedule = db::create_schedule(&post.name, &post.periods);
match new_schedule {
Ok(ok) => HttpResponse::Ok().json(ok),