// 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, 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)) }