فهرست منبع

Create route to get all of a users games

Lee Morgan 1 ماه پیش
والد
کامیت
e2b5a3b202
4فایلهای تغییر یافته به همراه35 افزوده شده و 1 حذف شده
  1. 18 0
      src/controllers/game/get_many.rs
  2. 1 0
      src/controllers/game/mod.rs
  3. 13 0
      src/models/game.rs
  4. 3 1
      src/routes/game/game.rs

+ 18 - 0
src/controllers/game/get_many.rs

@@ -0,0 +1,18 @@
+use actix_web::{HttpResponse, HttpRequest, web, get};
+use sqlx::PgPool;
+use crate::{
+    http_error::HttpError,
+    auth::user_auth,
+    models::game::Game
+};
+
+#[get("/game")]
+pub async fn route(
+    db: web::Data<PgPool>,
+    req: HttpRequest
+) -> Result<HttpResponse, HttpError> {
+    let user = user_auth(&db, &req, false).await?;
+    let games = Game::get_many(&db, user.id).await?;
+
+    Ok(HttpResponse::Ok().json(games))
+}

+ 1 - 0
src/controllers/game/mod.rs

@@ -1,5 +1,6 @@
 pub mod create;
 pub mod get_one;
+pub mod get_many;
 
 pub mod character;
 pub mod session;

+ 13 - 0
src/models/game.rs

@@ -65,6 +65,19 @@ impl Game {
         }
     }
 
+    pub async fn get_many(db: &PgPool, user_id: Uuid) -> Result<Vec<Game>, HttpError> {
+        let games = sqlx::query_as::<_, Game>(
+            "SELECT *
+            FROM games
+            WHERE user_id = $1"
+        )
+            .bind(user_id)
+            .fetch_all(db)
+            .await?;
+
+        Ok(games)
+    }
+
     pub async fn get_full_game(
         db: &PgPool,
         id: Uuid,

+ 3 - 1
src/routes/game/game.rs

@@ -1,10 +1,12 @@
 use actix_web::web::ServiceConfig;
 use crate::controllers::game::{
     create,
-    get_one
+    get_one,
+    get_many
 };
 
 pub fn config(cfg: &mut ServiceConfig) {
     cfg.service(create::route);
     cfg.service(get_one::route);
+    cfg.service(get_many::route);
 }