-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
65 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,62 @@ | ||
use entity::house; | ||
use entity::house::Entity as HouseEntity; | ||
|
||
use axum::{ | ||
http::StatusCode, | ||
Json, Extension, extract::Path, | ||
}; | ||
use sea_orm::{DatabaseConnection, EntityTrait, Set, ActiveModelTrait}; | ||
use std::sync::Arc; | ||
|
||
use crate::AppState; | ||
use axum::extract::State; | ||
use axum::{extract::Path, http::StatusCode, Json}; | ||
use entity::room; | ||
use entity::room::Entity as RoomEntity; | ||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, Set}; | ||
use uuid::Uuid; | ||
|
||
use crate::models::house::{House, CreateHouse}; | ||
use crate::models::room::{CreateRoom, Room}; | ||
|
||
pub async fn all_houses( | ||
Extension(database): Extension<DatabaseConnection>, | ||
) -> Result<Json<Vec<House>>, StatusCode> { | ||
pub async fn list_rooms( | ||
State(database): State<Arc<AppState>>, | ||
Path(house_id): Path<Uuid>, | ||
) -> Result<Json<Vec<Room>>, StatusCode> { | ||
let house_id = house_id.to_owned(); | ||
|
||
let list_houses = HouseEntity::find() | ||
.all(&database) | ||
let get_rooms = RoomEntity::find() | ||
.filter(room::Column::HouseId.contains(house_id)) | ||
.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, | ||
.map(|db_room| Room { | ||
id: db_room.id.to_string(), | ||
name: db_room.name, | ||
house_id: db_room.house_id, | ||
}) | ||
.collect(); | ||
|
||
Ok(Json(list_houses)) | ||
} | ||
|
||
pub async fn create_house( | ||
Extension(database): Extension<DatabaseConnection>, | ||
Json(house_params): Json<CreateHouse>, | ||
) -> 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() | ||
}; | ||
|
||
let _result = new_house | ||
.save(&database) | ||
.await | ||
.unwrap(); | ||
|
||
Ok(()) | ||
Ok(Json(get_rooms)) | ||
} | ||
|
||
pub async fn find_house( | ||
Extension(database): Extension<DatabaseConnection>, | ||
Path(house_id): Path<Uuid> | ||
) -> Result<Json<House>, StatusCode> { | ||
let house_id = house_id.to_owned(); | ||
|
||
let house = HouseEntity::find_by_id(house_id) | ||
.one(&database) | ||
.await | ||
.unwrap(); | ||
|
||
if let Some(house) = house { | ||
Ok(Json(House { | ||
id: house.id, | ||
title: house.title, | ||
body: house.body | ||
})) | ||
} else { | ||
Err(StatusCode::NOT_FOUND) | ||
} | ||
} | ||
|
||
pub async fn delete_house( | ||
Extension(database): Extension<DatabaseConnection>, | ||
Path(house_id): Path<Uuid> | ||
) -> 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)?; | ||
|
||
Ok(()) | ||
pub async fn create_rooms( | ||
State(database): State<Arc<AppState>>, | ||
Json(room_params): Json<CreateRoom>, | ||
) -> Result<Json<Room>, StatusCode> { | ||
let new_room = room::ActiveModel { | ||
id: Set(Uuid::new_v4().to_string()), | ||
name: Set(room_params.name), | ||
house_id: Set(room_params.house_id), | ||
}; | ||
|
||
match new_room.insert(&database.db).await { | ||
Ok(inserted_room) => { | ||
let response_json = Json(Room { | ||
id: inserted_room.id, | ||
name: inserted_room.name, | ||
house_id: inserted_room.house_id, | ||
}); | ||
|
||
Ok(response_json) | ||
} | ||
Err(db_err) => { | ||
let status_code = match db_err { | ||
_ => StatusCode::INTERNAL_SERVER_ERROR, | ||
}; | ||
Err(status_code) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters