DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 8db41b3a1e2ad2024db3d194fa35b4c231b2f3b5 deck-builder/api/src/handlers/game/mod.rs -rw-r--r-- 4.0 KiB
8db41b3aJonni Liljamo feat(client): revamp user details event, send it when needed 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * This file is part of laurelin/api
 * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
 *
 * Licensed under GPL-3.0-only.
 * See LICENSE for licensing information.
 */

use actix_session::Session;
use actix_web::{get, post, web, HttpResponse, Responder};
use laurelin_shared::error::api::APIError;

use crate::{actions, session, PgPool};

#[post("/api/game")]
async fn create(session: Session, pool: web::Data<PgPool>) -> impl Responder {
    let session_validation = session::validate_session(&session);

    match session_validation {
        Err(err) => err,
        Ok(user_id) => {
            let game_create = web::block(move || {
                let mut conn = match pool.get() {
                    Err(_) => return Err(APIError::DatabasePoolGetFailed),
                    Ok(conn) => conn,
                };
                actions::game::create(&mut conn, &user_id)
            })
            .await;
            match game_create {
                Err(_err) => {
                    return HttpResponse::InternalServerError().json(APIError::Undefined);
                }
                Ok(game_res) => match game_res {
                    Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
                    Ok(game) => HttpResponse::Ok().json(game),
                },
            }
        }
    }
}

#[get("/api/game/{id}")]
async fn info(session: Session, pool: web::Data<PgPool>, id: web::Path<String>) -> impl Responder {
    let session_validation = session::validate_session(&session);

    match session_validation {
        Err(err) => err,
        Ok(user_id) => {
            //
            return HttpResponse::Ok().body("INFO");
        }
    }
}

#[post("/api/game/{id}/join")]
async fn join(session: Session, pool: web::Data<PgPool>, id: web::Path<String>) -> impl Responder {
    let session_validation = session::validate_session(&session);

    match session_validation {
        Err(err) => err,
        Ok(user_id) => {
            //
            return HttpResponse::Ok().body("JOIN");
        }
    }
}

#[get("/api/game/all_forming")]
async fn all_forming(session: Session, pool: web::Data<PgPool>) -> impl Responder {
    let session_validation = session::validate_session(&session);

    match session_validation {
        Err(err) => err,
        Ok(user_id) => {
            let all_forming = web::block(move || {
                let mut conn = match pool.get() {
                    Err(_) => return Err(APIError::DatabasePoolGetFailed),
                    Ok(conn) => conn,
                };
                actions::game::all_forming(&mut conn)
            })
            .await;
            match all_forming {
                Err(_err) => {
                    return HttpResponse::InternalServerError().json(APIError::Undefined);
                }
                Ok(games_res) => match games_res {
                    Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
                    Ok(games) => HttpResponse::Ok().json(games),
                },
            }
        }
    }
}

#[get("/api/game/my_games")]
async fn my_games(session: Session, pool: web::Data<PgPool>) -> impl Responder {
    let session_validation = session::validate_session(&session);

    match session_validation {
        Err(err) => err,
        Ok(user_id) => {
            let my_games = web::block(move || {
                let mut conn = match pool.get() {
                    Err(_) => return Err(APIError::DatabasePoolGetFailed),
                    Ok(conn) => conn,
                };
                actions::game::my_games(&mut conn, &user_id)
            })
            .await;
            match my_games {
                Err(_err) => {
                    return HttpResponse::InternalServerError().json(APIError::Undefined);
                }
                Ok(games_res) => match games_res {
                    Err(err) => HttpResponse::InternalServerError().body(err.to_string()),
                    Ok(games) => HttpResponse::Ok().json(games),
                },
            }
        }
    }
}