From 1ad0e4400a7a063bfdc899c0e06acf097e032e07 Mon Sep 17 00:00:00 2001 From: Mika Date: Wed, 2 Apr 2025 21:22:17 +0200 Subject: [PATCH 1/3] WIP --- crates/backend/src/controller/class.rs | 7 ++++++- crates/backend/src/controller/group.rs | 7 ++++++- crates/backend/src/controller/project.rs | 21 +++++++++++++++++++-- crates/backend/src/controller/template.rs | 7 ++++++- crates/backend/src/controller/user.rs | 7 ++++++- crates/backend/src/db/group.rs | 8 ++++++-- crates/backend/src/db/project.rs | 13 +++++++++++++ 7 files changed, 62 insertions(+), 8 deletions(-) diff --git a/crates/backend/src/controller/class.rs b/crates/backend/src/controller/class.rs index e3e3498..d6d11dd 100644 --- a/crates/backend/src/controller/class.rs +++ b/crates/backend/src/controller/class.rs @@ -1,4 +1,4 @@ -use actix_web::{Responder, delete, get, post}; +use actix_web::{Responder, delete, get, post, put}; pub fn setup(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(get_classes) @@ -22,6 +22,11 @@ async fn create_class() -> impl Responder { "" } +#[put("")] +async fn change_class() -> impl Responder { + "" +} + #[delete("/{id}")] async fn delete_class() -> impl Responder { "" diff --git a/crates/backend/src/controller/group.rs b/crates/backend/src/controller/group.rs index 648788e..11e7da3 100644 --- a/crates/backend/src/controller/group.rs +++ b/crates/backend/src/controller/group.rs @@ -1,4 +1,4 @@ -use actix_web::{Responder, delete, get, post}; +use actix_web::{Responder, delete, get, post, put}; pub fn setup(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(get_groups) @@ -22,6 +22,11 @@ async fn create_group() -> impl Responder { "" } +#[put("")] +async fn change_group() -> impl Responder { + "" +} + #[delete("/{id}")] async fn delete_group() -> impl Responder { "" diff --git a/crates/backend/src/controller/project.rs b/crates/backend/src/controller/project.rs index c4c8ccf..28f7ad0 100644 --- a/crates/backend/src/controller/project.rs +++ b/crates/backend/src/controller/project.rs @@ -1,5 +1,4 @@ -use actix_web::{Result, delete, get, post, web}; -use log::debug; +use actix_web::{Responder, Result, delete, get, post, put, web}; use sea_orm::prelude::Uuid; use serde::Deserialize; @@ -11,6 +10,12 @@ struct CreateProject { name: String, } +#[derive(Deserialize)] +struct UpdateProject { + id: Uuid, + name: String, +} + pub fn setup(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(get_project) .service(get_projects) @@ -49,6 +54,18 @@ async fn create_project( Ok(web::Json(result)) } +#[put("")] +async fn change_project( + db: web::Data, + update_project_struct: web::Json, +) -> Result, ApiError> { + let updated_project = db + .update_project(update_project_struct.into_inner()) + .await?; + + Ok(web::Json(updated_project)) +} + #[delete("/{id}")] async fn delete_project( db: web::Data, diff --git a/crates/backend/src/controller/template.rs b/crates/backend/src/controller/template.rs index fa62dab..aeaaaf2 100644 --- a/crates/backend/src/controller/template.rs +++ b/crates/backend/src/controller/template.rs @@ -1,4 +1,4 @@ -use actix_web::{Responder, delete, get, post}; +use actix_web::{Responder, delete, get, post, put}; pub fn setup(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(get_templates) @@ -22,6 +22,11 @@ async fn create_template() -> impl Responder { "" } +#[put("")] +async fn change_template() -> impl Responder { + "" +} + #[delete("/{id}")] async fn delete_template() -> impl Responder { "" diff --git a/crates/backend/src/controller/user.rs b/crates/backend/src/controller/user.rs index 3063bfa..8758a9f 100644 --- a/crates/backend/src/controller/user.rs +++ b/crates/backend/src/controller/user.rs @@ -1,4 +1,4 @@ -use actix_web::{Responder, delete, get, post}; +use actix_web::{Responder, delete, get, post, put}; pub fn setup(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(get_users) @@ -22,6 +22,11 @@ async fn create_user() -> impl Responder { "" } +#[put("")] +async fn change_user() -> impl Responder { + "" +} + #[delete("/{id}")] async fn delete_user() -> impl Responder { "" diff --git a/crates/backend/src/db/group.rs b/crates/backend/src/db/group.rs index 0019a00..83983bf 100644 --- a/crates/backend/src/db/group.rs +++ b/crates/backend/src/db/group.rs @@ -1,3 +1,7 @@ -fn add_user_to_group() {} +use super::Database; -fn create_group() {} +impl Database { + async fn add_user_to_group(&self) {} + + async fn create_group(&self) {} +} diff --git a/crates/backend/src/db/project.rs b/crates/backend/src/db/project.rs index 2ddfdf5..ab145a8 100644 --- a/crates/backend/src/db/project.rs +++ b/crates/backend/src/db/project.rs @@ -40,6 +40,19 @@ impl Database { Ok(project) } + pub async fn update_project(&self, project: UpdateProject) -> Result { + debug!("Updating project with id: {}", &project.id); + + let active_model = project::ActiveModel { + id: NotSet, + name: Set(project.name.to_owned()), + }; + + let project = active_model.update(&self.conn).await?; + + Ok(project) + } + pub async fn delete_project(&self, id: &Uuid) -> Result { debug!("Deleting project with id: {}", id); -- 2.45.3 From fa098217f1f18abdb36ab851b9968ab77e9e25c5 Mon Sep 17 00:00:00 2001 From: Mika Date: Wed, 2 Apr 2025 21:23:19 +0200 Subject: [PATCH 2/3] renamed from change to update in PUT endpoints --- crates/backend/src/controller/class.rs | 2 +- crates/backend/src/controller/group.rs | 2 +- crates/backend/src/controller/project.rs | 2 +- crates/backend/src/controller/template.rs | 2 +- crates/backend/src/controller/user.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/backend/src/controller/class.rs b/crates/backend/src/controller/class.rs index d6d11dd..826ac35 100644 --- a/crates/backend/src/controller/class.rs +++ b/crates/backend/src/controller/class.rs @@ -23,7 +23,7 @@ async fn create_class() -> impl Responder { } #[put("")] -async fn change_class() -> impl Responder { +async fn update_class() -> impl Responder { "" } diff --git a/crates/backend/src/controller/group.rs b/crates/backend/src/controller/group.rs index 11e7da3..7d00ea2 100644 --- a/crates/backend/src/controller/group.rs +++ b/crates/backend/src/controller/group.rs @@ -23,7 +23,7 @@ async fn create_group() -> impl Responder { } #[put("")] -async fn change_group() -> impl Responder { +async fn update_group() -> impl Responder { "" } diff --git a/crates/backend/src/controller/project.rs b/crates/backend/src/controller/project.rs index 28f7ad0..d82aa16 100644 --- a/crates/backend/src/controller/project.rs +++ b/crates/backend/src/controller/project.rs @@ -55,7 +55,7 @@ async fn create_project( } #[put("")] -async fn change_project( +async fn update_project( db: web::Data, update_project_struct: web::Json, ) -> Result, ApiError> { diff --git a/crates/backend/src/controller/template.rs b/crates/backend/src/controller/template.rs index aeaaaf2..6b6dc19 100644 --- a/crates/backend/src/controller/template.rs +++ b/crates/backend/src/controller/template.rs @@ -23,7 +23,7 @@ async fn create_template() -> impl Responder { } #[put("")] -async fn change_template() -> impl Responder { +async fn update_template() -> impl Responder { "" } diff --git a/crates/backend/src/controller/user.rs b/crates/backend/src/controller/user.rs index 8758a9f..480a0de 100644 --- a/crates/backend/src/controller/user.rs +++ b/crates/backend/src/controller/user.rs @@ -23,7 +23,7 @@ async fn create_user() -> impl Responder { } #[put("")] -async fn change_user() -> impl Responder { +async fn update_user() -> impl Responder { "" } -- 2.45.3 From 621c41d79312e37763f9442258fbb40a3ff26870 Mon Sep 17 00:00:00 2001 From: Mika Date: Wed, 2 Apr 2025 21:56:51 +0200 Subject: [PATCH 3/3] feat: add update project functionality and corresponding DTO --- bruno/project/Delete Project.bru | 2 +- bruno/project/Update Project.bru | 22 ++++++++++++++++++++++ crates/backend/src/controller/class.rs | 1 + crates/backend/src/controller/group.rs | 1 + crates/backend/src/controller/project.rs | 12 +++++------- crates/backend/src/controller/template.rs | 1 + crates/backend/src/db/project.rs | 8 +++++--- crates/backend/src/dto.rs | 1 + crates/backend/src/dto/project.rs | 8 ++++++++ crates/backend/src/main.rs | 1 + 10 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 bruno/project/Update Project.bru create mode 100644 crates/backend/src/dto.rs create mode 100644 crates/backend/src/dto/project.rs diff --git a/bruno/project/Delete Project.bru b/bruno/project/Delete Project.bru index 930d813..26df51f 100644 --- a/bruno/project/Delete Project.bru +++ b/bruno/project/Delete Project.bru @@ -1,7 +1,7 @@ meta { name: Delete Project type: http - seq: 4 + seq: 5 } delete { diff --git a/bruno/project/Update Project.bru b/bruno/project/Update Project.bru new file mode 100644 index 0000000..b710625 --- /dev/null +++ b/bruno/project/Update Project.bru @@ -0,0 +1,22 @@ +meta { + name: Update Project + type: http + seq: 4 +} + +put { + url: {{api_base}}/project + body: json + auth: inherit +} + +body:json { + { + "id": "{{project_to_change}}", + "name": "ThisProjectHasBeenChanged!" + } +} + +vars:pre-request { + project_to_change: 77d0394b-6d61-44a9-97a8-35caa101dc0e +} diff --git a/crates/backend/src/controller/class.rs b/crates/backend/src/controller/class.rs index 826ac35..999afac 100644 --- a/crates/backend/src/controller/class.rs +++ b/crates/backend/src/controller/class.rs @@ -4,6 +4,7 @@ pub fn setup(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(get_classes) .service(get_class) .service(create_class) + .service(update_class) .service(delete_class); } diff --git a/crates/backend/src/controller/group.rs b/crates/backend/src/controller/group.rs index 7d00ea2..74f46d7 100644 --- a/crates/backend/src/controller/group.rs +++ b/crates/backend/src/controller/group.rs @@ -4,6 +4,7 @@ pub fn setup(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(get_groups) .service(get_groups_for_project) .service(create_group) + .service(update_group) .service(delete_group); } diff --git a/crates/backend/src/controller/project.rs b/crates/backend/src/controller/project.rs index d82aa16..35e017b 100644 --- a/crates/backend/src/controller/project.rs +++ b/crates/backend/src/controller/project.rs @@ -1,25 +1,23 @@ -use actix_web::{Responder, Result, delete, get, post, put, web}; +use actix_web::{Result, delete, get, post, put, web}; use sea_orm::prelude::Uuid; use serde::Deserialize; +use crate::dto::project::UpdateProject; + use crate::db::Database; use crate::error::ApiError; +// Maybe move this here into the corresponding DTO file #[derive(Deserialize)] struct CreateProject { name: String, } -#[derive(Deserialize)] -struct UpdateProject { - id: Uuid, - name: String, -} - pub fn setup(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(get_project) .service(get_projects) .service(create_project) + .service(update_project) .service(delete_project); } diff --git a/crates/backend/src/controller/template.rs b/crates/backend/src/controller/template.rs index 6b6dc19..3d927f5 100644 --- a/crates/backend/src/controller/template.rs +++ b/crates/backend/src/controller/template.rs @@ -4,6 +4,7 @@ pub fn setup(cfg: &mut actix_web::web::ServiceConfig) { cfg.service(get_templates) .service(get_template) .service(create_template) + .service(update_template) .service(delete_template); } diff --git a/crates/backend/src/db/project.rs b/crates/backend/src/db/project.rs index ab145a8..aa355f4 100644 --- a/crates/backend/src/db/project.rs +++ b/crates/backend/src/db/project.rs @@ -2,8 +2,10 @@ use super::Database; use crate::error::ApiError; use log::debug; +use crate::dto::project::UpdateProject; + use entity::project; -use sea_orm::ActiveValue::{NotSet, Set}; +use sea_orm::ActiveValue::{NotSet, Set, Unchanged}; use sea_orm::prelude::Uuid; use sea_orm::{ActiveModelTrait, DeleteResult, EntityTrait}; @@ -44,8 +46,8 @@ impl Database { debug!("Updating project with id: {}", &project.id); let active_model = project::ActiveModel { - id: NotSet, - name: Set(project.name.to_owned()), + id: Unchanged(project.id), + name: Set(project.name), }; let project = active_model.update(&self.conn).await?; diff --git a/crates/backend/src/dto.rs b/crates/backend/src/dto.rs new file mode 100644 index 0000000..36df406 --- /dev/null +++ b/crates/backend/src/dto.rs @@ -0,0 +1 @@ +pub mod project; diff --git a/crates/backend/src/dto/project.rs b/crates/backend/src/dto/project.rs new file mode 100644 index 0000000..e9fedc9 --- /dev/null +++ b/crates/backend/src/dto/project.rs @@ -0,0 +1,8 @@ +use sea_orm::prelude::Uuid; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct UpdateProject { + pub id: Uuid, + pub name: String, +} diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs index 2601185..cd67abb 100644 --- a/crates/backend/src/main.rs +++ b/crates/backend/src/main.rs @@ -7,6 +7,7 @@ use log::debug; mod controller; mod db; +mod dto; mod error; #[actix_web::main] -- 2.45.3