2022-05-28 18:25:53 +02:00
|
|
|
use crate::schema::gamenight::*;
|
2022-05-28 18:32:00 +02:00
|
|
|
use crate::schema::users::*;
|
2022-05-29 10:33:55 +02:00
|
|
|
use crate::schema::DatabaseError;
|
2022-04-21 19:12:16 +02:00
|
|
|
use crate::schema::DbConn;
|
2022-03-29 19:32:21 +02:00
|
|
|
use crate::AppConfig;
|
2022-04-21 19:12:16 +02:00
|
|
|
use chrono::Utc;
|
2022-05-29 10:33:55 +02:00
|
|
|
use futures::future::join_all;
|
2022-05-28 18:32:00 +02:00
|
|
|
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation};
|
2022-04-21 19:12:16 +02:00
|
|
|
use rocket::http::Status;
|
2022-05-28 18:32:00 +02:00
|
|
|
use rocket::request::{FromRequest, Outcome, Request};
|
2022-04-21 19:12:16 +02:00
|
|
|
use rocket::serde::json::{json, Json, Value};
|
2022-03-29 19:32:21 +02:00
|
|
|
use rocket::State;
|
2022-04-21 19:12:16 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-03-29 19:32:21 +02:00
|
|
|
use std::borrow::Cow;
|
2022-05-28 18:32:00 +02:00
|
|
|
use uuid::Uuid;
|
2022-04-21 19:12:16 +02:00
|
|
|
use validator::ValidateArgs;
|
2022-03-29 19:32:21 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Responder)]
|
|
|
|
pub enum ApiResponseVariant {
|
|
|
|
Status(Status),
|
|
|
|
Value(Value),
|
2022-05-01 17:41:14 +02:00
|
|
|
}
|
|
|
|
|
2022-03-29 19:32:21 +02:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct ApiResponse {
|
|
|
|
result: Cow<'static, str>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-04-21 19:12:16 +02:00
|
|
|
message: Option<Cow<'static, str>>,
|
2022-03-29 19:32:21 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-05-01 17:41:14 +02:00
|
|
|
user: Option<UserWithToken>,
|
2022-04-29 22:40:10 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-05-29 00:22:30 +02:00
|
|
|
gamenights: Option<Vec<GamenightOutput>>,
|
2022-05-27 20:53:12 +02:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-05-28 18:25:53 +02:00
|
|
|
games: Option<Vec<Game>>,
|
2022-03-29 19:32:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiResponse {
|
|
|
|
const SUCCES_RESULT: Cow<'static, str> = Cow::Borrowed("Ok");
|
|
|
|
const FAILURE_RESULT: Cow<'static, str> = Cow::Borrowed("Failure");
|
|
|
|
|
|
|
|
const SUCCES: Self = Self {
|
|
|
|
result: Self::SUCCES_RESULT,
|
|
|
|
message: None,
|
2022-05-01 17:41:14 +02:00
|
|
|
user: None,
|
2022-04-29 22:40:10 +02:00
|
|
|
gamenights: None,
|
2022-05-27 20:53:12 +02:00
|
|
|
games: None,
|
2022-03-29 19:32:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
fn error(message: String) -> Self {
|
2022-04-21 19:12:16 +02:00
|
|
|
Self {
|
2022-03-29 19:32:21 +02:00
|
|
|
result: Self::FAILURE_RESULT,
|
|
|
|
message: Some(Cow::Owned(message)),
|
2022-05-01 17:41:14 +02:00
|
|
|
user: None,
|
2022-04-29 22:40:10 +02:00
|
|
|
gamenights: None,
|
2022-05-27 20:53:12 +02:00
|
|
|
games: None,
|
2022-03-29 19:32:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 18:25:53 +02:00
|
|
|
fn login_response(user: User, jwt: String) -> Self {
|
2022-03-29 19:32:21 +02:00
|
|
|
Self {
|
|
|
|
result: Self::SUCCES_RESULT,
|
|
|
|
message: None,
|
2022-05-01 17:41:14 +02:00
|
|
|
user: Some(UserWithToken {
|
|
|
|
user: user,
|
2022-05-28 18:32:00 +02:00
|
|
|
jwt: jwt,
|
2022-05-01 17:41:14 +02:00
|
|
|
}),
|
2022-04-29 22:40:10 +02:00
|
|
|
gamenights: None,
|
2022-05-27 20:53:12 +02:00
|
|
|
games: None,
|
2022-04-29 22:40:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 00:22:30 +02:00
|
|
|
fn gamenight_response(gamenights: Vec<GamenightOutput>) -> Self {
|
2022-04-29 22:40:10 +02:00
|
|
|
Self {
|
|
|
|
result: Self::SUCCES_RESULT,
|
|
|
|
message: None,
|
2022-05-01 17:41:14 +02:00
|
|
|
user: None,
|
2022-04-29 22:40:10 +02:00
|
|
|
gamenights: Some(gamenights),
|
2022-05-27 20:53:12 +02:00
|
|
|
games: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 18:25:53 +02:00
|
|
|
fn games_response(games: Vec<Game>) -> Self {
|
2022-05-27 20:53:12 +02:00
|
|
|
Self {
|
|
|
|
result: Self::SUCCES_RESULT,
|
|
|
|
message: None,
|
|
|
|
user: None,
|
|
|
|
gamenights: None,
|
|
|
|
games: Some(games),
|
2022-03-29 19:32:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ApiError {
|
|
|
|
RequestError(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
const AUTH_HEADER: &str = "Authorization";
|
|
|
|
const BEARER: &str = "Bearer ";
|
|
|
|
|
|
|
|
#[rocket::async_trait]
|
2022-05-28 18:25:53 +02:00
|
|
|
impl<'r> FromRequest<'r> for User {
|
2022-03-29 19:32:21 +02:00
|
|
|
type Error = ApiError;
|
|
|
|
|
|
|
|
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
|
|
|
let header = match req.headers().get_one(AUTH_HEADER) {
|
|
|
|
Some(header) => header,
|
2022-05-28 18:32:00 +02:00
|
|
|
None => return Outcome::Forward(()),
|
2022-03-29 19:32:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if !header.starts_with(BEARER) {
|
2022-04-21 21:35:14 +02:00
|
|
|
return Outcome::Forward(());
|
2022-03-29 19:32:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let app_config = req.guard::<&State<AppConfig>>().await.unwrap().inner();
|
|
|
|
let jwt = header.trim_start_matches(BEARER).to_owned();
|
2022-04-21 19:12:16 +02:00
|
|
|
let token = match decode::<Claims>(
|
|
|
|
&jwt,
|
|
|
|
&DecodingKey::from_secret(app_config.jwt_secret.as_bytes()),
|
|
|
|
&Validation::default(),
|
|
|
|
) {
|
2022-03-29 19:32:21 +02:00
|
|
|
Ok(token) => token,
|
2022-05-28 18:32:00 +02:00
|
|
|
Err(_) => return Outcome::Forward(()),
|
2022-03-29 19:32:21 +02:00
|
|
|
};
|
|
|
|
let id = token.claims.uid;
|
2022-04-21 19:12:16 +02:00
|
|
|
|
2022-03-29 19:32:21 +02:00
|
|
|
let conn = req.guard::<DbConn>().await.unwrap();
|
2022-05-28 18:25:53 +02:00
|
|
|
return match get_user(conn, id).await {
|
2022-05-14 23:36:50 +02:00
|
|
|
Ok(o) => Outcome::Success(o),
|
2022-05-28 18:32:00 +02:00
|
|
|
Err(_) => Outcome::Forward(()),
|
|
|
|
};
|
2022-03-29 19:32:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 00:22:30 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct GamenightOutput {
|
|
|
|
#[serde(flatten)]
|
|
|
|
gamenight: Gamenight,
|
|
|
|
game_list: Vec<Game>,
|
2022-05-29 10:28:53 +02:00
|
|
|
participants: Vec<User>,
|
2022-05-29 00:22:30 +02:00
|
|
|
}
|
|
|
|
|
2022-03-29 19:32:21 +02:00
|
|
|
#[get("/gamenights")]
|
2022-05-28 18:25:53 +02:00
|
|
|
pub async fn gamenights(conn: DbConn, _user: User) -> ApiResponseVariant {
|
2022-05-29 00:22:30 +02:00
|
|
|
let gamenights = match get_all_gamenights(&conn).await {
|
|
|
|
Ok(result) => result,
|
2022-05-29 10:33:55 +02:00
|
|
|
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
2022-05-29 00:22:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let conn_ref = &conn;
|
|
|
|
|
2022-05-29 10:33:55 +02:00
|
|
|
let game_results: Result<Vec<GamenightOutput>, DatabaseError> =
|
|
|
|
join_all(gamenights.iter().map(|gn| async move {
|
|
|
|
let games = get_games_of_gamenight(conn_ref, gn.id).await?;
|
|
|
|
let participants = load_participants(conn_ref, gn.id).await?;
|
|
|
|
Ok(GamenightOutput {
|
|
|
|
gamenight: gn.clone(),
|
|
|
|
game_list: games,
|
|
|
|
participants: participants,
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
2022-05-29 10:28:53 +02:00
|
|
|
|
2022-05-29 00:22:30 +02:00
|
|
|
match game_results {
|
|
|
|
Ok(result) => ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(result))),
|
2022-05-29 10:33:55 +02:00
|
|
|
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
|
|
|
}
|
2022-04-21 21:35:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/gamenights", rank = 2)]
|
|
|
|
pub async fn gamenights_unauthorized() -> ApiResponseVariant {
|
|
|
|
ApiResponseVariant::Status(Status::Unauthorized)
|
2022-03-29 19:32:21 +02:00
|
|
|
}
|
|
|
|
|
2022-05-27 20:53:12 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
2022-05-29 00:22:30 +02:00
|
|
|
pub struct GamenightInput {
|
2022-05-27 20:53:12 +02:00
|
|
|
pub name: String,
|
|
|
|
pub datetime: String,
|
|
|
|
pub owner_id: Option<Uuid>,
|
2022-05-28 18:25:53 +02:00
|
|
|
pub game_list: Vec<Game>,
|
2022-05-27 20:53:12 +02:00
|
|
|
}
|
|
|
|
|
2022-05-29 00:22:30 +02:00
|
|
|
impl Into<Gamenight> for GamenightInput {
|
|
|
|
fn into(self) -> Gamenight {
|
|
|
|
Gamenight {
|
2022-05-27 20:53:12 +02:00
|
|
|
id: Uuid::new_v4(),
|
|
|
|
name: self.name,
|
|
|
|
datetime: self.datetime,
|
2022-05-28 18:32:00 +02:00
|
|
|
owner_id: self.owner_id.unwrap(),
|
2022-05-27 20:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/gamenights", format = "application/json", data = "<gamenight_json>")]
|
|
|
|
pub async fn gamenights_post_json(
|
2022-04-21 19:12:16 +02:00
|
|
|
conn: DbConn,
|
2022-05-28 18:25:53 +02:00
|
|
|
user: User,
|
2022-05-29 00:22:30 +02:00
|
|
|
gamenight_json: Json<GamenightInput>,
|
2022-04-21 19:12:16 +02:00
|
|
|
) -> ApiResponseVariant {
|
2022-05-14 15:46:05 +02:00
|
|
|
let mut gamenight = gamenight_json.into_inner();
|
|
|
|
gamenight.owner_id = Some(user.id);
|
2022-05-27 20:53:12 +02:00
|
|
|
|
|
|
|
let mut mutable_game_list = gamenight.game_list.clone();
|
|
|
|
|
2022-05-28 18:25:53 +02:00
|
|
|
match add_unknown_games(&conn, &mut mutable_game_list).await {
|
2022-05-27 20:53:12 +02:00
|
|
|
Ok(_) => (),
|
2022-05-28 18:32:00 +02:00
|
|
|
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
2022-05-27 20:53:12 +02:00
|
|
|
};
|
|
|
|
|
2022-05-29 10:33:55 +02:00
|
|
|
let gamenight_id = match insert_gamenight(&conn, gamenight.clone().into(), mutable_game_list)
|
|
|
|
.await
|
|
|
|
{
|
2022-05-28 19:28:58 +02:00
|
|
|
Ok(id) => id,
|
|
|
|
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
|
|
|
};
|
|
|
|
|
2022-05-29 10:33:55 +02:00
|
|
|
let participant = GamenightParticipantsEntry {
|
|
|
|
gamenight_id: gamenight_id,
|
|
|
|
user_id: user.id,
|
|
|
|
};
|
2022-05-28 19:28:58 +02:00
|
|
|
match insert_participant(&conn, participant).await {
|
2022-04-29 22:40:10 +02:00
|
|
|
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
|
2022-05-28 18:32:00 +02:00
|
|
|
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
2022-03-29 19:32:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:53:12 +02:00
|
|
|
#[post("/gamenights", rank = 2)]
|
|
|
|
pub async fn gamenights_post_json_unauthorized() -> ApiResponseVariant {
|
2022-04-29 22:40:10 +02:00
|
|
|
ApiResponseVariant::Status(Status::Unauthorized)
|
|
|
|
}
|
|
|
|
|
2022-05-28 18:32:00 +02:00
|
|
|
#[delete(
|
|
|
|
"/gamenights",
|
|
|
|
format = "application/json",
|
|
|
|
data = "<delete_gamenight_json>"
|
|
|
|
)]
|
2022-05-27 20:53:12 +02:00
|
|
|
pub async fn gamenights_delete_json(
|
2022-05-14 15:46:05 +02:00
|
|
|
conn: DbConn,
|
2022-05-28 18:25:53 +02:00
|
|
|
user: User,
|
2022-05-29 00:22:30 +02:00
|
|
|
delete_gamenight_json: Json<DeleteGamenight>,
|
2022-05-14 15:46:05 +02:00
|
|
|
) -> ApiResponseVariant {
|
2022-05-28 18:25:53 +02:00
|
|
|
if user.role == Role::Admin {
|
|
|
|
if let Err(error) = delete_gamenight(&conn, delete_gamenight_json.game_id).await {
|
2022-05-28 18:32:00 +02:00
|
|
|
return ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())));
|
2022-05-14 15:46:05 +02:00
|
|
|
}
|
2022-05-28 18:32:00 +02:00
|
|
|
return ApiResponseVariant::Value(json!(ApiResponse::SUCCES));
|
2022-05-14 15:46:05 +02:00
|
|
|
}
|
2022-05-28 18:32:00 +02:00
|
|
|
|
2022-05-28 18:25:53 +02:00
|
|
|
match get_gamenight(&conn, delete_gamenight_json.game_id).await {
|
2022-05-14 15:46:05 +02:00
|
|
|
Ok(gamenight) => {
|
|
|
|
if user.id == gamenight.owner_id {
|
2022-05-28 18:25:53 +02:00
|
|
|
if let Err(error) = delete_gamenight(&conn, delete_gamenight_json.game_id).await {
|
2022-05-28 18:32:00 +02:00
|
|
|
return ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())));
|
2022-05-14 15:46:05 +02:00
|
|
|
}
|
2022-05-28 18:32:00 +02:00
|
|
|
return ApiResponseVariant::Value(json!(ApiResponse::SUCCES));
|
2022-05-14 15:46:05 +02:00
|
|
|
}
|
2022-05-28 18:32:00 +02:00
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
return ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())))
|
|
|
|
}
|
2022-05-14 15:46:05 +02:00
|
|
|
}
|
|
|
|
ApiResponseVariant::Status(Status::Unauthorized)
|
|
|
|
}
|
|
|
|
|
2022-05-27 20:53:12 +02:00
|
|
|
#[delete("/gamenights", rank = 2)]
|
|
|
|
pub async fn gamenights_delete_json_unauthorized() -> ApiResponseVariant {
|
2022-05-14 15:46:05 +02:00
|
|
|
ApiResponseVariant::Status(Status::Unauthorized)
|
|
|
|
}
|
|
|
|
|
2022-03-29 19:32:21 +02:00
|
|
|
#[post("/register", format = "application/json", data = "<register_json>")]
|
2022-05-28 18:32:00 +02:00
|
|
|
pub async fn register_post_json(conn: DbConn, register_json: Json<Register>) -> ApiResponseVariant {
|
2022-04-21 13:30:44 +02:00
|
|
|
let register = register_json.into_inner();
|
|
|
|
let register_clone = register.clone();
|
2022-04-21 19:12:16 +02:00
|
|
|
match conn
|
|
|
|
.run(move |c| register_clone.validate_args((c, c)))
|
|
|
|
.await
|
|
|
|
{
|
2022-04-21 13:30:44 +02:00
|
|
|
Ok(()) => (),
|
2022-04-21 19:12:16 +02:00
|
|
|
Err(error) => {
|
|
|
|
return ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())))
|
|
|
|
}
|
2022-04-21 13:30:44 +02:00
|
|
|
}
|
2022-04-21 19:12:16 +02:00
|
|
|
|
2022-05-28 18:25:53 +02:00
|
|
|
match insert_user(conn, register).await {
|
2022-03-29 19:32:21 +02:00
|
|
|
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
|
2022-04-21 19:12:16 +02:00
|
|
|
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
2022-03-29 19:32:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
struct Claims {
|
|
|
|
exp: i64,
|
2022-05-27 20:53:12 +02:00
|
|
|
uid: Uuid,
|
2022-05-28 18:25:53 +02:00
|
|
|
role: Role,
|
2022-03-29 19:32:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/login", format = "application/json", data = "<login_json>")]
|
2022-04-21 19:12:16 +02:00
|
|
|
pub async fn login_post_json(
|
|
|
|
conn: DbConn,
|
|
|
|
config: &State<AppConfig>,
|
2022-05-28 18:25:53 +02:00
|
|
|
login_json: Json<Login>,
|
2022-04-21 19:12:16 +02:00
|
|
|
) -> ApiResponseVariant {
|
2022-05-28 18:25:53 +02:00
|
|
|
match login(conn, login_json.into_inner()).await {
|
2022-03-29 19:32:21 +02:00
|
|
|
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
|
|
|
Ok(login_result) => {
|
2022-04-21 19:12:16 +02:00
|
|
|
if !login_result.result {
|
|
|
|
return ApiResponseVariant::Value(json!(ApiResponse::error(String::from(
|
|
|
|
"username and password didn't match"
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
2022-05-01 17:41:14 +02:00
|
|
|
let user = login_result.user.unwrap();
|
2022-03-29 19:32:21 +02:00
|
|
|
let my_claims = Claims {
|
|
|
|
exp: Utc::now().timestamp() + chrono::Duration::days(7).num_seconds(),
|
2022-05-01 17:41:14 +02:00
|
|
|
uid: user.id,
|
|
|
|
role: user.role,
|
2022-03-29 19:32:21 +02:00
|
|
|
};
|
2022-04-21 19:12:16 +02:00
|
|
|
|
2022-03-29 19:32:21 +02:00
|
|
|
let secret = &config.inner().jwt_secret;
|
2022-04-21 19:12:16 +02:00
|
|
|
match encode(
|
|
|
|
&Header::default(),
|
|
|
|
&my_claims,
|
|
|
|
&EncodingKey::from_secret(secret.as_bytes()),
|
|
|
|
) {
|
2022-05-28 18:32:00 +02:00
|
|
|
Ok(token) => {
|
|
|
|
ApiResponseVariant::Value(json!(ApiResponse::login_response(user, token)))
|
|
|
|
}
|
2022-04-21 19:12:16 +02:00
|
|
|
Err(error) => {
|
|
|
|
ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())))
|
|
|
|
}
|
2022-03-29 19:32:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 19:12:16 +02:00
|
|
|
}
|
2022-05-27 20:53:12 +02:00
|
|
|
|
|
|
|
#[get("/games")]
|
2022-05-28 18:25:53 +02:00
|
|
|
pub async fn games(conn: DbConn, _user: User) -> ApiResponseVariant {
|
|
|
|
match get_all_known_games(&conn).await {
|
2022-05-27 20:53:12 +02:00
|
|
|
Ok(games) => ApiResponseVariant::Value(json!(ApiResponse::games_response(games))),
|
2022-05-28 18:32:00 +02:00
|
|
|
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))),
|
2022-05-27 20:53:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/games", rank = 2)]
|
|
|
|
pub async fn games_unauthorized() -> ApiResponseVariant {
|
|
|
|
ApiResponseVariant::Status(Status::Unauthorized)
|
|
|
|
}
|
2022-05-28 19:28:58 +02:00
|
|
|
|
2022-05-29 10:33:55 +02:00
|
|
|
#[get(
|
|
|
|
"/participants",
|
|
|
|
format = "application/json",
|
|
|
|
data = "<gamenight_id_json>"
|
|
|
|
)]
|
|
|
|
pub async fn get_participants(
|
|
|
|
conn: DbConn,
|
|
|
|
_user: User,
|
|
|
|
gamenight_id_json: Json<GamenightId>,
|
|
|
|
) -> ApiResponseVariant {
|
2022-05-29 10:28:53 +02:00
|
|
|
match load_participants(&conn, gamenight_id_json.into_inner().gamenight_id).await {
|
2022-05-28 19:28:58 +02:00
|
|
|
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
|
|
|
|
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/participants", rank = 2)]
|
|
|
|
pub async fn get_participants_unauthorized() -> ApiResponseVariant {
|
|
|
|
ApiResponseVariant::Status(Status::Unauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/participants", format = "application/json", data = "<entry_json>")]
|
2022-05-29 10:33:55 +02:00
|
|
|
pub async fn post_participants(
|
|
|
|
conn: DbConn,
|
|
|
|
_user: User,
|
|
|
|
entry_json: Json<GamenightParticipantsEntry>,
|
|
|
|
) -> ApiResponseVariant {
|
2022-05-28 19:28:58 +02:00
|
|
|
match insert_participant(&conn, entry_json.into_inner()).await {
|
|
|
|
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
|
|
|
|
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/participants", rank = 2)]
|
|
|
|
pub async fn post_participants_unauthorized() -> ApiResponseVariant {
|
|
|
|
ApiResponseVariant::Status(Status::Unauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/participants", format = "application/json", data = "<entry_json>")]
|
2022-05-29 10:33:55 +02:00
|
|
|
pub async fn delete_participants(
|
|
|
|
conn: DbConn,
|
|
|
|
_user: User,
|
|
|
|
entry_json: Json<GamenightParticipantsEntry>,
|
|
|
|
) -> ApiResponseVariant {
|
2022-05-28 19:28:58 +02:00
|
|
|
match remove_participant(&conn, entry_json.into_inner()).await {
|
|
|
|
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
|
|
|
|
Err(error) => ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/participants", rank = 2)]
|
|
|
|
pub async fn delete_participants_unauthorized() -> ApiResponseVariant {
|
|
|
|
ApiResponseVariant::Status(Status::Unauthorized)
|
|
|
|
}
|