/* Social program for Ramaskrik. Copyright (C) 2019 Harald Eilertsen This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ use crate::{ db, models, }; use std::result::Result; use rocket::{delete, get, post}; use rocket::request::{Form, FromForm}; use rocket::response::Redirect; use rocket_contrib::{ json::Json, templates::Template, }; use serde::Serialize; use std::error::Error; #[get("/", format = "application/json")] pub fn get_aggregated_screenings(db: db::Connection) -> Json> { Json(db.get_aggregated_screenings().unwrap()) } #[get("/", rank = 2)] pub fn list_screenings(db: db::Connection) -> Result> { #[derive(Serialize)] struct Context { screenings: Vec, } let ctx = Context { screenings: db.get_aggregated_screenings()? }; Ok(Template::render("screening/list", &ctx)) } #[get("/new")] pub fn new_screening(db: db::Connection) -> Result> { #[derive(Serialize)] struct Context { rooms: Vec, films: Vec, } let ctx = Context { rooms: db.get_rooms()?, films: db.get_films()?, }; Ok(Template::render("screening/new", &ctx)) } #[derive(FromForm)] pub struct NewScreeningForm { film_id: i32, room_id: i32, date: String, start_time: String, end_time: String, } #[post("/", format = "application/x-www-form-urlencoded", data = "")] pub fn create_screening(db: db::Connection, screening: Form) -> Result> { let date = chrono::NaiveDate::parse_from_str(dbg!(&screening.date), "%Y-%m-%d")?; let start_time = chrono::NaiveTime::parse_from_str(dbg!(&screening.start_time), "%H:%M")?; let end_time = chrono::NaiveTime::parse_from_str(dbg!(&screening.end_time), "%H:%M")?; db.create_screening(dbg!(screening.room_id), dbg!(screening.film_id), date, start_time, end_time)?; Ok(Redirect::to("/screenings")) } #[derive(FromForm)] pub struct DeleteScreeningForm { screening_id: i32, } #[delete("/", format = "application/x-www-form-urlencoded", data = "")] pub fn delete(db: db::Connection, screening: Form) -> Result> { db.delete_screening(screening.screening_id)?; Ok(Redirect::to("/screenings")) }