Skip to content

Commit

Permalink
Fixes htmx (#41)
Browse files Browse the repository at this point in the history
* Renamed HTMX files

* Fixes: Rooms Listing
  • Loading branch information
JijoBose authored May 5, 2024
1 parent 28f24ed commit fa788fa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
37 changes: 27 additions & 10 deletions api/src/handlers/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,25 @@ use uuid::Uuid;

use crate::models::room::{CreateRoom, Room};

// Todo - Fix internal server error
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 get_rooms = RoomEntity::find()
.filter(room::Column::HouseId.contains(house_id))
let list_rooms = RoomEntity::find()
.filter(room::Column::HouseId.eq(house_id))
.all(&database.db)
.await
.map_err(|_error| StatusCode::INTERNAL_SERVER_ERROR)?
.map_err(|_error|
StatusCode::INTERNAL_SERVER_ERROR)?
.into_iter()
.map(|db_room| Room {
id: db_room.id.to_string(),
name: db_room.name,
house_id: db_room.house_id.to_string(),
id: db_room.id.to_string(),
name: db_room.name,
house_id: db_room.house_id.to_string(),
})
.collect();

Ok(Json(get_rooms))
Ok(Json(list_rooms))
}

pub async fn create_rooms(
Expand Down Expand Up @@ -63,3 +61,22 @@ pub async fn create_rooms(
}
}
}

pub async fn find_room(
State(database): State<Arc<AppState>>,
Path(id): Path<Uuid>,
) -> Result<Json<Room>, StatusCode> {
let id = id.to_owned();

let room = RoomEntity::find_by_id(id).one(&database.db).await.unwrap();

if let Some(room) = room {
Ok(Json(Room {
id: room.id.to_string(),
name: room.name,
house_id: room.house_id.to_string(),
}))
} else {
Err(StatusCode::NOT_FOUND)
}
}
10 changes: 8 additions & 2 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Ok;
use axum::serve;
use migration::{Migrator, MigratorTrait};
use sea_orm::{Database, DatabaseConnection};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use std::sync::Arc;
use tokio::net::TcpListener;

Expand All @@ -24,7 +25,12 @@ async fn start() -> anyhow::Result<()> {
Migrator::up(&db_connection, None).await?;

// Initialize tracing
tracing_subscriber::fmt::init();
let filter = tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
tracing_subscriber::registry()
.with(filter)
.with(tracing_subscriber::fmt::layer())
.init();

let app = routes::create_routes(Arc::new(AppState {
db: db_connection.clone(),
Expand All @@ -40,6 +46,6 @@ pub fn main() {
let result = start();

if let Some(err) = result.err() {
println!("Error: {err}");
println!("Main: Error: {err}");
}
}
7 changes: 4 additions & 3 deletions api/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ use crate::AppState;

pub fn create_routes(app_state: Arc<AppState>) -> Router {
Router::new()
// Web Endpoints
.route("/", get(root_path))
.route("/houses", get(get_houses_web))
// .route("/houses", post(create_house_web)) // Todo
// API Endpoints
// Houses API Endpoints
.route("/api/houses", get(all_houses))
.route("/api/houses", post(create_house))
.route("/api/houses/:id", get(find_house))
.route("/api/houses/:id", patch(update_house))
.route("/api/houses/:id", delete(delete_house))
.route("/api/rooms/:house_id", get(list_rooms))
.route("/api/houses/:id/rooms", get(list_rooms))
// Rooms API Endpoints
.route("/api/rooms", post(create_rooms))
.with_state(app_state)
}

0 comments on commit fa788fa

Please sign in to comment.