forked from Roflin/gamenight
Adds own/disown game logic
This commit is contained in:
@@ -203,6 +203,44 @@ paths:
|
||||
$ref: '#/components/requestBodies/RenameGameRequest'
|
||||
security:
|
||||
- JWT-Auth: []
|
||||
/own:
|
||||
post:
|
||||
responses:
|
||||
'200':
|
||||
description: "OK"
|
||||
'401':
|
||||
$ref: '#/components/responses/FailureResponse'
|
||||
'422':
|
||||
$ref: '#/components/responses/FailureResponse'
|
||||
requestBody:
|
||||
$ref: '#/components/requestBodies/OwnGameRequest'
|
||||
security:
|
||||
- JWT-Auth: []
|
||||
/disown:
|
||||
post:
|
||||
responses:
|
||||
'200':
|
||||
description: "OK"
|
||||
'401':
|
||||
$ref: '#/components/responses/FailureResponse'
|
||||
'422':
|
||||
$ref: '#/components/responses/FailureResponse'
|
||||
requestBody:
|
||||
$ref: '#/components/requestBodies/DisownGameRequest'
|
||||
security:
|
||||
- JWT-Auth: []
|
||||
/owned_games:
|
||||
get:
|
||||
responses:
|
||||
'200':
|
||||
$ref: "#/components/responses/GameIdsResponse"
|
||||
'401':
|
||||
$ref: '#/components/responses/FailureResponse'
|
||||
'422':
|
||||
$ref: '#/components/responses/FailureResponse'
|
||||
security:
|
||||
- JWT-Auth: []
|
||||
|
||||
|
||||
|
||||
components:
|
||||
@@ -357,6 +395,11 @@ components:
|
||||
required:
|
||||
- id
|
||||
- name
|
||||
GameIdsResponse:
|
||||
type: array
|
||||
items:
|
||||
type: string
|
||||
|
||||
requestBodies:
|
||||
LoginRequest:
|
||||
content:
|
||||
@@ -413,6 +456,16 @@ components:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/RenameGameRequestBody'
|
||||
OwnGameRequest:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GameId'
|
||||
DisownGameRequest:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GameId'
|
||||
responses:
|
||||
TokenResponse:
|
||||
description: Example response
|
||||
@@ -466,6 +519,12 @@ components:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Game'
|
||||
GameIdsResponse:
|
||||
description: A list of game ids.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/GameIdsResponse'
|
||||
securitySchemes:
|
||||
JWT-Auth:
|
||||
type: http
|
||||
|
||||
@@ -52,6 +52,9 @@ async fn main() -> std::io::Result<()> {
|
||||
.service(get_game)
|
||||
.service(post_game)
|
||||
.service(post_rename_game)
|
||||
.service(post_own_game)
|
||||
.service(post_disown_game)
|
||||
.service(get_owned_games)
|
||||
})
|
||||
.bind(("::1", 8080))?
|
||||
.run()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use actix_web::{get, http::header::ContentType, post, web, HttpResponse, Responder};
|
||||
use gamenight_database::{game::{insert_game, load_game, rename_game}, DbPool, GetConnection};
|
||||
use gamenight_database::{game::{insert_game, load_game, rename_game}, owned_game::{disown_game, own_game, owned_games, OwnedGame}, DbPool, GetConnection};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{models::{add_game_request_body::AddGameRequestBody, game::Game, game_id::GameId, rename_game_request_body::RenameGameRequestBody}, request::{authorization::AuthUser, error::ApiError}};
|
||||
@@ -60,4 +60,35 @@ pub async fn post_rename_game(pool: web::Data<DbPool>, _user: AuthUser, game_dat
|
||||
rename_game(&mut conn, Uuid::parse_str(&game_data.0.id)?, game_data.0.name)?;
|
||||
|
||||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/own")]
|
||||
pub async fn post_own_game(pool: web::Data<DbPool>, user: AuthUser, game_id: web::Json<GameId>) -> Result <impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
own_game(&mut conn, OwnedGame { user_id: user.0.id, game_id: Uuid::parse_str(&game_id.0.game_id)? })?;
|
||||
|
||||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
|
||||
#[post("/disown")]
|
||||
pub async fn post_disown_game(pool: web::Data<DbPool>, user: AuthUser, game_id: web::Json<GameId>) -> Result <impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
disown_game(&mut conn, OwnedGame { user_id: user.0.id, game_id: Uuid::parse_str(&game_id.0.game_id)? })?;
|
||||
|
||||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
|
||||
#[get("/owned_games")]
|
||||
pub async fn get_owned_games(pool: web::Data<DbPool>, user: AuthUser) -> Result <impl Responder, ApiError> {
|
||||
let mut conn = pool.get_conn();
|
||||
let game_ids = owned_games(&mut conn, user.0.id)?;
|
||||
|
||||
let model : Vec<String> = game_ids.iter().map(|x| {
|
||||
x.to_string()
|
||||
}).collect();
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
.content_type(ContentType::json())
|
||||
.body(serde_json::to_string(&model)?)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -22,3 +22,6 @@ pub use game::get_games;
|
||||
pub use game::get_game;
|
||||
pub use game::post_game;
|
||||
pub use game::post_rename_game;
|
||||
pub use game::post_own_game;
|
||||
pub use game::post_disown_game;
|
||||
pub use game::get_owned_games;
|
||||
|
||||
Reference in New Issue
Block a user