Adds the post gamenight handler.

This commit is contained in:
2023-03-25 22:44:58 +01:00
parent 534e6867d8
commit 5216f55a14
6 changed files with 50 additions and 5 deletions

View File

@@ -72,3 +72,12 @@ impl From<ValidationErrors> for ApiError {
}
}
}
impl From<chrono::ParseError> for ApiError {
fn from(value: chrono::ParseError) -> Self {
ApiError {
status: 422,
message: value.to_string()
}
}
}

View File

@@ -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<schema::gamenight::Gamenight, ParseError> {
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<DbPool>, _user: User) -> Result<impl Responder, ApiError> {
@@ -11,4 +25,13 @@ pub async fn gamenights(pool: web::Data<DbPool>, _user: User) -> Result<impl Res
.content_type(ContentType::json())
.body(serde_json::to_string(&GameNightResponse { gamenights })?)
)
}
#[post("/gamenight")]
pub async fn gamenight(pool: web::Data<DbPool>, user: User, gamenight_data: web::Json<Gamenight>) -> Result<impl Responder, ApiError> {
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())
}

View File

@@ -8,4 +8,5 @@ mod authorization;
pub use user_handlers::login;
pub use user_handlers::register;
pub use gamenight_handlers::gamenights;
pub use gamenight_handlers::gamenights;
pub use gamenight_handlers::gamenight;

View File

@@ -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
}