From 217e5ee64b4264522abd7778f5de4e9b71a4a907 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sat, 25 Mar 2023 23:32:41 +0100 Subject: [PATCH] Adds get for a single gamenight. --- backend-actix/src/main.rs | 6 ++--- .../src/request/gamenight_handlers.rs | 23 ++++++++++++++++--- backend-actix/src/request/mod.rs | 3 ++- backend-actix/src/request/requests.rs | 7 +++++- backend-actix/src/schema/gamenight.rs | 6 ++++- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/backend-actix/src/main.rs b/backend-actix/src/main.rs index ceb8aa7..cf5d071 100644 --- a/backend-actix/src/main.rs +++ b/backend-actix/src/main.rs @@ -5,8 +5,7 @@ use actix_web::HttpServer; use actix_web::App; use actix_web::web; use diesel::PgConnection; -use request::gamenight; -use request::{login, register, gamenights}; +use request::{login, register, gamenights, gamenight_post, gamenight_get}; use diesel::r2d2::ConnectionManager; use diesel::r2d2::Pool; @@ -30,7 +29,8 @@ async fn main() -> std::io::Result<()> { .service(login) .service(register) .service(gamenights) - .service(gamenight) + .service(gamenight_post) + .service(gamenight_get) }) .bind(("::1", 8080))? .run() diff --git a/backend-actix/src/request/gamenight_handlers.rs b/backend-actix/src/request/gamenight_handlers.rs index 15262ba..f283487 100644 --- a/backend-actix/src/request/gamenight_handlers.rs +++ b/backend-actix/src/request/gamenight_handlers.rs @@ -1,11 +1,11 @@ use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse, post}; use chrono::{DateTime, ParseError}; -use crate::{DbPool, request::{error::ApiError, responses::GameNightResponse, requests::Gamenight}, schema::{self, user::User}}; +use crate::{DbPool, request::{error::ApiError, responses::GameNightResponse, requests::{GamenightPost, GamenightGet}}, schema::{self, user::User}}; use uuid::Uuid; -impl Gamenight { +impl GamenightPost { pub fn into_with_user(&self, user: User) -> Result { return Ok(schema::gamenight::Gamenight { datetime: DateTime::parse_from_rfc3339(&self.datetime)?.with_timezone(&chrono::Utc), @@ -16,6 +16,12 @@ impl Gamenight { } } +impl Into for GamenightGet { + fn into(self) -> Uuid { + Uuid::parse_str(self.id.as_str()).unwrap() + } +} + #[get("/gamenights")] pub async fn gamenights(pool: web::Data, _user: User) -> Result { let mut conn = pool.get().expect("couldn't get db connection from pool"); @@ -28,10 +34,21 @@ pub async fn gamenights(pool: web::Data, _user: User) -> Result, user: User, gamenight_data: web::Json) -> Result { +pub async fn gamenight_post(pool: web::Data, user: User, gamenight_data: web::Json) -> Result { let mut conn = pool.get().expect("couldn't get db connection from pool"); schema::gamenight::add_gamenight(&mut conn, gamenight_data.into_with_user(user)?)?; Ok(HttpResponse::Ok()) +} + +#[get("/gamenight")] +pub async fn gamenight_get(pool: web::Data, _user: User, gamenight_data: web::Json) -> Result { + let mut conn = pool.get().expect("couldn't get db connection from pool"); + + let gamenight = schema::gamenight::get_gamenight(&mut conn, gamenight_data.into_inner().into())?; + + Ok(HttpResponse::Ok() + .content_type(ContentType::json()) + .body(serde_json::to_string(&gamenight)?)) } \ No newline at end of file diff --git a/backend-actix/src/request/mod.rs b/backend-actix/src/request/mod.rs index 888bd8e..dd975f3 100644 --- a/backend-actix/src/request/mod.rs +++ b/backend-actix/src/request/mod.rs @@ -9,4 +9,5 @@ mod authorization; pub use user_handlers::login; pub use user_handlers::register; pub use gamenight_handlers::gamenights; -pub use gamenight_handlers::gamenight; \ No newline at end of file +pub use gamenight_handlers::gamenight_post; +pub use gamenight_handlers::gamenight_get; \ No newline at end of file diff --git a/backend-actix/src/request/requests.rs b/backend-actix/src/request/requests.rs index 30a0bc5..06911cf 100644 --- a/backend-actix/src/request/requests.rs +++ b/backend-actix/src/request/requests.rs @@ -28,7 +28,12 @@ pub struct Register { } #[derive(Serialize, Deserialize, Clone)] -pub struct Gamenight { +pub struct GamenightPost { pub name: String, pub datetime: String +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct GamenightGet { + pub id: String } \ No newline at end of file diff --git a/backend-actix/src/schema/gamenight.rs b/backend-actix/src/schema/gamenight.rs index fc60ec6..4bf7685 100644 --- a/backend-actix/src/schema/gamenight.rs +++ b/backend-actix/src/schema/gamenight.rs @@ -1,5 +1,5 @@ use chrono::{DateTime, Utc}; -use diesel::{Insertable, Queryable, PgConnection, RunQueryDsl, insert_into}; +use diesel::{Insertable, Queryable, PgConnection, RunQueryDsl, insert_into, QueryDsl}; use serde::{Serialize, Deserialize}; use uuid::Uuid; use crate::schema::schema::gamenight; @@ -21,4 +21,8 @@ pub fn gamenights(conn: &mut PgConnection) -> Result, DatabaseE pub fn add_gamenight(conn: &mut PgConnection, gamenight: Gamenight) -> Result { Ok(insert_into(gamenight::table).values(&gamenight).execute(conn)?) +} + +pub(crate) fn get_gamenight(conn: &mut PgConnection, id: Uuid) -> Result { + Ok(gamenight::table.find(id).first(conn)?) } \ No newline at end of file