From c91dce4f77ae12453203f0a28b91efb6533cc095 Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Tue, 9 Dec 2025 13:07:14 +0100 Subject: feat(rocie-server): Implement basic user handling and authentication --- crates/rocie-server/src/api/set/auth/barcode.rs | 125 +++++++++++++++++++ crates/rocie-server/src/api/set/auth/mod.rs | 21 ++++ crates/rocie-server/src/api/set/auth/product.rs | 138 +++++++++++++++++++++ .../src/api/set/auth/product_parent.rs | 67 ++++++++++ crates/rocie-server/src/api/set/auth/recipe.rs | 60 +++++++++ crates/rocie-server/src/api/set/auth/unit.rs | 67 ++++++++++ .../rocie-server/src/api/set/auth/unit_property.rs | 57 +++++++++ crates/rocie-server/src/api/set/auth/user.rs | 63 ++++++++++ crates/rocie-server/src/api/set/barcode.rs | 105 ---------------- crates/rocie-server/src/api/set/mod.rs | 21 +--- crates/rocie-server/src/api/set/no_auth/mod.rs | 7 ++ crates/rocie-server/src/api/set/no_auth/user.rs | 131 +++++++++++++++++++ crates/rocie-server/src/api/set/product.rs | 126 ------------------- crates/rocie-server/src/api/set/product_parent.rs | 60 --------- crates/rocie-server/src/api/set/recipe.rs | 54 -------- crates/rocie-server/src/api/set/unit.rs | 60 --------- crates/rocie-server/src/api/set/unit_property.rs | 51 -------- 17 files changed, 738 insertions(+), 475 deletions(-) create mode 100644 crates/rocie-server/src/api/set/auth/barcode.rs create mode 100644 crates/rocie-server/src/api/set/auth/mod.rs create mode 100644 crates/rocie-server/src/api/set/auth/product.rs create mode 100644 crates/rocie-server/src/api/set/auth/product_parent.rs create mode 100644 crates/rocie-server/src/api/set/auth/recipe.rs create mode 100644 crates/rocie-server/src/api/set/auth/unit.rs create mode 100644 crates/rocie-server/src/api/set/auth/unit_property.rs create mode 100644 crates/rocie-server/src/api/set/auth/user.rs delete mode 100644 crates/rocie-server/src/api/set/barcode.rs create mode 100644 crates/rocie-server/src/api/set/no_auth/mod.rs create mode 100644 crates/rocie-server/src/api/set/no_auth/user.rs delete mode 100644 crates/rocie-server/src/api/set/product.rs delete mode 100644 crates/rocie-server/src/api/set/product_parent.rs delete mode 100644 crates/rocie-server/src/api/set/recipe.rs delete mode 100644 crates/rocie-server/src/api/set/unit.rs delete mode 100644 crates/rocie-server/src/api/set/unit_property.rs (limited to 'crates/rocie-server/src/api/set') diff --git a/crates/rocie-server/src/api/set/auth/barcode.rs b/crates/rocie-server/src/api/set/auth/barcode.rs new file mode 100644 index 0000000..1d97852 --- /dev/null +++ b/crates/rocie-server/src/api/set/auth/barcode.rs @@ -0,0 +1,125 @@ +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, Result, post, web}; +use log::debug; + +use crate::{ + app::App, + storage::sql::{ + barcode::{Barcode, BarcodeId, BarcodeIdStub}, + insert::Operations, + unit::UnitAmount, + }, +}; + +/// Buy an barcode +#[utoipa::path( + responses( + ( + status = OK, + description = "Barcode successfully bought", + ), + ( + status = NOT_FOUND, + description = "Barcode id was not found", + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + params( + ( + "barcode_id" = BarcodeId, + description = "The numeric value of the barcode" + ), + ( + "times" = u16, + description = "How often to buy the barcode" + ), + ) +)] +#[post("/barcode/{barcode_id}/buy/{times}")] +pub(crate) async fn buy_barcode( + app: web::Data, + path: web::Path<(BarcodeIdStub, u16)>, + _user: Identity, +) -> Result { + let (barcode_id, times) = path.into_inner(); + + let mut ops = Operations::new("buy barcode unit"); + + let barcode = Barcode::from_id(&app, barcode_id.into()).await?; + + match barcode { + Some(barcode) => { + for _ in 0..times { + barcode.buy(&mut ops); + } + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().finish()) + } + None => Ok(HttpResponse::NotFound().finish()), + } +} + +/// Consume an barcode +#[utoipa::path( + responses( + ( + status = OK, + description = "Barcode successfully consumed", + ), + ( + status = NOT_FOUND, + description = "Barcode id was not found", + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + params( + ( + "id" = BarcodeId, + description = "The numeric value of the barcode" + ), + ), + request_body = UnitAmount, +)] +#[post("/barcode/{id}/consume")] +pub(crate) async fn consume_barcode( + app: web::Data, + barcode_id: web::Path, + unit_amount: web::Json, + _user: Identity, +) -> Result { + let mut ops = Operations::new("consume barcode unit"); + + let barcode = Barcode::from_id(&app, barcode_id.into_inner().into()).await?; + debug!("Starting consume for barcode: {barcode:?}"); + + match barcode { + Some(barcode) => { + barcode + .consume(&app, unit_amount.into_inner(), &mut ops) + .await?; + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().finish()) + } + None => Ok(HttpResponse::NotFound().finish()), + } +} diff --git a/crates/rocie-server/src/api/set/auth/mod.rs b/crates/rocie-server/src/api/set/auth/mod.rs new file mode 100644 index 0000000..4e733a9 --- /dev/null +++ b/crates/rocie-server/src/api/set/auth/mod.rs @@ -0,0 +1,21 @@ +use actix_web::web; + +pub(crate) mod barcode; +pub(crate) mod product; +pub(crate) mod product_parent; +pub(crate) mod recipe; +pub(crate) mod unit; +pub(crate) mod unit_property; +pub(crate) mod user; + +pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) { + cfg.service(product::register_product) + .service(product::associate_barcode) + .service(product_parent::register_product_parent) + .service(recipe::add_recipe) + .service(unit::register_unit) + .service(unit_property::register_unit_property) + .service(barcode::consume_barcode) + .service(barcode::buy_barcode) + .service(user::register_user); +} diff --git a/crates/rocie-server/src/api/set/auth/product.rs b/crates/rocie-server/src/api/set/auth/product.rs new file mode 100644 index 0000000..b2a751f --- /dev/null +++ b/crates/rocie-server/src/api/set/auth/product.rs @@ -0,0 +1,138 @@ +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, Result, post, web}; +use serde::Deserialize; +use utoipa::ToSchema; + +use crate::{ + app::App, + storage::sql::{ + barcode::Barcode, + insert::Operations, + product::{Product, ProductId, ProductIdStub}, + product_parent::ProductParentId, + unit::Unit, + unit_property::UnitPropertyId, + }, +}; + +#[derive(Deserialize, ToSchema)] +struct ProductStub { + /// The name of the product + name: String, + + /// The Unit Property to use for this product. + unit_property: UnitPropertyId, + + /// A description. + #[schema(nullable = false)] + description: Option, + + /// A parent of this product, otherwise the parent will be the root of the parent tree. + #[schema(nullable = false)] + parent: Option, +} + +/// Register a product +#[utoipa::path( + responses( + ( + status = 200, + description = "Product successfully registered in database", + body = ProductId, + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + request_body = ProductStub, +)] +#[post("/product/new")] +pub(crate) async fn register_product( + app: web::Data, + product_stub: web::Json, + _user: Identity, +) -> Result { + let product_stub = product_stub.into_inner(); + let mut ops = Operations::new("register product"); + + let product = Product::register( + product_stub.name, + product_stub.description, + product_stub.parent, + product_stub.unit_property, + &mut ops, + ); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().json(product.id)) +} + +/// Associate a barcode with a product +#[utoipa::path( + responses( + ( + status = OK, + description = "Barcode successfully associated with product", + ), + ( + status = NOT_FOUND, + description = "Product id not found in database", + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = BAD_REQUEST, + description = "Unit used in request has not been registered yet", + body = String, + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + params ( + ( + "id" = ProductId, + description = "The id of the product to associated the barcode with" + ), + ), + request_body = Barcode, +)] +#[post("/product/{id}/associate")] +pub(crate) async fn associate_barcode( + app: web::Data, + id: web::Path, + barcode: web::Json, + _user: Identity, +) -> Result { + let mut ops = Operations::new("associated barcode with product"); + + { + let units = Unit::get_all(&app).await?; + if !units.into_iter().any(|unit| unit.id == barcode.amount.unit) { + return Ok(HttpResponse::BadRequest() + .body("The used unit has not been registered; it cannot be used.\n")); + } + } + + match Product::from_id(&app, id.into_inner().into()).await? { + Some(product) => { + product.associate_barcode(barcode.into_inner(), &mut ops); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().finish()) + } + None => Ok(HttpResponse::NotFound().finish()), + } +} diff --git a/crates/rocie-server/src/api/set/auth/product_parent.rs b/crates/rocie-server/src/api/set/auth/product_parent.rs new file mode 100644 index 0000000..416875b --- /dev/null +++ b/crates/rocie-server/src/api/set/auth/product_parent.rs @@ -0,0 +1,67 @@ +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, Result, post, web}; +use serde::Deserialize; +use utoipa::ToSchema; + +use crate::{ + app::App, + storage::sql::{ + insert::Operations, + product_parent::{ProductParent, ProductParentId}, + }, +}; + +#[derive(Deserialize, ToSchema)] +struct ProductParentStub { + /// The name of the product parent + name: String, + + /// A description. + #[schema(nullable = false)] + description: Option, + + /// A parent of this product parent, otherwise the parent will be the root of the parent tree. + #[schema(nullable = false)] + parent: Option, +} + +/// Register a product parent +#[utoipa::path( + responses( + ( + status = OK, + description = "Product parent successfully registered in database", + body = ProductParentId, + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + request_body = ProductParentStub, +)] +#[post("/product_parent/new")] +pub(crate) async fn register_product_parent( + app: web::Data, + product_stub: web::Json, + _user: Identity, +) -> Result { + let product_stub = product_stub.into_inner(); + let mut ops = Operations::new("register product parent"); + + let product = ProductParent::register( + product_stub.name, + product_stub.description, + product_stub.parent, + &mut ops, + ); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().json(product.id)) +} diff --git a/crates/rocie-server/src/api/set/auth/recipe.rs b/crates/rocie-server/src/api/set/auth/recipe.rs new file mode 100644 index 0000000..43a034e --- /dev/null +++ b/crates/rocie-server/src/api/set/auth/recipe.rs @@ -0,0 +1,60 @@ +use std::path::PathBuf; + +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, error::Result, post, web}; +use serde::Deserialize; +use utoipa::ToSchema; + +use crate::{ + app::App, + storage::sql::{ + insert::Operations, + recipe::{Recipe, RecipeId}, + }, +}; + +#[derive(Deserialize, ToSchema)] +struct RecipeStub { + /// The path the recipe should have + #[schema(value_type = String)] + path: PathBuf, + + /// The content of this recipe, in cooklang format + content: String, +} + +/// Register a product parent +#[utoipa::path( + responses( + ( + status = OK, + description = "Product parent successfully registered in database", + body = RecipeId, + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + request_body = RecipeStub, +)] +#[post("/recipe/new")] +pub(crate) async fn add_recipe( + app: web::Data, + stub: web::Json, + _user: Identity, +) -> Result { + let stub = stub.into_inner(); + let mut ops = Operations::new("add recipe parent"); + + let recipe = Recipe::new(stub.path, stub.content, &mut ops); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().json(recipe.id)) +} diff --git a/crates/rocie-server/src/api/set/auth/unit.rs b/crates/rocie-server/src/api/set/auth/unit.rs new file mode 100644 index 0000000..21d1e11 --- /dev/null +++ b/crates/rocie-server/src/api/set/auth/unit.rs @@ -0,0 +1,67 @@ +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, Result, post, web}; +use serde::Deserialize; +use utoipa::ToSchema; + +use crate::{ + app::App, + storage::sql::{ + insert::Operations, + unit::{Unit, UnitId}, + unit_property::UnitPropertyId, + }, +}; + +#[derive(Deserialize, ToSchema)] +struct UnitStub { + full_name_plural: String, + full_name_singular: String, + short_name: String, + unit_property: UnitPropertyId, + + #[schema(nullable = false)] + description: Option, +} + +/// Register an Unit +#[utoipa::path( + responses( + ( + status = OK, + description = "Unit successfully registered in database", + body = UnitId, + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + request_body = UnitStub, +)] +#[post("/unit/new")] +pub(crate) async fn register_unit( + app: web::Data, + unit: web::Json, + _user: Identity, +) -> Result { + let unit = unit.into_inner(); + let mut ops = Operations::new("register unit"); + + let unit = Unit::register( + unit.full_name_singular, + unit.full_name_plural, + unit.short_name, + unit.description, + unit.unit_property, + &mut ops, + ); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().json(unit.id)) +} diff --git a/crates/rocie-server/src/api/set/auth/unit_property.rs b/crates/rocie-server/src/api/set/auth/unit_property.rs new file mode 100644 index 0000000..2958e1f --- /dev/null +++ b/crates/rocie-server/src/api/set/auth/unit_property.rs @@ -0,0 +1,57 @@ +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, Result, post, web}; +use serde::Deserialize; +use utoipa::ToSchema; + +use crate::{ + app::App, + storage::sql::{ + insert::Operations, + unit_property::{UnitProperty, UnitPropertyId}, + }, +}; + +#[derive(Deserialize, ToSchema)] +struct UnitPropertyStub { + /// The name of the unit property. + name: String, + + /// An optional description of the unit property. + #[schema(nullable = false)] + description: Option, +} + +/// Register an Unit Property +#[utoipa::path( + responses( + ( + status = OK, + description = "Unit property successfully registered in database", + body = UnitPropertyId, + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + request_body = UnitPropertyStub, +)] +#[post("/unit-property/new")] +pub(crate) async fn register_unit_property( + app: web::Data, + unit: web::Json, + _user: Identity, +) -> Result { + let mut ops = Operations::new("register unit property"); + + let unit = UnitProperty::register(unit.name.clone(), unit.description.clone(), &mut ops); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().json(unit.id)) +} diff --git a/crates/rocie-server/src/api/set/auth/user.rs b/crates/rocie-server/src/api/set/auth/user.rs new file mode 100644 index 0000000..1f262d5 --- /dev/null +++ b/crates/rocie-server/src/api/set/auth/user.rs @@ -0,0 +1,63 @@ +use actix_identity::Identity; +use actix_web::{HttpResponse, Responder, Result, post, web}; +use serde::Deserialize; +use utoipa::ToSchema; + +use crate::{ + app::App, + storage::sql::{ + insert::Operations, + user::{PasswordHash, User, UserId}, + }, +}; + +#[derive(Deserialize, ToSchema)] +pub(crate) struct UserStub { + /// The name of the new user. + pub(crate) name: String, + + /// The password of the new user. + pub(crate) password: String, + + /// An optional description of the new user. + #[schema(nullable = false)] + pub(crate) description: Option, +} + +/// Register an new User +#[utoipa::path( + responses( + ( + status = OK, + description = "User successfully registered in database", + body = UserId, + ), + ( + status = UNAUTHORIZED, + description = "You did not login before calling this endpoint", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String, + ) + ), + request_body = UserStub, +)] +#[post("/user/new")] +pub(crate) async fn register_user( + app: web::Data, + new_user: web::Json, + _user: Identity, +) -> Result { + let user = new_user.into_inner(); + + let mut ops = Operations::new("register user"); + + let password_hash = PasswordHash::from_password(&user.password); + let user = User::register(user.name, password_hash, user.description, &mut ops); + + ops.apply(&app).await?; + + Ok(HttpResponse::Ok().json(user.id)) +} diff --git a/crates/rocie-server/src/api/set/barcode.rs b/crates/rocie-server/src/api/set/barcode.rs deleted file mode 100644 index bb84bbf..0000000 --- a/crates/rocie-server/src/api/set/barcode.rs +++ /dev/null @@ -1,105 +0,0 @@ -use actix_web::{HttpResponse, Responder, Result, post, web}; -use log::debug; - -use crate::{ - app::App, - storage::sql::{ - barcode::{Barcode, BarcodeId, BarcodeIdStub}, - insert::Operations, - unit::UnitAmount, - }, -}; - -/// Buy an barcode -#[utoipa::path( - responses( - ( - status = OK, - description = "Barcode successfully bought", - ), - ( - status = NOT_FOUND, - description = "Barcode id was not found", - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String, - ) - ), - params( - ("barcode_id" = BarcodeId, description = "The numeric value of the barcode"), - ("times" = u16, description = "How often to buy the barcode"), - ) -)] -#[post("/barcode/{barcode_id}/buy/{times}")] -pub(crate) async fn buy_barcode( - app: web::Data, - path: web::Path<(BarcodeIdStub, u16)>, -) -> Result { - let (barcode_id, times) = path.into_inner(); - - let mut ops = Operations::new("buy barcode unit"); - - let barcode = Barcode::from_id(&app, barcode_id.into()).await?; - - match barcode { - Some(barcode) => { - for _ in 0..times { - barcode.buy(&mut ops); - } - - ops.apply(&app).await?; - - Ok(HttpResponse::Ok().finish()) - } - None => Ok(HttpResponse::NotFound().finish()), - } -} - -/// Consume an barcode -#[utoipa::path( - responses( - ( - status = OK, - description = "Barcode successfully consumed", - ), - ( - status = NOT_FOUND, - description = "Barcode id was not found", - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String, - ) - ), - params( - ("id" = BarcodeId, description = "The numeric value of the barcode"), - ), - request_body = UnitAmount, -)] -#[post("/barcode/{id}/consume")] -pub(crate) async fn consume_barcode( - app: web::Data, - barcode_id: web::Path, - unit_amount: web::Json, -) -> Result { - let mut ops = Operations::new("consume barcode unit"); - - let barcode = Barcode::from_id(&app, barcode_id.into_inner().into()).await?; - debug!("Starting consume for barcode: {barcode:?}"); - - match barcode { - Some(barcode) => { - barcode - .consume(&app, unit_amount.into_inner(), &mut ops) - .await?; - - ops.apply(&app).await?; - - Ok(HttpResponse::Ok().finish()) - } - None => Ok(HttpResponse::NotFound().finish()), - } -} diff --git a/crates/rocie-server/src/api/set/mod.rs b/crates/rocie-server/src/api/set/mod.rs index a6037b9..c6ee9ab 100644 --- a/crates/rocie-server/src/api/set/mod.rs +++ b/crates/rocie-server/src/api/set/mod.rs @@ -1,19 +1,2 @@ -use actix_web::web; - -pub(crate) mod barcode; -pub(crate) mod product; -pub(crate) mod product_parent; -pub(crate) mod recipe; -pub(crate) mod unit; -pub(crate) mod unit_property; - -pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) { - cfg.service(product::register_product) - .service(product::associate_barcode) - .service(product_parent::register_product_parent) - .service(recipe::add_recipe) - .service(unit::register_unit) - .service(unit_property::register_unit_property) - .service(barcode::consume_barcode) - .service(barcode::buy_barcode); -} +pub(crate) mod auth; +pub(crate) mod no_auth; diff --git a/crates/rocie-server/src/api/set/no_auth/mod.rs b/crates/rocie-server/src/api/set/no_auth/mod.rs new file mode 100644 index 0000000..27783fc --- /dev/null +++ b/crates/rocie-server/src/api/set/no_auth/mod.rs @@ -0,0 +1,7 @@ +use actix_web::web; + +pub(crate) mod user; + +pub(crate) fn register_paths(cfg: &mut web::ServiceConfig) { + cfg.service(user::login).service(user::logout).service(user::provision); +} diff --git a/crates/rocie-server/src/api/set/no_auth/user.rs b/crates/rocie-server/src/api/set/no_auth/user.rs new file mode 100644 index 0000000..7acb482 --- /dev/null +++ b/crates/rocie-server/src/api/set/no_auth/user.rs @@ -0,0 +1,131 @@ +use actix_identity::Identity; +use actix_web::{HttpMessage, HttpRequest, HttpResponse, Responder, Result, post, web}; +use serde::{Deserialize, Serialize}; +use utoipa::ToSchema; + +use crate::{ + api::set::auth::user::UserStub, + app::App, + storage::sql::{ + insert::Operations, + user::{PasswordHash, User, UserId}, + }, +}; + +#[derive(ToSchema, Deserialize, Serialize)] +struct LoginInfo { + /// The id of the user. + id: UserId, + + /// The password of the user. + password: String, +} + +/// Log in as a specific user +#[utoipa::path( + responses( + ( + status = OK, + description = "User logged in", + ), + ( + status = NOT_FOUND, + description = "User id not found" + ), + ( + status = FORBIDDEN, + description = "Password did not match" + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), + request_body = LoginInfo, +)] +#[post("/login")] +async fn login( + request: HttpRequest, + app: web::Data, + info: web::Json, +) -> Result { + let info = info.into_inner(); + + if let Some(user) = User::from_id(&app, info.id).await? { + if user.password_hash.verify(&info.password) { + Identity::login(&request.extensions(), info.id.to_string())?; + Ok(HttpResponse::Ok().finish()) + } else { + Ok(HttpResponse::Forbidden().finish()) + } + } else { + Ok(HttpResponse::NotFound().finish()) + } +} + +/// Log the current user out +#[utoipa::path( + responses( + ( + status = OK, + description = "User logged out", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), +)] +#[post("/logout")] +async fn logout(user: Identity) -> impl Responder { + user.logout(); + HttpResponse::Ok() +} + +/// Provision this instance. +/// +/// This only works, if no users exist yet. +#[utoipa::path( + responses( + ( + status = OK, + description = "User created and logged in", + body = UserId, + ), + ( + status = FORBIDDEN, + description = "Instance already provisioned", + ), + ( + status = INTERNAL_SERVER_ERROR, + description = "Server encountered error", + body = String + ) + ), + request_body = UserStub, +)] +#[post("/provision")] +async fn provision( + request: HttpRequest, + app: web::Data, + new_user: web::Json, +) -> Result { + if User::get_all(&app).await?.is_empty() { + let user = new_user.into_inner(); + + let mut ops = Operations::new("register user (during provisioning)"); + + let password_hash = PasswordHash::from_password(&user.password); + let user = User::register(user.name, password_hash, user.description, &mut ops); + + ops.apply(&app).await?; + + Identity::login(&request.extensions(), user.id.to_string())?; + + Ok(HttpResponse::Ok().json(user.id)) + } else { + Ok(HttpResponse::Forbidden().finish()) + } +} diff --git a/crates/rocie-server/src/api/set/product.rs b/crates/rocie-server/src/api/set/product.rs deleted file mode 100644 index 74a92d2..0000000 --- a/crates/rocie-server/src/api/set/product.rs +++ /dev/null @@ -1,126 +0,0 @@ -use actix_web::{HttpResponse, Responder, Result, post, web}; -use serde::Deserialize; -use utoipa::ToSchema; - -use crate::{ - app::App, - storage::sql::{ - barcode::Barcode, - insert::Operations, - product::{Product, ProductId, ProductIdStub}, - product_parent::ProductParentId, - unit::Unit, - unit_property::UnitPropertyId, - }, -}; - -#[derive(Deserialize, ToSchema)] -struct ProductStub { - /// The name of the product - name: String, - - /// The Unit Property to use for this product. - unit_property: UnitPropertyId, - - /// A description. - #[schema(nullable = false)] - description: Option, - - /// A parent of this product, otherwise the parent will be the root of the parent tree. - #[schema(nullable = false)] - parent: Option, -} - -/// Register a product -#[utoipa::path( - responses( - ( - status = 200, - description = "Product successfully registered in database", - body = ProductId, - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String, - ) - ), - request_body = ProductStub, -)] -#[post("/product/new")] -pub(crate) async fn register_product( - app: web::Data, - product_stub: web::Json, -) -> Result { - let mut ops = Operations::new("register product"); - - let product = Product::register( - product_stub.name.clone(), - product_stub.description.clone(), - product_stub.parent.into(), - product_stub.unit_property, - &mut ops, - ); - - ops.apply(&app).await?; - - Ok(HttpResponse::Ok().json(product.id)) -} - -/// Associate a barcode with a product -#[utoipa::path( - responses( - ( - status = OK, - description = "Barcode successfully associated with product", - ), - ( - status = NOT_FOUND, - description = "Product id not found in database", - ), - ( - status = FORBIDDEN, - description = "Unit used in request has not been registered yet", - body = String, - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String, - ) - ), - params ( - ( - "id" = ProductId, - description = "The id of the product to associated the barcode with" - ), - ), - request_body = Barcode, -)] -#[post("/product/{id}/associate")] -pub(crate) async fn associate_barcode( - app: web::Data, - id: web::Path, - barcode: web::Json, -) -> Result { - let mut ops = Operations::new("associated barcode with product"); - - { - let units = Unit::get_all(&app).await?; - if !units.into_iter().any(|unit| unit.id == barcode.amount.unit) { - return Ok(HttpResponse::Forbidden() - .body("The used unit has not been registered; it cannot be used.\n")); - } - } - - match Product::from_id(&app, id.into_inner().into()).await? { - Some(product) => { - product.associate_barcode(barcode.into_inner(), &mut ops); - - ops.apply(&app).await?; - - Ok(HttpResponse::Ok().finish()) - } - None => Ok(HttpResponse::NotFound().finish()), - } -} diff --git a/crates/rocie-server/src/api/set/product_parent.rs b/crates/rocie-server/src/api/set/product_parent.rs deleted file mode 100644 index f917207..0000000 --- a/crates/rocie-server/src/api/set/product_parent.rs +++ /dev/null @@ -1,60 +0,0 @@ -use actix_web::{HttpResponse, Responder, Result, post, web}; -use serde::Deserialize; -use utoipa::ToSchema; - -use crate::{ - app::App, - storage::sql::{ - insert::Operations, - product_parent::{ProductParent, ProductParentId}, - }, -}; - -#[derive(Deserialize, ToSchema)] -struct ProductParentStub { - /// The name of the product parent - name: String, - - /// A description. - #[schema(nullable = false)] - description: Option, - - /// A parent of this product parent, otherwise the parent will be the root of the parent tree. - #[schema(nullable = false)] - parent: Option, -} - -/// Register a product parent -#[utoipa::path( - responses( - ( - status = 200, - description = "Product parent successfully registered in database", - body = ProductParentId, - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String, - ) - ), - request_body = ProductParentStub, -)] -#[post("/product_parent/new")] -pub(crate) async fn register_product_parent( - app: web::Data, - product_stub: web::Json, -) -> Result { - let mut ops = Operations::new("register product parent"); - - let product = ProductParent::register( - product_stub.name.clone(), - product_stub.description.clone(), - product_stub.parent.into(), - &mut ops, - ); - - ops.apply(&app).await?; - - Ok(HttpResponse::Ok().json(product.id)) -} diff --git a/crates/rocie-server/src/api/set/recipe.rs b/crates/rocie-server/src/api/set/recipe.rs deleted file mode 100644 index bb5be37..0000000 --- a/crates/rocie-server/src/api/set/recipe.rs +++ /dev/null @@ -1,54 +0,0 @@ -use std::path::PathBuf; - -use actix_web::{HttpResponse, Responder, error::Result, post, web}; -use serde::Deserialize; -use utoipa::ToSchema; - -use crate::{ - app::App, - storage::sql::{ - insert::Operations, - recipe::{Recipe, RecipeId}, - }, -}; - -#[derive(Deserialize, ToSchema)] -struct RecipeStub { - /// The path the recipe should have - #[schema(value_type = String)] - path: PathBuf, - - /// The content of this recipe, in cooklang format - content: String, -} - -/// Register a product parent -#[utoipa::path( - responses( - ( - status = 200, - description = "Product parent successfully registered in database", - body = RecipeId, - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String, - ) - ), - request_body = RecipeStub, -)] -#[post("/recipe/new")] -pub(crate) async fn add_recipe( - app: web::Data, - stub: web::Json, -) -> Result { - let stub = stub.into_inner(); - let mut ops = Operations::new("add recipe parent"); - - let recipe = Recipe::new(stub.path, stub.content, &mut ops); - - ops.apply(&app).await?; - - Ok(HttpResponse::Ok().json(recipe.id)) -} diff --git a/crates/rocie-server/src/api/set/unit.rs b/crates/rocie-server/src/api/set/unit.rs deleted file mode 100644 index 1671918..0000000 --- a/crates/rocie-server/src/api/set/unit.rs +++ /dev/null @@ -1,60 +0,0 @@ -use actix_web::{HttpResponse, Responder, Result, post, web}; -use serde::Deserialize; -use utoipa::ToSchema; - -use crate::{ - app::App, - storage::sql::{ - insert::Operations, - unit::{Unit, UnitId}, - unit_property::UnitPropertyId, - }, -}; - -#[derive(Deserialize, ToSchema)] -struct UnitStub { - full_name_plural: String, - full_name_singular: String, - short_name: String, - unit_property: UnitPropertyId, - - #[schema(nullable = false)] - description: Option, -} - -/// Register an Unit -#[utoipa::path( - responses( - ( - status = 200, - description = "Unit successfully registered in database", - body = UnitId, - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String, - ) - ), - request_body = UnitStub, -)] -#[post("/unit/new")] -pub(crate) async fn register_unit( - app: web::Data, - unit: web::Json, -) -> Result { - let mut ops = Operations::new("register unit"); - - let unit = Unit::register( - unit.full_name_singular.clone(), - unit.full_name_plural.clone(), - unit.short_name.clone(), - unit.description.clone(), - unit.unit_property, - &mut ops, - ); - - ops.apply(&app).await?; - - Ok(HttpResponse::Ok().json(unit.id)) -} diff --git a/crates/rocie-server/src/api/set/unit_property.rs b/crates/rocie-server/src/api/set/unit_property.rs deleted file mode 100644 index ca2960f..0000000 --- a/crates/rocie-server/src/api/set/unit_property.rs +++ /dev/null @@ -1,51 +0,0 @@ -use actix_web::{HttpResponse, Responder, Result, post, web}; -use serde::Deserialize; -use utoipa::ToSchema; - -use crate::{ - app::App, - storage::sql::{ - insert::Operations, - unit_property::{UnitProperty, UnitPropertyId}, - }, -}; - -#[derive(Deserialize, ToSchema)] -struct UnitPropertyStub { - /// The name of the unit property. - name: String, - - /// An optional description of the unit property. - #[schema(nullable = false)] - description: Option, -} - -/// Register an Unit Property -#[utoipa::path( - responses( - ( - status = 200, - description = "Unit property successfully registered in database", - body = UnitPropertyId, - ), - ( - status = INTERNAL_SERVER_ERROR, - description = "Server encountered error", - body = String, - ) - ), - request_body = UnitPropertyStub, -)] -#[post("/unit-property/new")] -pub(crate) async fn register_unit_property( - app: web::Data, - unit: web::Json, -) -> Result { - let mut ops = Operations::new("register unit property"); - - let unit = UnitProperty::register(unit.name.clone(), unit.description.clone(), &mut ops); - - ops.apply(&app).await?; - - Ok(HttpResponse::Ok().json(unit.id)) -} -- cgit 1.4.1