backend/controller/
plants.rs

1//! `Plants` endpoints.
2
3use actix_web::{
4    get,
5    web::{Path, Query},
6    HttpResponse, Result,
7};
8
9use crate::{
10    config::data::SharedPool,
11    model::dto::{PageParameters, PlantsSearchParameters},
12    service::plants,
13};
14
15/// Endpoint for fetching or searching [`PlantsSummaryDto`](crate::model::dto::PlantsSummaryDto).
16/// Search parameters are taken from the URLs query string (e.g. .../`api/plants?name=example&per_page=5`).
17/// If no page parameters are provided, the first page is returned.
18///
19/// # Errors
20/// * If the connection to the database could not be established.
21#[utoipa::path(
22    context_path = "/api/plants",
23    params(
24        PlantsSearchParameters,
25        PageParameters,
26    ),
27    responses(
28        (status = 200, description = "Fetch or search for all plants", body = PagePlantsSummaryDto),
29    ),
30    security(
31        ("oauth2" = [])
32    )
33)]
34#[get("")]
35pub async fn find(
36    search_query: Query<PlantsSearchParameters>,
37    page_query: Query<PageParameters>,
38    pool: SharedPool,
39) -> Result<HttpResponse> {
40    let payload = plants::find(search_query.into_inner(), page_query.into_inner(), &pool).await?;
41    Ok(HttpResponse::Ok().json(payload))
42}
43
44/// Endpoint for fetching a [`Plant`](crate::model::entity::Plants).
45///
46/// # Errors
47/// * If the connection to the database could not be established.
48#[utoipa::path(
49    context_path = "/api/plants",
50    responses(
51        (status = 200, description = "Fetch plant by id", body = PlantsSummaryDto)
52    ),
53    security(
54        ("oauth2" = [])
55    )
56)]
57#[get("/{id}")]
58pub async fn find_by_id(id: Path<i32>, pool: SharedPool) -> Result<HttpResponse> {
59    let response = plants::find_by_id(*id, &pool).await?;
60    Ok(HttpResponse::Ok().json(response))
61}