forked from Roflin/gamenight
Adds the post gamenight handler.
This commit is contained in:
parent
534e6867d8
commit
5216f55a14
@ -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()
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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> {
|
||||
@ -12,3 +26,12 @@ pub async fn gamenights(pool: web::Data<DbPool>, _user: User) -> Result<impl Res
|
||||
.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())
|
||||
}
|
@ -9,3 +9,4 @@ mod authorization;
|
||||
pub use user_handlers::login;
|
||||
pub use user_handlers::register;
|
||||
pub use gamenight_handlers::gamenights;
|
||||
pub use gamenight_handlers::gamenight;
|
@ -26,3 +26,9 @@ pub struct Register {
|
||||
pub password: String,
|
||||
pub password_repeat: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub struct Gamenight {
|
||||
pub name: String,
|
||||
pub datetime: String
|
||||
}
|
@ -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)?)
|
||||
}
|
Loading…
Reference in New Issue
Block a user