aboutsummaryrefslogblamecommitdiffstats
path: root/src/controllers/screening.rs
blob: 5df8602070027a31c20a5ac42f07196f33c7eac1 (plain) (tree)



















                                                                               
           

  
                        
                                       

                                   
                               

                                   

                      
 
                                        

                                                                                                      
 

                     
                                                                              




                                                     
                                                                                                                     



                                                
                                                                            






                                 

                                                                              








                                               
                 



                       




                                                                                                    
                                                                                


                                                                                                                      



                                                        

                        


                          
                                                                  
 
                                  
 
 
               
                                                                            







                                     


                                                                                        















                                                                                 


                                                                                                                      









                                                        
                                                                  
 
                                  

 





                                                                                  

                                                                                                           
                                  
 
/*
    Social program for Ramaskrik.
    Copyright (C) 2019  Harald Eilertsen <haraldei@anduin.net>

    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 <https://www.gnu.org/licenses/>.
*/

use crate::{
    db,
    models,
};

use std::result::Result;
use rocket::{delete, get, patch, post};
use rocket::form::{Form, FromForm};
use rocket::http::Status;
use rocket::response::Redirect;
use rocket::serde::json::Json;
use rocket_dyn_templates::Template;
use serde::Serialize;
use std::error::Error;

#[get("/", format = "application/json")]
pub async fn get_aggregated_screenings(db: db::Connection) -> Json<Vec<models::AggregatedScreening>> {
    Json(db.get_aggregated_screenings().await.unwrap())
}

#[get("/", rank = 2)]
pub async fn list_screenings(db: db::Connection) -> Result<Template, Status> {
    #[derive(Serialize)]
    struct Context {
        screenings: Vec<models::AggregatedScreening>,
    }

    let ctx = Context { screenings: db.get_aggregated_screenings().await.map_err(|_| Status::InternalServerError)? };
    Ok(Template::render("screening/list", &ctx))
}

#[get("/new")]
pub async fn new_screening(db: db::Connection) -> Result<Template, Status> {
    #[derive(Serialize)]
    struct Context {
        rooms: Vec<models::Room>,
        films: Vec<models::Film>,
    }

    let ctx = Context {
        rooms: db.get_rooms().await.map_err(|_| Status::InternalServerError)?,
        films: db.get_films().await.map_err(|_| Status::InternalServerError)?,
    };

    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,
}

fn parse_datetime(date: &str, time: &str) -> Result<chrono::DateTime<chrono::Utc>, Box<dyn Error>> {
    let dts = format!("{}T{}:00+02:00", &date, &time);
    Ok(chrono::DateTime::parse_from_rfc3339(&dts)?.with_timezone(&chrono::Utc))
}

#[post("/", format = "application/x-www-form-urlencoded", data = "<screening>")]
pub async fn create_screening(db: db::Connection, screening: Form<NewScreeningForm>) -> Result<Redirect, Status> {
    let start_time = parse_datetime(&screening.date, &screening.start_time).map_err(|_| Status::InternalServerError)?;
    let mut end_time = parse_datetime(&screening.date, &screening.end_time).map_err(|_| Status::InternalServerError)?;

    if end_time < start_time {
        end_time = end_time + chrono::Duration::days(1);
    }

    db.create_screening(
        screening.room_id,
        screening.film_id,
        start_time,
        end_time).await.map_err(|_| Status::InternalServerError)?;

    Ok(Redirect::to("screenings"))
}

#[get("/<id>")]
pub async fn edit(db: db::Connection, id: i32) -> Result<Template, Status> {
    #[derive(Serialize)]
    struct Context {
        screening: models::Screening,
        rooms: Vec<models::Room>,
        films: Vec<models::Film>,
    }

    let ctx = Context {
        screening: db.get_screening(id).await.map_err(|_| Status::InternalServerError)?,
        rooms: db.get_rooms().await.map_err(|_| Status::InternalServerError)?,
        films: db.get_films().await.map_err(|_| Status::InternalServerError)?,
    };

    Ok(Template::render("screening/edit", &ctx))
}

#[derive(FromForm)]
pub struct EditScreeningForm {
    id: i32,
    film_id: i32,
    room_id: i32,
    date: String,
    start_time: String,
    end_time: String,
}

#[patch("/", format = "application/x-www-form-urlencoded", data = "<screening>")]
pub async fn update(db: db::Connection, screening: Form<EditScreeningForm>) -> Result<Redirect, Status> {
    let start_time = parse_datetime(&screening.date, &screening.start_time).map_err(|_| Status::InternalServerError)?;
    let mut end_time = parse_datetime(&screening.date, &screening.end_time).map_err(|_| Status::InternalServerError)?;

    if end_time < start_time {
        end_time = end_time + chrono::Duration::days(1);
    }

    db.update_screening(
        screening.id,
        screening.room_id,
        screening.film_id,
        start_time,
        end_time).await.map_err(|_| Status::InternalServerError)?;

    Ok(Redirect::to("screenings"))
}

#[derive(FromForm)]
pub struct DeleteScreeningForm {
    screening_id: i32,
}

#[delete("/", format = "application/x-www-form-urlencoded", data = "<screening>")]
pub async fn delete(db: db::Connection, screening: Form<DeleteScreeningForm>) -> Result<Redirect, Status> {
    db.delete_screening(screening.screening_id).await.map_err(|_| Status::InternalServerError)?;
    Ok(Redirect::to("screenings"))
}