diff --git a/Cargo.lock b/Cargo.lock index 37f4262..e8a9666 100644 Binary files a/Cargo.lock and b/Cargo.lock differ diff --git a/Cargo.toml b/Cargo.toml index bb6f53a..21bb0d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ chrono = { version = "0.4", features = ["serde"] } diesel = { version = "1.4", features = ["sqlite", "uuid"] } diesel_migrations = "1.4" dotenv = "0.15" +env_logger = "0.9.0" serde = "1.0" serde_json = "1.0" serde_derive = "1.0" diff --git a/migrations/2021-10-13-000000_init/up.sql b/migrations/2021-10-13-000000_init/up.sql index 6ac0492..acee951 100644 --- a/migrations/2021-10-13-000000_init/up.sql +++ b/migrations/2021-10-13-000000_init/up.sql @@ -44,10 +44,8 @@ CREATE TABLE schedules periods BLOB NOT NULL ); ---INSERT INTO schedules (uid, name, periods) VALUES (x'6f666600000000000000000000000000', 'off', x'00'); ---INSERT INTO schedules (uid, name, periods) VALUES (x'6f6e0000000000000000000000000000', 'on', x'010000009F05'); INSERT INTO schedules (uid, name, periods) VALUES (x'00', 'off', x''); -INSERT INTO schedules (uid, name, periods) VALUES (x'01', 'on', x'0000173B'); +INSERT INTO schedules (uid, name, periods) VALUES (x'01', 'on', x'00000000'); CREATE TABLE tags ( diff --git a/src/db/models.rs b/src/db/models.rs index 71d6afa..9374553 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -15,7 +15,7 @@ use crate::types::EmgauwaUid; pub struct Schedule { #[serde(skip)] pub id: i32, - #[serde(alias = "id")] + #[serde(rename(serialize = "id"))] pub uid: EmgauwaUid, pub name: String, pub periods: Periods, diff --git a/src/handlers/v1/schedules.rs b/src/handlers/v1/schedules.rs index 06fcb9f..9a3289d 100644 --- a/src/handlers/v1/schedules.rs +++ b/src/handlers/v1/schedules.rs @@ -45,17 +45,10 @@ pub async fn show(web::Path((schedule_uid,)): web::Path<(String,)>) -> impl Resp } pub async fn add(post: web::Json) -> 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), + Ok(ok) => HttpResponse::Created().json(ok), Err(err) => HttpResponse::InternalServerError().json(err), } } diff --git a/src/main.rs b/src/main.rs index 65f8cb0..d5e5575 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,9 @@ extern crate diesel; extern crate diesel_migrations; extern crate dotenv; -use actix_web::{App, HttpServer, web}; +use actix_web::{middleware, web, App, HttpServer}; +use actix_web::middleware::normalize::TrailingSlash; +use env_logger::{Builder, Env}; mod db; mod handlers; @@ -14,8 +16,17 @@ mod types; async fn main() -> std::io::Result<()> { db::run_migrations(); + Builder::from_env(Env::default().default_filter_or("info")).init(); + HttpServer::new(|| { App::new() + .wrap(middleware::DefaultHeaders::new() + .header("Access-Control-Allow-Origin", "*") + .header("Access-Control-Allow-Headers", "*") + .header("Access-Control-Allow-Methods", "*") + ) + .wrap(middleware::Logger::default()) + .wrap(middleware::NormalizePath::new(TrailingSlash::Trim)) .route( "/api/v1/schedules", web::get().to(handlers::v1::schedules::index),