backend/controller/
timeline.rs

1//! `Users` endpoints.
2
3use actix_web::{
4    get,
5    web::{Path, Query},
6    HttpResponse, Result,
7};
8
9use crate::service;
10use crate::{config::data::SharedPool, model::dto::timeline::TimelineParameters};
11
12/// Get calculated timeline data for a given map from dates start to end.
13/// The timeline contains all additions and removals of `Plantings` aggregated
14/// by years, months, and also on the actual dates. It only contains years, months
15/// and dates if there is at least one addition or removal.
16///
17/// # Errors
18/// * If the connection to the database could not be established.
19#[utoipa::path(
20    context_path = "/api/maps/{map_id}/timeline",
21    params(
22        TimelineParameters
23    ),
24    responses(
25      (status = 200, description = "Get timeline data from plantings", body = TimelineDto),
26      (status = 404, description = "Map not found", body = TimelineDto),
27      (status = 422, description = "Start is not smaller than end", body = TimelineDto)
28    ),
29    security(
30        ("oauth2" = [])
31    )
32)]
33#[get("timeline")]
34pub async fn get_timeline(
35    map_id: Path<i32>,
36    parameters: Query<TimelineParameters>,
37    pool: SharedPool,
38) -> Result<HttpResponse> {
39    let params = parameters.into_inner();
40    if params.start > params.end {
41        return Ok(HttpResponse::UnprocessableEntity().body("Start must be smaller than end"));
42    }
43    let dto = service::timeline::calculate(map_id.into_inner(), params, &pool).await?;
44    Ok(HttpResponse::Ok().json(dto))
45}