Skip to content

Commit

Permalink
Adds Read and Update CRUD handlers to the Organization entity
Browse files Browse the repository at this point in the history
  • Loading branch information
jhodapp committed Nov 15, 2023
1 parent 9db120d commit 4b5b553
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion entity/src/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
56 changes: 54 additions & 2 deletions web/src/controller/organization_controller.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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/<id>
pub async fn read(State(app_state): State<AppState>, Path(id): Path<i32>) -> impl IntoResponse {
debug!("GET Organization by id: {}", id);

let organization: Option<organization::Model> = 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 \
Expand All @@ -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/<id>\?name\=New_Organization_Name
pub async fn update(
State(app_state): State<AppState>,
Path(id): Path<i32>,
Query(organization_params): Query<organization::Model>,
) -> 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 \
Expand Down
5 changes: 4 additions & 1 deletion web/src/router.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
}
Expand Down

0 comments on commit 4b5b553

Please sign in to comment.