forked from Roflin/gamenight
Added gamenight owners and some ui and api functions to delete gamenights.
This commit is contained in:
@@ -141,10 +141,12 @@ pub async fn gamenights_unauthorized() -> ApiResponseVariant {
|
||||
#[post("/gamenight", format = "application/json", data = "<gamenight_json>")]
|
||||
pub async fn gamenight_post_json(
|
||||
conn: DbConn,
|
||||
_user: schema::User,
|
||||
user: schema::User,
|
||||
gamenight_json: Json<schema::GameNightNoId>,
|
||||
) -> ApiResponseVariant {
|
||||
match schema::insert_gamenight(conn, gamenight_json.into_inner()).await {
|
||||
let mut gamenight = gamenight_json.into_inner();
|
||||
gamenight.owner_id = Some(user.id);
|
||||
match schema::insert_gamenight(conn, gamenight).await {
|
||||
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
|
||||
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
||||
}
|
||||
@@ -155,6 +157,39 @@ pub async fn gamenight_post_json_unauthorized() -> ApiResponseVariant {
|
||||
ApiResponseVariant::Status(Status::Unauthorized)
|
||||
}
|
||||
|
||||
#[delete("/gamenight", format = "application/json", data = "<delete_gamenight_json>")]
|
||||
pub async fn gamenight_delete_json(
|
||||
conn: DbConn,
|
||||
user: schema::User,
|
||||
delete_gamenight_json: Json<schema::DeleteGameNight>
|
||||
) -> ApiResponseVariant {
|
||||
if user.role == schema::Role::Admin {
|
||||
if let Err(error) = schema::delete_gamenight(&conn, delete_gamenight_json.game_id).await {
|
||||
return ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())))
|
||||
}
|
||||
return ApiResponseVariant::Value(json!(ApiResponse::SUCCES))
|
||||
}
|
||||
|
||||
match schema::get_gamenight(&conn, delete_gamenight_json.game_id).await {
|
||||
Ok(gamenight) => {
|
||||
if user.id == gamenight.owner_id {
|
||||
if let Err(error) = schema::delete_gamenight(&conn, delete_gamenight_json.game_id).await {
|
||||
return ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())))
|
||||
}
|
||||
return ApiResponseVariant::Value(json!(ApiResponse::SUCCES))
|
||||
}
|
||||
},
|
||||
Err(error) => return ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())))
|
||||
}
|
||||
ApiResponseVariant::Status(Status::Unauthorized)
|
||||
}
|
||||
|
||||
|
||||
#[delete("/gamenight", rank = 2)]
|
||||
pub async fn gamenight_delete_json_unauthorized() -> ApiResponseVariant {
|
||||
ApiResponseVariant::Status(Status::Unauthorized)
|
||||
}
|
||||
|
||||
#[post("/register", format = "application/json", data = "<register_json>")]
|
||||
pub async fn register_post_json(
|
||||
conn: DbConn,
|
||||
|
||||
@@ -33,7 +33,7 @@ impl Default for AppConfig {
|
||||
}
|
||||
|
||||
#[launch]
|
||||
fn rocket() -> _ {
|
||||
async fn rocket() -> _ {
|
||||
let figment = Figment::from(rocket::Config::default())
|
||||
.merge(Serialized::defaults(AppConfig::default()))
|
||||
.merge(Toml::file("App.toml").nested())
|
||||
@@ -61,7 +61,9 @@ fn rocket() -> _ {
|
||||
api::gamenight_post_json,
|
||||
api::gamenight_post_json_unauthorized,
|
||||
api::register_post_json,
|
||||
api::login_post_json
|
||||
api::login_post_json,
|
||||
api::gamenight_delete_json,
|
||||
api::gamenight_delete_json_unauthorized
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ table! {
|
||||
id -> Integer,
|
||||
game -> Text,
|
||||
datetime -> Text,
|
||||
owner_id -> Integer,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,6 +99,24 @@ pub async fn insert_gamenight(conn: DbConn, new_gamenight: GameNightNoId) -> Res
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_gamenight(conn: &DbConn, game_id: i32) -> Result<GameNight, DatabaseError> {
|
||||
conn.run(move |c| {
|
||||
match gamenight::table.find(game_id).first(c) {
|
||||
Ok(gamenight) => Ok(gamenight),
|
||||
Err(error) => Err(DatabaseError::Query(error.to_string()))
|
||||
}
|
||||
}).await
|
||||
}
|
||||
|
||||
pub async fn delete_gamenight(conn: &DbConn, game_id: i32) -> Result<(), DatabaseError> {
|
||||
conn.run(move |c| {
|
||||
match diesel::delete(gamenight::table.filter(gamenight::id.eq(game_id))).execute(c) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(error) => Err(DatabaseError::Query(error.to_string()))
|
||||
}
|
||||
}).await
|
||||
}
|
||||
|
||||
pub async fn insert_user(conn: DbConn, new_user: Register) -> Result<(), DatabaseError> {
|
||||
let salt = SaltString::generate(&mut OsRng);
|
||||
|
||||
@@ -245,7 +264,7 @@ pub async fn run_migrations(rocket: Rocket<Build>) -> Rocket<Build> {
|
||||
rocket
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, DbEnum, Clone, Copy)]
|
||||
#[derive(Debug, Serialize, Deserialize, DbEnum, Clone, Copy, PartialEq)]
|
||||
pub enum Role {
|
||||
Admin,
|
||||
User,
|
||||
@@ -272,11 +291,12 @@ pub struct Game {
|
||||
pub game: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, FromForm, Insertable)]
|
||||
#[derive(Serialize, Deserialize, Debug, Insertable)]
|
||||
#[table_name = "gamenight"]
|
||||
pub struct GameNightNoId {
|
||||
pub game: String,
|
||||
pub datetime: String,
|
||||
pub owner_id: Option<i32>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, FromForm, Queryable)]
|
||||
@@ -284,6 +304,12 @@ pub struct GameNight {
|
||||
pub id: i32,
|
||||
pub game: String,
|
||||
pub datetime: String,
|
||||
pub owner_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, FromForm, Queryable)]
|
||||
pub struct DeleteGameNight {
|
||||
pub game_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Validate, Clone)]
|
||||
|
||||
Reference in New Issue
Block a user