Skip to content

Commit

Permalink
add final files for create routes
Browse files Browse the repository at this point in the history
again thanks to Brook Jeynes for the tutorial
  • Loading branch information
extua committed Oct 1, 2024
1 parent fc2df48 commit d38fff3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
9 changes: 1 addition & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ members = [
"infrastructure",
"application",
"shared",
]

# [dependencies]
# rocket = "0.5"
#
# [dependencies.rocket_dyn_templates]
# version = "~0.2"
# features = ["tera"]
]
1 change: 1 addition & 0 deletions api/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ fn rocket() -> _ {
.mount("/api", routes![
event_handler::list_events_handler,
event_handler::list_event_handler,
event_handler::create_event_handler,
])
}
7 changes: 6 additions & 1 deletion api/src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ pub fn list_events_handler() -> String {

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

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

#[event("/new_event", format = "application/json", data = "<event>")]
pub fn create_event_handler(post: Json<NewEvent>) -> Created<String> {
create::create_event(event)
}
24 changes: 24 additions & 0 deletions application/src/event/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use domain::models::{Event, NewEvent};
use shared::response_models::{Response, ResponseBody};
use infrastructure::establish_connection;
use diesel::prelude::*;
use rocket::response::status::Created;
use rocket::serde::json::Json;

pub fn create_event(event: Json<NewEvent>) -> Created<String> {
use domain::schema::events;

let event = event.into_inner();

match diesel::insert_into(events::table).values(&event).get_result::<Event>(&mut establish_connection()) {
Ok(event) => {
let response = Response { body: ResponseBody::Event(event) };
Created::new("").tagged_body(serde_json::to_string(&response).unwrap())
},
Err(err) => match err {
_ => {
panic!("Database error - {}", err);
}
}
}
}
3 changes: 2 additions & 1 deletion application/src/event/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod read;
pub mod read;
pub mod create;

0 comments on commit d38fff3

Please sign in to comment.