Skip to content

Commit

Permalink
Add naive CREATE and DELETE crud handlers for the entity Organization
Browse files Browse the repository at this point in the history
  • Loading branch information
jhodapp committed Nov 12, 2023
1 parent 52c4b3e commit f387053
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions entity/src/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
41 changes: 40 additions & 1 deletion web/src/controller/organization_controller.rs
Original file line number Diff line number Diff line change
@@ -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<AppState>) -> impl IntoResponse {
let organizations = organization::Entity::find()
.all(&app_state.database_connection.unwrap())
Expand All @@ -16,4 +25,34 @@ impl OrganizationController {

Json(organizations)
}

/// CREATE a new Organization entity
pub async fn create(State(app_state): State<AppState>, Json(organization_json): Json<organization::Model>) -> 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<AppState>, Path(id): Path<i32>) -> 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}))
}
}
7 changes: 5 additions & 2 deletions web/src/router.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
}

Expand Down

0 comments on commit f387053

Please sign in to comment.