From a4bdf3509b2e8b78ead60dcd09b011626bd9a796 Mon Sep 17 00:00:00 2001 From: Jijo Bose Date: Mon, 15 Jan 2024 23:16:07 +0530 Subject: [PATCH] Axum and SeaORM Rewrite --- .github/workflows/rust.yml | 2 + README.md | 1 - src/api/house.rs | 138 ++++++++++++++++--------------------- src/models/house.rs | 2 +- 4 files changed, 64 insertions(+), 79 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 554a83e..31000a2 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -3,6 +3,8 @@ name: Rust on: push: branches: [ "main" ] + pull_request: + branches: [ "main" ] env: CARGO_TERM_COLOR: always diff --git a/README.md b/README.md index 76c6a20..2547c00 100644 --- a/README.md +++ b/README.md @@ -25,4 +25,3 @@ Built with Axum and SeaORM **Update Entity** ``sea-orm-cli generate entity -u postgres://jijobose:password@localhost/loco_app -o src/database/`` - diff --git a/src/api/house.rs b/src/api/house.rs index 6437e93..4b4024a 100644 --- a/src/api/house.rs +++ b/src/api/house.rs @@ -1,33 +1,26 @@ use std::sync::Arc; -use crate::{database::house::Entity as HouseEntity, AppState}; use crate::database::house; +use crate::{database::house::Entity as HouseEntity, AppState}; use axum::extract::State; -use axum::response::IntoResponse; -use axum::{ - http::StatusCode, - Json, Extension, extract::Path, -}; -use serde_json::json; -use sea_orm::{DatabaseConnection, EntityTrait, Set, ActiveModelTrait}; +use axum::{extract::Path, http::StatusCode, Extension, Json}; +use sea_orm::{ActiveModelTrait, DatabaseConnection, EntityTrait, Set}; use uuid::Uuid; -use crate::models::house::{House, CreateHouse}; - +use crate::models::house::{CreateHouse, House}; pub async fn all_houses( - State(database): State>, + State(database): State>, ) -> Result>, StatusCode> { - let list_houses = HouseEntity::find() .all(&database.db) .await .map_err(|_error| StatusCode::INTERNAL_SERVER_ERROR)? .into_iter() .map(|db_house| House { - id: db_house.id.to_string(), - title: db_house.title, - body: db_house.body, + id: db_house.id.to_string(), + title: db_house.title, + body: db_house.body, }) .collect(); @@ -35,77 +28,68 @@ pub async fn all_houses( } pub async fn create_house( - State(database): State>, - Json(house_params): Json, -) -> Result)> { - - let new_house = house::ActiveModel { - id: Set(Uuid::new_v4().to_string()), - title: Set(house_params.title), - body: Set(house_params.body), - ..Default::default() - }; - - let result = new_house - .save(&database.db) - .await; - - match result { - Ok(_) => { - let house_response = json!({ "status": "success" }); - - return Ok((StatusCode::CREATED, Json(house_response))); + State(database): State>, + Json(house_params): Json, +) -> Result, StatusCode> { + let new_house = house::ActiveModel { + id: Set(Uuid::new_v4().to_string()), + title: Set(house_params.title), + body: Set(house_params.body), + ..Default::default() + }; + + match new_house.insert(&database.db).await { + Ok(inserted_house) => { + let response_json = Json(House { + id: inserted_house.id, + title: inserted_house.title, + body: inserted_house.body, + }); + + Ok(response_json) + } + Err(db_err) => { + let status_code = match db_err { + _ => StatusCode::INTERNAL_SERVER_ERROR, + }; + Err(status_code) + } } - Err(e) => { - if e.to_string() - .contains("duplicate key value violates unique constraint") - { - let error_response = serde_json::json!({ - "status": "fail", - "message": "Note with that title already exists", - }); - return Err((StatusCode::CONFLICT, Json(error_response))); - } - return Err(( - StatusCode::INTERNAL_SERVER_ERROR, - Json(json!({"status": "error","message": format!("{:?}", e)})), - )); - } - - } } +// Todo - Database implementation required pub async fn find_house( - Extension(database): Extension, - Path(house_id): Path + Extension(database): Extension, + Path(house_id): Path, ) -> Result, StatusCode> { - let house_id = house_id.to_owned(); - - let house = HouseEntity::find_by_id(house_id) - .one(&database) - .await - .unwrap(); + let house_id = house_id.to_owned(); - if let Some(house) = house { - Ok(Json(House { - id: house.id.to_string(), - title: house.title, - body: house.body - })) - } else { - Err(StatusCode::NOT_FOUND) - } + let house = HouseEntity::find_by_id(house_id) + .one(&database) + .await + .unwrap(); + + if let Some(house) = house { + Ok(Json(House { + id: house.id.to_string(), + title: house.title, + body: house.body, + })) + } else { + Err(StatusCode::NOT_FOUND) + } } +// Todo - Database implementation required pub async fn delete_house( - Extension(database): Extension, - Path(house_id): Path + Extension(database): Extension, + Path(house_id): Path, ) -> Result<(), StatusCode> { - let house_id = house_id.to_owned(); - HouseEntity::delete_by_id(house_id) - .exec(&database) - .await - .map_err(|_error| StatusCode::INTERNAL_SERVER_ERROR)?; + let house_id = house_id.to_owned(); + HouseEntity::delete_by_id(house_id) + .exec(&database) + .await + .map_err(|_error| StatusCode::INTERNAL_SERVER_ERROR)?; - Ok(()) + Ok(()) } diff --git a/src/models/house.rs b/src/models/house.rs index 6158952..b26beda 100644 --- a/src/models/house.rs +++ b/src/models/house.rs @@ -28,7 +28,7 @@ impl CreateHouse { return Err("Name is empty".to_string()); } if self.body.trim().is_empty() { - return Err("Desciption is empty".to_string()); + return Err("Description is empty".to_string()); } Ok(()) }