forked from Roflin/gamenight
Adds get for a single gamenight.
This commit is contained in:
parent
5216f55a14
commit
217e5ee64b
@ -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()
|
||||
|
@ -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<schema::gamenight::Gamenight, ParseError> {
|
||||
return Ok(schema::gamenight::Gamenight {
|
||||
datetime: DateTime::parse_from_rfc3339(&self.datetime)?.with_timezone(&chrono::Utc),
|
||||
@ -16,6 +16,12 @@ impl Gamenight {
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Uuid> for GamenightGet {
|
||||
fn into(self) -> Uuid {
|
||||
Uuid::parse_str(self.id.as_str()).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/gamenights")]
|
||||
pub async fn gamenights(pool: web::Data<DbPool>, _user: User) -> Result<impl Responder, ApiError> {
|
||||
let mut conn = pool.get().expect("couldn't get db connection from pool");
|
||||
@ -28,10 +34,21 @@ pub async fn gamenights(pool: web::Data<DbPool>, _user: User) -> Result<impl Res
|
||||
}
|
||||
|
||||
#[post("/gamenight")]
|
||||
pub async fn gamenight(pool: web::Data<DbPool>, user: User, gamenight_data: web::Json<Gamenight>) -> Result<impl Responder, ApiError> {
|
||||
pub async fn gamenight_post(pool: web::Data<DbPool>, user: User, gamenight_data: web::Json<GamenightPost>) -> 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())
|
||||
}
|
||||
|
||||
#[get("/gamenight")]
|
||||
pub async fn gamenight_get(pool: web::Data<DbPool>, _user: User, gamenight_data: web::Json<GamenightGet>) -> Result<impl Responder, ApiError> {
|
||||
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)?))
|
||||
}
|
@ -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;
|
||||
pub use gamenight_handlers::gamenight_post;
|
||||
pub use gamenight_handlers::gamenight_get;
|
@ -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
|
||||
}
|
@ -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;
|
||||
@ -22,3 +22,7 @@ pub fn gamenights(conn: &mut PgConnection) -> Result<Vec::<Gamenight>, DatabaseE
|
||||
pub fn add_gamenight(conn: &mut PgConnection, gamenight: Gamenight) -> Result<usize, DatabaseError> {
|
||||
Ok(insert_into(gamenight::table).values(&gamenight).execute(conn)?)
|
||||
}
|
||||
|
||||
pub(crate) fn get_gamenight(conn: &mut PgConnection, id: Uuid) -> Result<Gamenight, DatabaseError> {
|
||||
Ok(gamenight::table.find(id).first(conn)?)
|
||||
}
|
Loading…
Reference in New Issue
Block a user