Adds the participants part of the API.
This commit is contained in:
parent
836a4ab59f
commit
86cdbedd41
@ -180,7 +180,13 @@ pub async fn gamenights_post_json(
|
||||
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
||||
};
|
||||
|
||||
match insert_gamenight(conn, gamenight.clone().into(), mutable_game_list).await {
|
||||
let gamenight_id = match insert_gamenight(&conn, gamenight.clone().into(), mutable_game_list).await {
|
||||
Ok(id) => id,
|
||||
Err(err) => return ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
||||
};
|
||||
|
||||
let participant = GamenightParticipantsEntry{gamenight_id: gamenight_id, user_id: user.id};
|
||||
match insert_participant(&conn, participant).await {
|
||||
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
|
||||
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
||||
}
|
||||
@ -307,3 +313,43 @@ pub async fn games(conn: DbConn, _user: User) -> ApiResponseVariant {
|
||||
pub async fn games_unauthorized() -> ApiResponseVariant {
|
||||
ApiResponseVariant::Status(Status::Unauthorized)
|
||||
}
|
||||
|
||||
#[get("/participants", format = "application/json", data = "<gamenight_id_json>")]
|
||||
pub async fn get_participants(conn: DbConn, _user: User, gamenight_id_json: Json<GamenightId>) -> ApiResponseVariant {
|
||||
match load_participants(&conn, gamenight_id_json.into_inner()).await {
|
||||
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>")]
|
||||
pub async fn post_participants(conn: DbConn, _user: User, entry_json: Json<GamenightParticipantsEntry>) -> ApiResponseVariant {
|
||||
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>")]
|
||||
pub async fn delete_participants(conn: DbConn, _user: User, entry_json: Json<GamenightParticipantsEntry>) -> ApiResponseVariant {
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -60,6 +60,12 @@ async fn rocket() -> _ {
|
||||
api::gamenights_delete_json_unauthorized,
|
||||
api::games,
|
||||
api::games_unauthorized,
|
||||
api::get_participants,
|
||||
api::get_participants_unauthorized,
|
||||
api::post_participants,
|
||||
api::post_participants_unauthorized,
|
||||
api::delete_participants,
|
||||
api::delete_participants_unauthorized,
|
||||
],
|
||||
);
|
||||
|
||||
|
@ -69,21 +69,27 @@ pub struct DeleteGameNight {
|
||||
pub game_id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Queryable)]
|
||||
pub struct GamenightId {
|
||||
pub gamenight_id: Uuid,
|
||||
}
|
||||
|
||||
pub async fn get_all_gamenights(conn: DbConn) -> Result<Vec<GameNight>, DatabaseError> {
|
||||
Ok(conn.run(|c| gamenight::table.load::<GameNight>(c)).await?)
|
||||
}
|
||||
|
||||
pub async fn insert_gamenight(
|
||||
conn: DbConn,
|
||||
conn: &DbConn,
|
||||
new_gamenight: GameNight,
|
||||
game_list: Vec<Game>,
|
||||
) -> Result<usize, DatabaseError> {
|
||||
) -> Result<Uuid, DatabaseError> {
|
||||
Ok(conn
|
||||
.run(move |c| {
|
||||
c.transaction(|| {
|
||||
diesel::insert_into(gamenight::table)
|
||||
c.transaction::<_, DatabaseError, _>(|| {
|
||||
let id : Uuid = diesel::insert_into(gamenight::table)
|
||||
.values(&new_gamenight)
|
||||
.execute(c)?;
|
||||
.returning(gamenight::id)
|
||||
.get_result(c)?;
|
||||
|
||||
let entries: Vec<GamenightGameListEntry> = game_list
|
||||
.iter()
|
||||
@ -95,10 +101,12 @@ pub async fn insert_gamenight(
|
||||
|
||||
diesel::insert_into(gamenight_gamelist::table)
|
||||
.values(entries)
|
||||
.execute(c)
|
||||
.execute(c)?;
|
||||
|
||||
Ok(id)
|
||||
})
|
||||
})
|
||||
.await?)
|
||||
}).await?
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn get_gamenight(conn: &DbConn, game_id: Uuid) -> Result<GameNight, DatabaseError> {
|
||||
@ -138,6 +146,16 @@ pub async fn add_unknown_games(conn: &DbConn, games: &mut Vec<Game>) -> Result<(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn load_participants(conn: &DbConn, gamenight_id: GamenightId) -> Result<Vec<GamenightParticipantsEntry>, DatabaseError> {
|
||||
Ok(conn
|
||||
.run(move |c| {
|
||||
gamenight_participants::table
|
||||
.filter(gamenight_participants::gamenight_id.eq(gamenight_id.gamenight_id))
|
||||
.load::<GamenightParticipantsEntry>(c)
|
||||
})
|
||||
.await?)
|
||||
}
|
||||
|
||||
pub async fn insert_participant(
|
||||
conn: &DbConn,
|
||||
participant: GamenightParticipantsEntry,
|
||||
|
Loading…
Reference in New Issue
Block a user