Skip to content

Commit

Permalink
more api handling stuff
Browse files Browse the repository at this point in the history
this should also trigger the checking workflow
  • Loading branch information
extua committed Sep 26, 2024
1 parent 0a745d7 commit d691814
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
10 changes: 8 additions & 2 deletions api/src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ use rocket::serde::json::Json;

#[get("/")]
pub fn list_events_handler() -> String {
todo!()
let posts: Vec<Event> = read::list_events();
let response = Response { body: ResponseBody::Events(events) };

serde_json::to_string(&response).unwrap()
}

#[get("/event/<event_id>")]
pub fn list_event_handler(event_id: i32) -> Result<String, NotFound<String>> {
todo!()
let event = read::list_post(event_id)?;
let response = Response { body: ResponseBody::Event(event) };

Ok(serde_json::to_string(&response).unwrap())
}
7 changes: 7 additions & 0 deletions application/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
1 change: 1 addition & 0 deletions application/src/event/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod read;
38 changes: 38 additions & 0 deletions application/src/event/read.rs
Original file line number Diff line number Diff line change
@@ -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<Event, NotFound<String>> {
use domain::schema::events;

match events::table.find(event_id).first::<Event>(&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<Event> {
use domain::schema::events;

match events::table.select(events::all_columns).load::<Event>(&mut establish_connection()) {
Ok(mut events) => {
events.sort();
events
},
Err(err) => match err {
_ => {
panic!("Database error - {}", err);
}
}
}
}
15 changes: 1 addition & 14 deletions application/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit d691814

Please sign in to comment.