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
//! `Users` endpoints.

use actix_web::{
    get,
    web::{Path, Query},
    HttpResponse, Result,
};

use crate::service;
use crate::{config::data::SharedPool, model::dto::timeline::TimelineParameters};

/// Get calculated timeline data for a given map from dates start to end.
/// The timeline contains all additions and removals of `Plantings` aggregated
/// by years, months, and also on the actual dates. It only contains years, months
/// and dates if there is at least one addition or removal.
///
/// # Errors
/// * If the connection to the database could not be established.
#[utoipa::path(
    context_path = "/api/maps/{map_id}/timeline",
    params(
        TimelineParameters
    ),
    responses(
      (status = 200, description = "Get timeline data from plantings", body = TimelineDto),
      (status = 404, description = "Map not found", body = TimelineDto),
      (status = 422, description = "Start is not smaller than end", body = TimelineDto)
    ),
    security(
        ("oauth2" = [])
    )
)]
#[get("timeline")]
pub async fn get_timeline(
    map_id: Path<i32>,
    parameters: Query<TimelineParameters>,
    pool: SharedPool,
) -> Result<HttpResponse> {
    let params = parameters.into_inner();
    if params.start > params.end {
        return Ok(HttpResponse::UnprocessableEntity().body("Start must be smaller than end"));
    }
    let dto = service::timeline::calculate(map_id.into_inner(), params, &pool).await?;
    Ok(HttpResponse::Ok().json(dto))
}