diff --git a/entity/src/organization.rs b/entity/src/organization.rs index a489f5a..3ca749b 100644 --- a/entity/src/organization.rs +++ b/entity/src/organization.rs @@ -3,7 +3,7 @@ use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, PartialEq, DeriveEntityModel, Eq, Deserialize, Serialize)] #[sea_orm(schema_name = "refactor_platform_rs", table_name = "organizations")] pub struct Model { #[sea_orm(primary_key)] diff --git a/web/src/controller/organization_controller.rs b/web/src/controller/organization_controller.rs index da4bdc4..d5c8589 100644 --- a/web/src/controller/organization_controller.rs +++ b/web/src/controller/organization_controller.rs @@ -1,5 +1,5 @@ use crate::AppState; -use axum::extract::{Path, State}; +use axum::extract::{Path, Query, State}; use axum::response::IntoResponse; use axum::Json; use entity::organization; @@ -29,6 +29,21 @@ impl OrganizationController { Json(organizations) } + /// GET a particular Organization entity specified by its primary key + /// Test this with curl: curl --header "Content-Type: application/json" \ in zsh at 12:03:06 + /// --request GET \ + /// http://localhost:3000/organizations/ + pub async fn read(State(app_state): State, Path(id): Path) -> impl IntoResponse { + debug!("GET Organization by id: {}", id); + + let organization: Option = organization::Entity::find_by_id(id) + .one(&app_state.database_connection.unwrap()) + .await + .unwrap_or_default(); + + Json(organization) + } + /// CREATE a new Organization entity /// Test this with curl: curl --header "Content-Type: application/json" \ /// --request POST \ @@ -48,11 +63,48 @@ impl OrganizationController { let organization: organization::Model = organization_active_model .insert(&app_state.database_connection.unwrap()) .await - .unwrap(); + .unwrap_or_default(); Json(organization) } + /// UPDATE a particular Organization entity specified by its primary key + /// Test this with curl: curl --header "Content-Type: application/json" \ in zsh at 12:03:06 + /// --request PUT \ + /// http://localhost:3000/organizations/\?name\=New_Organization_Name + pub async fn update( + State(app_state): State, + Path(id): Path, + Query(organization_params): Query, + ) -> impl IntoResponse { + debug!( + "UPDATE the entire Organization by id: {}, new name: {}", + id, organization_params.name + ); + + let db = app_state.database_connection.as_ref().unwrap(); + + let organization_to_update = organization::Entity::find_by_id(id) + .one(db) + .await + .unwrap_or_default(); + + let updated_organization = match organization_to_update { + Some(org) => { + let mut organization_am: organization::ActiveModel = org.into(); + organization_am.name = Set(organization_params.name); + + organization::Entity::update(organization_am) + .exec(db) + .await + .unwrap() + } + None => organization::Model::default(), + }; + + Json(updated_organization) + } + /// DELETE an Organization entity specified by its primary key /// Test this with curl: curl --header "Content-Type: application/json" \ in zsh at 12:03:06 /// --request DELETE \ diff --git a/web/src/router.rs b/web/src/router.rs index fdf5f4f..cef44f7 100644 --- a/web/src/router.rs +++ b/web/src/router.rs @@ -1,6 +1,6 @@ use crate::AppState; use axum::{ - routing::{delete, get, get_service, post}, + routing::{delete, get, get_service, post, put}, Router, }; use tower_http::services::ServeDir; @@ -16,8 +16,11 @@ pub fn define_routes(app_state: AppState) -> Router { pub fn organization_routes(app_state: AppState) -> Router { Router::new() // TODO: Add an API versioning scheme and prefix all routes with it + // See Router::nest() - https://docs.rs/axum/latest/axum/struct.Router.html#method.nest .route("/organizations", get(OrganizationController::index)) + .route("/organizations/:id", get(OrganizationController::read)) .route("/organizations", post(OrganizationController::create)) + .route("/organizations/:id", put(OrganizationController::update)) .route("/organizations/:id", delete(OrganizationController::delete)) .with_state(app_state) }