Skip to content
Snippets Groups Projects
Commit bc827c16 authored by Paolo.Brasolin's avatar Paolo.Brasolin
Browse files

feat: #be switch to shallow routing

parent 1ebd42f3
No related branches found
No related tags found
No related merge requests found
...@@ -5,19 +5,10 @@ import { connection } from "./db"; ...@@ -5,19 +5,10 @@ import { connection } from "./db";
// NOTE: see https://www.npmjs.com/package/fastify-plugin for TS plugin definition // NOTE: see https://www.npmjs.com/package/fastify-plugin for TS plugin definition
const GameParamsSchema = Type.Object({ const IdInParamsSchema = Type.Object({
game_id: Type.String({ format: "uuid" }), id: Type.String({ format: "uuid" }),
});
type GameParamsType = Static<typeof GameParamsSchema>;
const ClueParamsSchema = Type.Object({
game_id: Type.String({ format: "uuid" }),
clue_id: Type.String({ format: "uuid" }),
}); });
type ClueParamsType = Static<typeof ClueParamsSchema>;
const ClueSchema = Type.Object({ const ClueSchema = Type.Object({
id: Type.Readonly(Type.String({ format: "uuid" })), id: Type.Readonly(Type.String({ format: "uuid" })),
game_id: Type.Readonly(Type.String({ format: "uuid" })), game_id: Type.Readonly(Type.String({ format: "uuid" })),
...@@ -64,6 +55,8 @@ const WordSchema = Type.Object({ ...@@ -64,6 +55,8 @@ const WordSchema = Type.Object({
type WordType = Static<typeof WordSchema>; type WordType = Static<typeof WordSchema>;
//==============================================================================
const apiPlugin: FastifyPluginCallback = (fastify, options, next) => { const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
fastify.route<{ fastify.route<{
Reply: WordType; Reply: WordType;
...@@ -92,13 +85,13 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => { ...@@ -92,13 +85,13 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
}); });
fastify.route<{ fastify.route<{
Params: GameParamsType; Params: Static<typeof IdInParamsSchema>;
Reply: GameType; Reply: GameType;
}>({ }>({
method: "GET", method: "GET",
url: "/games/:game_id", url: "/games/:id",
schema: { schema: {
params: GameParamsSchema, params: IdInParamsSchema,
response: { response: {
200: GameSchema, 200: GameSchema,
404: {}, // TODO: JSend error 404: {}, // TODO: JSend error
...@@ -106,7 +99,7 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => { ...@@ -106,7 +99,7 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
}, },
handler: async (request, reply) => { handler: async (request, reply) => {
const game = await connection<GameType>("games") const game = await connection<GameType>("games")
.where("id", request.params.game_id) .where("id", request.params.id)
.first(); .first();
if (game === undefined) { if (game === undefined) {
reply.code(404).send(); reply.code(404).send();
...@@ -137,14 +130,14 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => { ...@@ -137,14 +130,14 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
}); });
fastify.route<{ fastify.route<{
Params: GameParamsType; Params: Static<typeof IdInParamsSchema>;
Body: Static<typeof GamePatchSchema>; Body: Static<typeof GamePatchSchema>;
Reply: GameType; Reply: GameType;
}>({ }>({
method: "PATCH", method: "PATCH",
url: "/games/:game_id", url: "/games/:id",
schema: { schema: {
params: GameParamsSchema, params: IdInParamsSchema,
body: GamePatchSchema, body: GamePatchSchema,
response: { response: {
200: GameSchema, 200: GameSchema,
...@@ -152,7 +145,7 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => { ...@@ -152,7 +145,7 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
}, },
handler: async (request, reply) => { handler: async (request, reply) => {
const game = await connection<GameType>("games") const game = await connection<GameType>("games")
.where("id", request.params.game_id) .where("id", request.params.id)
.first(); .first();
if (game === undefined) { if (game === undefined) {
reply.code(404).send(); reply.code(404).send();
...@@ -166,14 +159,14 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => { ...@@ -166,14 +159,14 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
}); });
fastify.route<{ fastify.route<{
Params: Static<typeof GameParamsSchema>; Params: Static<typeof IdInParamsSchema>;
Body: Static<typeof CluePostSchema>; Body: Static<typeof CluePostSchema>;
Reply: ClueType; Reply: ClueType;
}>({ }>({
method: "POST", method: "POST",
url: "/games/:game_id/clues", url: "/games/:id/clues",
schema: { schema: {
params: GameParamsSchema, params: IdInParamsSchema,
body: CluePostSchema, body: CluePostSchema,
response: { response: {
200: ClueSchema, 200: ClueSchema,
...@@ -183,14 +176,14 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => { ...@@ -183,14 +176,14 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
}); });
fastify.route<{ fastify.route<{
Params: Static<typeof ClueParamsSchema>; Params: Static<typeof IdInParamsSchema>;
Body: Static<typeof CluePatchSchema>; Body: Static<typeof CluePatchSchema>;
Reply: ClueType; Reply: ClueType;
}>({ }>({
method: "PATCH", method: "PATCH",
url: "/games/:game_id/clues/:clue_id", url: "/clues/:id",
schema: { schema: {
params: ClueParamsSchema, params: IdInParamsSchema,
body: CluePatchSchema, body: CluePatchSchema,
response: { response: {
200: ClueSchema, 200: ClueSchema,
...@@ -200,14 +193,14 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => { ...@@ -200,14 +193,14 @@ const apiPlugin: FastifyPluginCallback = (fastify, options, next) => {
}); });
fastify.route<{ fastify.route<{
Params: Static<typeof GameParamsSchema>; Params: Static<typeof IdInParamsSchema>;
Body: Static<typeof ShotPostSchema>; Body: Static<typeof ShotPostSchema>;
Reply: ShotType; Reply: ShotType;
}>({ }>({
method: "POST", method: "POST",
url: "/games/:game_id/shots", url: "/games/:id/shots",
schema: { schema: {
params: GameParamsSchema, params: IdInParamsSchema,
body: ShotPostSchema, body: ShotPostSchema,
response: { response: {
200: ShotSchema, 200: ShotSchema,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment