From d6918145e39e5097aef939e45fe36804b67e923b Mon Sep 17 00:00:00 2001 From: Pierre Marshall Date: Thu, 26 Sep 2024 22:16:41 +0100 Subject: [PATCH] more api handling stuff this should also trigger the checking workflow --- api/src/event_handler.rs | 10 +++++++-- application/Cargo.toml | 7 +++++++ application/src/event/mod.rs | 1 + application/src/event/read.rs | 38 +++++++++++++++++++++++++++++++++++ application/src/lib.rs | 15 +------------- 5 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 application/src/event/mod.rs create mode 100644 application/src/event/read.rs diff --git a/api/src/event_handler.rs b/api/src/event_handler.rs index 4c0858f..69dc690 100644 --- a/api/src/event_handler.rs +++ b/api/src/event_handler.rs @@ -7,10 +7,16 @@ use rocket::serde::json::Json; #[get("/")] pub fn list_events_handler() -> String { - todo!() + let posts: Vec = read::list_events(); + let response = Response { body: ResponseBody::Events(events) }; + + serde_json::to_string(&response).unwrap() } #[get("/event/")] pub fn list_event_handler(event_id: i32) -> Result> { - todo!() + let event = read::list_post(event_id)?; + let response = Response { body: ResponseBody::Event(event) }; + + Ok(serde_json::to_string(&response).unwrap()) } \ No newline at end of file diff --git a/application/Cargo.toml b/application/Cargo.toml index c17a9d2..7864cea 100644 --- a/application/Cargo.toml +++ b/application/Cargo.toml @@ -4,3 +4,10 @@ version = "0.1.0" edition = "2021" [dependencies] +domain = { path = "../domain" } +infrastructure = { path = "../infrastructure" } +shared = { path = "../shared" } + +diesel = { version = "2.0", features = ["sqlite, "returning_clauses_for_sqlite_3_35"] } +serde_json = "1.0" +rocket = { version = "0.5.0", features = ["json"] } \ No newline at end of file diff --git a/application/src/event/mod.rs b/application/src/event/mod.rs new file mode 100644 index 0000000..d3d7132 --- /dev/null +++ b/application/src/event/mod.rs @@ -0,0 +1 @@ +pub mod read; \ No newline at end of file diff --git a/application/src/event/read.rs b/application/src/event/read.rs new file mode 100644 index 0000000..b67b7b0 --- /dev/null +++ b/application/src/event/read.rs @@ -0,0 +1,38 @@ +use domain::models::Event; +use shared::response_models::{Response, ResponseBody}; +use infrastructure::establish_connection; +use diesel::prelude::*; +use rocket::response::status::NotFound; + +pub fn list_event(event_id: i32) -> Result> { + use domain::schema::events; + + match events::table.find(event_id).first::(&mut establish_connection()) { + Ok(event) => Ok(event), + Err(err) => match err { + diesel::result::Error::NotFound => { + let response = Response { body: ResponseBody::Message(format!("Error selecting event with id {} - {}", event_id, err))}; + return Err(NotFound(serde_json::to_string(&response).unwrap())); + }, + _ => { + panic!("Database error - {}", err); + } + } + } +} + +pub fn list_events() -> Vec { + use domain::schema::events; + + match events::table.select(events::all_columns).load::(&mut establish_connection()) { + Ok(mut events) => { + events.sort(); + events + }, + Err(err) => match err { + _ => { + panic!("Database error - {}", err); + } + } + } +} \ No newline at end of file diff --git a/application/src/lib.rs b/application/src/lib.rs index b93cf3f..c47b0f4 100644 --- a/application/src/lib.rs +++ b/application/src/lib.rs @@ -1,14 +1 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} +pub mod event; \ No newline at end of file