From b7b3ae1850bd56c0f0b074b1b418b50ce8251967 Mon Sep 17 00:00:00 2001
From: Paolo Brasolin <paolo.brasolin@eurac.edu>
Date: Wed, 16 Mar 2022 21:13:46 +0100
Subject: [PATCH] refactor: #be api cleanup

---
 backend/src/api.ts | 21 ++++++++-------------
 1 file changed, 8 insertions(+), 13 deletions(-)

diff --git a/backend/src/api.ts b/backend/src/api.ts
index 628086d..6811973 100644
--- a/backend/src/api.ts
+++ b/backend/src/api.ts
@@ -6,7 +6,7 @@ import { connection } from "./db";
 // NOTE: see https://www.npmjs.com/package/fastify-plugin for TS plugin definition
 
 const ParamsSchema = Type.Object({
-  id: Type.String({ format: "uuid" }),
+  game_id: Type.String({ format: "uuid" }),
 });
 
 type ParamsType = Static<typeof ParamsSchema>;
@@ -19,6 +19,8 @@ const GameSchema = Type.Object({
 
 type GameType = Static<typeof GameSchema>;
 
+const GamePatchSchema = Type.Omit(GameSchema, ["id"]);
+
 const WordSchema = Type.Object({
   id: Type.String({ format: "uuid" }),
   image: Type.String(),
@@ -50,12 +52,7 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
       if (word === undefined) {
         reply.code(404).send();
       } else {
-        reply.code(200).send({
-          id: word.id,
-          image: word.image,
-          ocr_confidence: word.ocr_confidence,
-          ocr_transcript: word.ocr_transcript,
-        });
+        reply.code(200).send(word);
       }
     },
   });
@@ -65,7 +62,7 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
     Reply: GameType;
   }>({
     method: "GET",
-    url: "/games/:id",
+    url: "/games/:game_id",
     schema: {
       params: ParamsSchema,
       response: {
@@ -75,7 +72,7 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
     },
     handler: async (request, reply) => {
       const game = await connection<GameType>("games")
-        .where("id", request.params.id)
+        .where("id", request.params.game_id)
         .first();
       if (game === undefined) {
         reply.code(404).send();
@@ -105,15 +102,13 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
     },
   });
 
-  const GamePatchSchema = Type.Omit(GameSchema, ["id"]);
-
   fastify.route<{
     Params: ParamsType;
     Body: Static<typeof GamePatchSchema>;
     Reply: GameType;
   }>({
     method: "PATCH",
-    url: "/games/:id",
+    url: "/games/:game_id",
     schema: {
       params: ParamsSchema,
       body: GamePatchSchema,
@@ -123,7 +118,7 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
     },
     handler: async (request, reply) => {
       const game = await connection<GameType>("games")
-        .where("id", request.params.id)
+        .where("id", request.params.game_id)
         .first();
       if (game === undefined) {
         reply.code(404).send();
-- 
GitLab