// rocie - An enterprise grocery management system // // Copyright (C) 2026 Benedikt Peetz // SPDX-License-Identifier: GPL-3.0-or-later // // This file is part of Rocie. // // You should have received a copy of the License along with this program. // If not, see . use actix_identity::Identity; use actix_web::{HttpResponse, Responder, error::Result, get, web}; use crate::{ app::App, storage::sql::product_parent::{ProductParent, ProductParentId, ProductParentIdStub}, }; /// Return all registered product parents #[utoipa::path( responses( ( status = OK, description = "All parents found", body = Vec ), ( status = UNAUTHORIZED, description = "You did not login before calling this endpoint", ), ( status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String ) ), )] #[get("/product_parents/")] pub(crate) async fn product_parents( app: web::Data, _user: Identity, ) -> Result { let all: Vec = ProductParent::get_all(&app).await?; Ok(HttpResponse::Ok().json(all)) } /// Return all registered product parents, that have no parents themselves #[utoipa::path( responses( ( status = OK, description = "All parents found", body = Vec ), ( status = UNAUTHORIZED, description = "You did not login before calling this endpoint", ), ( status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String ) ), )] #[get("/product_parents_toplevel/")] pub(crate) async fn product_parents_toplevel( app: web::Data, _user: Identity, ) -> Result { let all: Vec = ProductParent::get_all(&app) .await? .into_iter() .filter(|parent| parent.parent.is_none()) .collect(); Ok(HttpResponse::Ok().json(all)) } /// Return all parents, that have this parent as parent #[utoipa::path( responses( ( status = OK, description = "All parents found", body = Vec ), ( status = UNAUTHORIZED, description = "You did not login before calling this endpoint", ), ( status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String ) ), params( ( "id" = ProductParentId, description = "Product parent id" ), ), )] #[get("/product_parents_under/{id}")] pub(crate) async fn product_parents_under( app: web::Data, id: web::Path, _user: Identity, ) -> Result { let id = id.into_inner().into(); let all: Vec<_> = ProductParent::get_all(&app) .await? .into_iter() .filter(|parent| parent.parent.is_some_and(|found| found == id)) .collect(); Ok(HttpResponse::Ok().json(all)) }