forked from Roflin/gamenight
29 lines
969 B
Rust
29 lines
969 B
Rust
use actix_web::{get, web, HttpResponse, Responder};
|
|
use actix_web::http::header::ContentType;
|
|
use gamenight_database::{DbPool, GetConnection};
|
|
use gamenight_database::owned_game::owned_games;
|
|
use uuid::Uuid;
|
|
use crate::models;
|
|
use crate::request::authorization::AuthUser;
|
|
use crate::request::error::ApiError;
|
|
|
|
#[get("/user/{userId}/owned_games")]
|
|
pub async fn get_user_owned_games(
|
|
pool: web::Data<DbPool>,
|
|
_user: AuthUser,
|
|
user_id: web::Path<Uuid>
|
|
) -> Result<impl Responder, ApiError> {
|
|
let mut conn = pool.get_conn();
|
|
let game_ids = owned_games(&mut conn, user_id.into_inner())?;
|
|
|
|
let model = game_ids.iter().map(|(u,g , l)| models::owned_game::OwnedGame {
|
|
user_id: u.to_string(),
|
|
game_id: g.to_string(),
|
|
location_id: l.map(|x| x.to_string())
|
|
}).collect::<Vec<models::owned_game::OwnedGame>>();
|
|
|
|
Ok(HttpResponse::Ok()
|
|
.content_type(ContentType::json())
|
|
.body(serde_json::to_string(&model)?))
|
|
}
|