From f3870539d5fe9550d44a90e8a6aa883c097845f0 Mon Sep 17 00:00:00 2001 From: Jim Hodapp Date: Sun, 12 Nov 2023 12:05:28 -0600 Subject: [PATCH] Add naive CREATE and DELETE crud handlers for the entity Organization --- entity/src/organization.rs | 1 + web/src/controller/organization_controller.rs | 41 ++++++++++++++++++- web/src/router.rs | 7 +++- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/entity/src/organization.rs b/entity/src/organization.rs index a9164cd..a489f5a 100644 --- a/entity/src/organization.rs +++ b/entity/src/organization.rs @@ -8,6 +8,7 @@ use serde::{Deserialize, Serialize}; pub struct Model { #[sea_orm(primary_key)] #[serde(skip_deserializing)] + // TODO: consider changing this to a u64 pub id: i32, pub name: String, } diff --git a/web/src/controller/organization_controller.rs b/web/src/controller/organization_controller.rs index a1b8b9b..18b68fe 100644 --- a/web/src/controller/organization_controller.rs +++ b/web/src/controller/organization_controller.rs @@ -1,13 +1,22 @@ use crate::AppState; -use axum::extract::State; +use axum::extract::{Path, State}; use axum::response::IntoResponse; use axum::Json; use entity::organization; +use entity::organization::Entity as Organization; +use sea_orm::ActiveValue::{Set, NotSet}; +use sea_orm::ActiveModelTrait; +use sea_orm::DeleteResult; use sea_orm::entity::EntityTrait; +use serde_json::json; + +extern crate log; +use log::*; pub struct OrganizationController {} impl OrganizationController { + /// GET all Organizations pub async fn index(State(app_state): State) -> impl IntoResponse { let organizations = organization::Entity::find() .all(&app_state.database_connection.unwrap()) @@ -16,4 +25,34 @@ impl OrganizationController { Json(organizations) } + + /// CREATE a new Organization entity + pub async fn create(State(app_state): State, Json(organization_json): Json) -> impl IntoResponse { + debug!("CREATE new Organization: {}", organization_json.name); + + let organization_active_model = organization::ActiveModel { + id: NotSet, + name: Set(organization_json.name), + }; + + let organization: organization::Model = organization_active_model.insert(&app_state.database_connection.unwrap()) + .await + .unwrap(); + + Json(organization) + } + + /// DELETE an Organization entity specified by its primary key + pub async fn delete(State(app_state): State, Path(id): Path) -> impl IntoResponse { + debug!("DELETE Organization by id: {}", id); + + let res: DeleteResult = Organization::delete_by_id(id).exec(&app_state.database_connection.unwrap()) + .await + .unwrap(); + + // TODO: temporary check while learning, return a DBErr instead + assert_eq!(res.rows_affected, 1); + + Json(json!({"id": id})) + } } diff --git a/web/src/router.rs b/web/src/router.rs index 5b0c180..fdf5f4f 100644 --- a/web/src/router.rs +++ b/web/src/router.rs @@ -1,6 +1,6 @@ use crate::AppState; use axum::{ - routing::{get, get_service}, + routing::{delete, get, get_service, post}, Router, }; use tower_http::services::ServeDir; @@ -15,7 +15,10 @@ pub fn define_routes(app_state: AppState) -> Router { pub fn organization_routes(app_state: AppState) -> Router { Router::new() - .route("/organization", get(OrganizationController::index)) + // TODO: Add an API versioning scheme and prefix all routes with it + .route("/organizations", get(OrganizationController::index)) + .route("/organizations", post(OrganizationController::create)) + .route("/organizations/:id", delete(OrganizationController::delete)) .with_state(app_state) }