Implement user retrieval and deletion endpoint #56

Merged
fargan_tamplate merged 1 commit from get_user-enpoint into main 2025-04-07 14:23:55 +02:00
4 changed files with 81 additions and 11 deletions
Showing only changes of commit e2fc4972bc - Show all commits

15
bruno/user/Get user.bru Normal file
View file

@ -0,0 +1,15 @@
meta {
name: Get user
type: http
seq: 2
}
get {
url: {{api_base}}/user/:id
body: none
auth: inherit
}
params:path {
id: 293ef329-91b2-4912-ad5d-0277490c7b55
}

11
bruno/user/Get users.bru Normal file
View file

@ -0,0 +1,11 @@
meta {
name: Get users
type: http
seq: 1
}
get {
url: {{api_base}}/user
body: none
auth: inherit
}

View file

@ -1,5 +1,5 @@
use crate::{entity, error::ApiError, Database}; use crate::{Database, entity, error::ApiError};
use actix_web::{delete, get, post, put, web, Responder}; use actix_web::{Responder, delete, get, post, put, web};
use serde::Deserialize; use serde::Deserialize;
use validator::Validate; use validator::Validate;
@ -20,13 +20,21 @@ struct CreateUser {
} }
#[get("")] #[get("")]
async fn get_users() -> impl Responder { async fn get_users(
"" db: web::Data<Database>,
) -> Result<web::Json<Vec<entity::user::Model>>, ApiError> {
let users = db.get_users().await?;
Ok(web::Json(users))
} }
#[get("/{id}")] #[get("/{id}")]
async fn get_user() -> impl Responder { async fn get_user(
"" db: web::Data<Database>,
id: web::Path<uuid::Uuid>,
) -> Result<web::Json<entity::user::Model>, ApiError> {
let user = db.get_user(id.into_inner()).await?;
Ok(web::Json(user.unwrap()))
} }
#[post("")] #[post("")]
@ -48,6 +56,11 @@ async fn update_user() -> impl Responder {
} }
#[delete("/{id}")] #[delete("/{id}")]
async fn delete_user() -> impl Responder { async fn delete_user(
"" db: web::Data<Database>,
id: web::Path<uuid::Uuid>,
) -> Result<web::Json<String>, ApiError> {
let id = id.into_inner();
db.delete_user(id).await?;
Ok(web::Json(format!("User {} deleted", id)))
} }

View file

@ -1,18 +1,37 @@
use crate::error::ApiError; use crate::error::ApiError;
use argon2::{ use argon2::{
password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
Argon2, PasswordHash, PasswordVerifier, Argon2, PasswordHash, PasswordVerifier,
password_hash::{PasswordHasher, SaltString, rand_core::OsRng},
}; };
use sea_orm::{ use sea_orm::{
ActiveModelTrait, ActiveModelTrait,
ActiveValue::{NotSet, Set}, ActiveValue::{NotSet, Set},
ColumnTrait, DbErr, EntityTrait, ModelTrait, QueryFilter, TransactionTrait, ColumnTrait, DbErr, DeleteResult, EntityTrait, ModelTrait, QueryFilter, TransactionTrait,
}; };
use uuid::Uuid; use uuid::Uuid;
use crate::{entity, Database}; use crate::{Database, entity};
impl Database { impl Database {
pub async fn get_users(&self) -> Result<Vec<entity::user::Model>, ApiError> {
let users = entity::user::Entity::find().all(&self.conn).await?;
Ok(users)
}
pub async fn get_user(&self, id: Uuid) -> Result<Option<entity::user::Model>, ApiError> {
let user = entity::user::Entity::find()
.filter(entity::user::Column::Id.eq(id))
.one(&self.conn)
.await?;
if user.is_none() {
return Err(ApiError::NotFound);
}
Ok(user)
}
pub async fn create_user( pub async fn create_user(
&self, &self,
name: String, name: String,
@ -82,6 +101,18 @@ impl Database {
Ok(user.id) Ok(user.id)
} }
pub async fn delete_user(&self, id: Uuid) -> Result<DeleteResult, ApiError> {
let user = entity::user::Entity::delete_by_id(id)
.exec(&self.conn)
.await?;
if user.rows_affected == 0 {
return Err(ApiError::NotFound);
}
Ok(user)
}
pub async fn verify_ldap_user() {} pub async fn verify_ldap_user() {}
pub async fn change_user_password() {} pub async fn change_user_password() {}