Adds the post gamenight handler.

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

View File

@ -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()

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
}

View File

@ -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<Vec::<Gamenight>, DatabaseError> {
pub fn gamenights(conn: &mut PgConnection) -> Result<Vec::<Gamenight>, DatabaseError> {
Ok(gamenight::table.load::<Gamenight>(conn)?)
}
pub fn add_gamenight(conn: &mut PgConnection, gamenight: Gamenight) -> Result<usize, DatabaseError> {
Ok(insert_into(gamenight::table).values(&gamenight).execute(conn)?)
}