aboutsummaryrefslogtreecommitdiffstats
path: root/src/controllers/screening.rs
blob: 40eefd377d5610484f7da142ad4a8d00c5f78ddb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
    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, 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<Vec<models::AggregatedScreening>> {
    Json(db.get_aggregated_screenings().unwrap())
}

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

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

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

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

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 fn create_screening(db: db::Connection, screening: Form<NewScreeningForm>) -> Result<Redirect, Box<dyn Error>> {
    let start_time = parse_datetime(&screening.date, &screening.start_time)?;
    let mut end_time = parse_datetime(&screening.date, &screening.end_time)?;

    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)?;

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

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

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