-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(game): get a game by id route (#29)
- Loading branch information
1 parent
052447d
commit b636d5a
Showing
18 changed files
with
3,502 additions
and
2,549 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const { OFF } = require("../../constants"); | ||
|
||
const pipeFilesOverride = Object.freeze({ | ||
files: ["*.pipe.ts"], | ||
rules: { "class-methods-use-this": OFF }, | ||
}); | ||
|
||
module.exports = { pipeFilesOverride }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum API_RESOURCES { | ||
GAMES = "games", | ||
} | ||
|
||
export { API_RESOURCES }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { API_RESOURCES } from "../enums/api.enum"; | ||
|
||
function getResourceSingularForm(resource: API_RESOURCES): string { | ||
const resourceSingularForms: Record<API_RESOURCES, string> = { [API_RESOURCES.GAMES]: "game" }; | ||
return resourceSingularForms[resource]; | ||
} | ||
|
||
export { getResourceSingularForm }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { PipeTransform } from "@nestjs/common"; | ||
import { BadRequestException, Injectable } from "@nestjs/common"; | ||
import { Types } from "mongoose"; | ||
|
||
@Injectable() | ||
class ValidateMongoId implements PipeTransform<string> { | ||
public transform(value: string): string { | ||
if (Types.ObjectId.isValid(value)) { | ||
return value; | ||
} | ||
throw new BadRequestException("Validation failed (Mongo ObjectId is expected)"); | ||
} | ||
} | ||
|
||
export { ValidateMongoId }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { NotFoundException } from "@nestjs/common"; | ||
import { ResourceNotFoundError } from "../types/error.type"; | ||
|
||
function getControllerRouteError(err: unknown): unknown { | ||
if (err instanceof ResourceNotFoundError) { | ||
return new NotFoundException(err.message); | ||
} | ||
return err; | ||
} | ||
|
||
export { getControllerRouteError }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { upperFirst } from "lodash"; | ||
import type { API_RESOURCES } from "../../api/enums/api.enum"; | ||
import { getResourceSingularForm } from "../../api/helpers/api.helper"; | ||
|
||
class ResourceNotFoundError extends Error { | ||
public constructor(resource: API_RESOURCES, id: string) { | ||
const resourceSingularForm = getResourceSingularForm(resource); | ||
const message = `${upperFirst(resourceSingularForm)} with id "${id}" not found`; | ||
super(message); | ||
} | ||
} | ||
|
||
export { ResourceNotFoundError }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { API_RESOURCES } from "../../../../../../src/_shared/api/enums/api.enum"; | ||
import { getResourceSingularForm } from "../../../../../../src/_shared/api/helpers/api.helper"; | ||
|
||
describe("API Helper", () => { | ||
describe("getResourceSingularForm", () => { | ||
it.each<{ resource: API_RESOURCES; singular: string }>([{ resource: API_RESOURCES.GAMES, singular: "game" }])("should return $singular when called with $resource [#$#].", ({ resource, singular }) => { | ||
expect(getResourceSingularForm(resource)).toBe(singular); | ||
}); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
tests/unit/specs/_shared/api/pipes/validate-mongo-id.pipe.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { ValidateMongoId } from "../../../../../../src/_shared/api/pipes/validate-mongo-id.pipe"; | ||
|
||
describe("Validate MongoId Pipe", () => { | ||
const pipe = new ValidateMongoId(); | ||
|
||
describe("transform", () => { | ||
it("should return the value as is when value is a correct MongoId.", () => { | ||
const validObjectId = faker.database.mongodbObjectId(); | ||
expect(pipe.transform(validObjectId)).toBe(validObjectId); | ||
}); | ||
|
||
it("should throw an error when value is a incorrect MongoId.", () => { | ||
expect(() => pipe.transform("123")).toThrow("Validation failed (Mongo ObjectId is expected)"); | ||
}); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
tests/unit/specs/_shared/error/helpers/error.helper.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { NotFoundException } from "@nestjs/common"; | ||
import { API_RESOURCES } from "../../../../../../src/_shared/api/enums/api.enum"; | ||
import { getControllerRouteError } from "../../../../../../src/_shared/error/helpers/error.helper"; | ||
import { ResourceNotFoundError } from "../../../../../../src/_shared/error/types/error.type"; | ||
|
||
describe("Error Helper", () => { | ||
describe("getControllerRouteError", () => { | ||
it("should return the error as is when it doesn't have to be transformed.", () => { | ||
const error = new Error("123"); | ||
expect(getControllerRouteError(error)).toStrictEqual(error); | ||
}); | ||
|
||
it("should return a NotFoundExceptionError when error is ResourceNotFoundError.", () => { | ||
const id = "123"; | ||
const error = new ResourceNotFoundError(API_RESOURCES.GAMES, id); | ||
const result = getControllerRouteError(error); | ||
expect(result instanceof NotFoundException).toBe(true); | ||
expect((result as NotFoundException).message).toBe(`Game with id "${id}" not found`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters