refactor: reorganize project structure and add new endpoints for group and user management
Some checks failed
ci/woodpecker/push/check_fmt Pipeline failed

This commit is contained in:
Mika 2025-04-02 15:27:26 +02:00
parent 693ce67806
commit e2de7467ed
20 changed files with 107 additions and 17 deletions

3
bruno/group/folder.bru Normal file
View file

@ -0,0 +1,3 @@
meta {
name: group
}

View file

@ -1,3 +0,0 @@
meta {
name: groups
}

View file

@ -5,7 +5,7 @@ meta {
}
post {
url: {{api_base}}/projects
url: {{api_base}}/project
body: json
auth: inherit
}
@ -18,4 +18,4 @@ body:json {
vars:pre-request {
name: FillThisOutButDontCommitIt!
}
}

View file

@ -0,0 +1,15 @@
meta {
name: Delete Project
type: http
seq: 2
}
delete {
url: {{api_base}}/project/:id
body: none
auth: inherit
}
params:path {
id: MyAwesomeUUIDHere!
}

3
bruno/project/folder.bru Normal file
View file

@ -0,0 +1,3 @@
meta {
name: project
}

View file

@ -1,3 +0,0 @@
meta {
name: projects
}

3
bruno/user/folder.bru Normal file
View file

@ -0,0 +1,3 @@
meta {
name: user
}

View file

@ -1,3 +0,0 @@
meta {
name: users
}

View file

@ -17,6 +17,7 @@ argon2 = "0.5.3"
thiserror = "2"
env_logger = "0.11"
log = "0.4"
serde = { version = "1", features = ["derive"] }
sea-orm = { version = "1.1", features = [

View file

@ -7,5 +7,9 @@ mod template;
mod user;
pub fn register_controllers(cfg: &mut ServiceConfig) {
cfg.service(web::scope("/projects").configure(project::setup));
cfg.service(web::scope("/project").configure(project::setup))
.service(web::scope("/group").configure(group::setup))
.service(web::scope("/user").configure(user::setup))
.service(web::scope("/class").configure(class::setup))
.service(web::scope("/template").configure(template::setup));
}

View file

@ -1,5 +1,12 @@
use actix_web::{Responder, delete, get, post};
pub fn setup(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(get_classes)
.service(get_class)
.service(create_class)
.service(delete_class);
}
#[get("")]
async fn get_classes() -> impl Responder {
""

View file

@ -1,5 +1,12 @@
use actix_web::{Responder, delete, get, post};
pub fn setup(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(get_groups)
.service(get_groups_for_project)
.service(create_group)
.service(delete_group);
}
#[get("")]
async fn get_groups() -> impl Responder {
""

View file

@ -1,4 +1,6 @@
use actix_web::{Responder, Result, delete, get, post, web};
use log::info;
use sea_orm::prelude::Uuid;
use serde::Deserialize;
use crate::db::Database;
@ -10,7 +12,9 @@ struct CreateProject {
}
pub fn setup(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(create_project);
cfg.service(create_project)
.service(delete_project)
.service(get_projects);
}
#[post("")]
@ -24,8 +28,17 @@ async fn create_project(
}
#[delete("/{id}")]
async fn delete_project() -> impl Responder {
""
async fn delete_project(
db: web::Data<Database>,
path: web::Path<Uuid>,
) -> Result<web::Json<String>, ApiError> {
let id = path.into_inner();
let result = db.delete_project(&id).await?;
Ok(web::Json(format!(
"Successfully deleted {} project/s with the id: {}",
result.rows_affected, id
)))
}
#[get("")]

View file

@ -1,5 +1,12 @@
use actix_web::{Responder, delete, get, post};
pub fn setup(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(get_templates)
.service(get_template)
.service(create_template)
.service(delete_template);
}
#[get("")]
async fn get_templates() -> impl Responder {
""

View file

@ -1,12 +1,19 @@
use actix_web::{Responder, delete, get, post};
pub fn setup(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(get_users)
.service(get_user)
.service(create_user)
.service(delete_user);
}
#[get("")]
async fn get_users() -> impl Responder {
""
}
#[get("/{id}")]
async fn get_class() -> impl Responder {
async fn get_user() -> impl Responder {
""
}

View file

@ -1,6 +1,8 @@
use sea_orm::{ConnectOptions, DatabaseConnection};
mod group;
mod project;
mod user;
#[derive(Clone)]
pub struct Database {

View file

@ -0,0 +1,3 @@
fn add_user_to_group() {}
fn create_group() {}

View file

@ -1,9 +1,11 @@
use super::Database;
use crate::error::ApiError;
use log::info;
use entity::project;
use sea_orm::ActiveModelTrait;
use sea_orm::ActiveValue::{NotSet, Set};
use sea_orm::prelude::Uuid;
use sea_orm::{ActiveModelTrait, DeleteResult, EntityTrait};
impl Database {
pub async fn create_project(&self, name: &str) -> Result<project::Model, ApiError> {
@ -15,4 +17,18 @@ impl Database {
let project = project.insert(&self.conn).await?;
Ok(project)
}
pub async fn delete_project(&self, id: &Uuid) -> Result<DeleteResult, ApiError> {
info!("Deleting project with id: {}", id);
let project = project::Entity::delete_by_id(id.to_owned())
.exec(&self.conn)
.await?;
if project.rows_affected == 0 {
return Err(ApiError::NotFound);
}
Ok(project)
}
}

View file

@ -0,0 +1,5 @@
use super::Database;
impl Database {
async fn create_user() {}
}

View file

@ -5,12 +5,15 @@ use thiserror::Error;
pub enum ApiError {
#[error("Database Error: {0}")]
Database(#[from] sea_orm::DbErr),
#[error("Not Found")]
NotFound,
}
impl ResponseError for ApiError {
fn status_code(&self) -> StatusCode {
match self {
ApiError::Database(..) => StatusCode::INTERNAL_SERVER_ERROR,
ApiError::NotFound => StatusCode::NOT_FOUND,
}
}