From 5216f55a1411266c172a013442e741a0bf9d131f Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Sat, 25 Mar 2023 22:44:58 +0100 Subject: [PATCH] Adds the post gamenight handler. --- backend-actix/src/main.rs | 2 ++ backend-actix/src/request/error.rs | 9 +++++++ .../src/request/gamenight_handlers.rs | 27 +++++++++++++++++-- backend-actix/src/request/mod.rs | 3 ++- backend-actix/src/request/requests.rs | 6 +++++ backend-actix/src/schema/gamenight.rs | 8 ++++-- 6 files changed, 50 insertions(+), 5 deletions(-) diff --git a/backend-actix/src/main.rs b/backend-actix/src/main.rs index e9ffb40..ceb8aa7 100644 --- a/backend-actix/src/main.rs +++ b/backend-actix/src/main.rs @@ -5,6 +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 diesel::r2d2::ConnectionManager; use diesel::r2d2::Pool; @@ -29,6 +30,7 @@ async fn main() -> std::io::Result<()> { .service(login) .service(register) .service(gamenights) + .service(gamenight) }) .bind(("::1", 8080))? .run() diff --git a/backend-actix/src/request/error.rs b/backend-actix/src/request/error.rs index 6519952..683febc 100644 --- a/backend-actix/src/request/error.rs +++ b/backend-actix/src/request/error.rs @@ -72,3 +72,12 @@ impl From for ApiError { } } } + +impl From for ApiError { + fn from(value: chrono::ParseError) -> Self { + ApiError { + status: 422, + message: value.to_string() + } + } +} diff --git a/backend-actix/src/request/gamenight_handlers.rs b/backend-actix/src/request/gamenight_handlers.rs index 4cc923a..15262ba 100644 --- a/backend-actix/src/request/gamenight_handlers.rs +++ b/backend-actix/src/request/gamenight_handlers.rs @@ -1,6 +1,20 @@ -use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse}; +use actix_web::{get, web, Responder, http::header::ContentType, HttpResponse, post}; +use chrono::{DateTime, ParseError}; -use crate::{DbPool, request::{error::ApiError, responses::GameNightResponse}, schema::{self, user::User}}; +use crate::{DbPool, request::{error::ApiError, responses::GameNightResponse, requests::Gamenight}, schema::{self, user::User}}; + +use uuid::Uuid; + +impl Gamenight { + 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), + id: Uuid::new_v4(), + name: self.name.clone(), + owner_id: user.id + }) + } +} #[get("/gamenights")] pub async fn gamenights(pool: web::Data, _user: User) -> Result { @@ -11,4 +25,13 @@ pub async fn gamenights(pool: web::Data, _user: User) -> Result, 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()) } \ No newline at end of file diff --git a/backend-actix/src/request/mod.rs b/backend-actix/src/request/mod.rs index a29ade6..888bd8e 100644 --- a/backend-actix/src/request/mod.rs +++ b/backend-actix/src/request/mod.rs @@ -8,4 +8,5 @@ mod authorization; pub use user_handlers::login; pub use user_handlers::register; -pub use gamenight_handlers::gamenights; \ No newline at end of file +pub use gamenight_handlers::gamenights; +pub use gamenight_handlers::gamenight; \ No newline at end of file diff --git a/backend-actix/src/request/requests.rs b/backend-actix/src/request/requests.rs index 8d14eca..30a0bc5 100644 --- a/backend-actix/src/request/requests.rs +++ b/backend-actix/src/request/requests.rs @@ -25,4 +25,10 @@ pub struct Register { #[validate(length(min = 10), must_match = "password_repeat")] pub password: String, pub password_repeat: String, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct Gamenight { + pub name: String, + pub datetime: String } \ No newline at end of file diff --git a/backend-actix/src/schema/gamenight.rs b/backend-actix/src/schema/gamenight.rs index ffb4b1f..fc60ec6 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}; +use diesel::{Insertable, Queryable, PgConnection, RunQueryDsl, insert_into}; use serde::{Serialize, Deserialize}; use uuid::Uuid; use crate::schema::schema::gamenight; @@ -15,6 +15,10 @@ pub struct Gamenight { pub owner_id: Uuid, } -pub fn gamenights(conn: &mut PgConnection, ) -> Result, DatabaseError> { +pub fn gamenights(conn: &mut PgConnection) -> Result, DatabaseError> { Ok(gamenight::table.load::(conn)?) +} + +pub fn add_gamenight(conn: &mut PgConnection, gamenight: Gamenight) -> Result { + Ok(insert_into(gamenight::table).values(&gamenight).execute(conn)?) } \ No newline at end of file