-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(anime): add endpoints for anime information and summary (#122)
Introduces two new endpoints to access anime information: 1. /api/v1/anime/:id 2. /api/v1/anime/:id/summary
- Loading branch information
1 parent
b0320ba
commit 3da93ed
Showing
4 changed files
with
140 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import type { Request, Response } from "express"; | ||
import { prisma } from "~/libs/prisma"; | ||
|
||
export const getAnimeInformation = async (req: Request, res: Response) => { | ||
const animeId = req.params?.id; | ||
|
||
if (!animeId) { | ||
return res.status(400).json({ | ||
status: "error", | ||
error: { | ||
code: 400, | ||
message: "Not a valid 'animeId'", | ||
}, | ||
}); | ||
} | ||
|
||
const anime = await prisma.anime.findUnique({ | ||
where: { id: Number.parseInt(animeId) }, | ||
include: { animeCharacters: true }, | ||
}); | ||
|
||
if (!anime) { | ||
return res.status(404).json({ | ||
status: "error", | ||
error: { | ||
code: 404, | ||
message: "No matching anime found", | ||
}, | ||
}); | ||
} | ||
|
||
const characters = anime.animeCharacters.map((c) => ({ | ||
id: c.id, | ||
name: c.name, | ||
})); | ||
return res.json({ | ||
status: "success", | ||
data: { | ||
id: anime.id, | ||
name: anime.name, | ||
summary: anime.synopsis, | ||
episodesCount: anime.episodeCount, | ||
characters: characters, | ||
}, | ||
}); | ||
}; | ||
|
||
export const getAnimeSummary = async (req: Request, res: Response) => { | ||
const animeId = req.params?.id; | ||
|
||
if (!animeId) { | ||
return res.status(400).json({ | ||
status: "error", | ||
error: { | ||
code: 400, | ||
message: "Not a valid 'animeId'", | ||
}, | ||
}); | ||
} | ||
|
||
const anime = await prisma.anime.findUnique({ | ||
where: { id: Number.parseInt(animeId) }, | ||
include: { animeCharacters: true }, | ||
}); | ||
|
||
if (!anime) { | ||
return res.status(404).json({ | ||
status: "error", | ||
error: { | ||
code: 404, | ||
message: "No matching anime found", | ||
}, | ||
}); | ||
} | ||
|
||
return res.json({ | ||
status: "success", | ||
data: { | ||
id: anime.id, | ||
name: anime.name, | ||
summary: anime.synopsis, | ||
}, | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,51 @@ | ||
import { expect, test } from "vitest"; | ||
import { describe, expect, test } from "vitest"; | ||
import { isProtectedEndpoint } from "../src/libs/auth.util"; | ||
|
||
const BASE_PATH = "/api/v1/"; | ||
|
||
test("Should pass on protected API urls", () => { | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random?anime=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random?character=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/?anime=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/?character=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes?anime=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes?character=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/?anime=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/?character=naruto`)).toBeTruthy(); | ||
}); | ||
describe("Query param based endpoints", () => { | ||
test("Should pass on protected API urls", () => { | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random?anime=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random?character=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/?anime=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/?character=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes?anime=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes?character=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/?anime=naruto`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/?character=naruto`)).toBeTruthy(); | ||
}); | ||
|
||
test("should fail on unprotected/free API urls", () => { | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes`)).toBeFalsy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/`)).toBeFalsy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random`)).toBeFalsy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/`)).toBeFalsy(); | ||
}); | ||
test("should fail on unprotected based API urls", () => { | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes`)).toBeFalsy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/`)).toBeFalsy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random`)).toBeFalsy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/`)).toBeFalsy(); | ||
}); | ||
|
||
test("Should pass on valid protected API urls with additional params", () => { | ||
expect( | ||
isProtectedEndpoint(`${BASE_PATH}quotes/random?anime=naruto&character=naruto`), | ||
).toBeTruthy(); | ||
expect( | ||
isProtectedEndpoint(`${BASE_PATH}quotes/random/?character=naruto&anime=naruto`), | ||
).toBeTruthy(); | ||
expect( | ||
isProtectedEndpoint(`${BASE_PATH}quotes/random/?character=naruto&anime=naruto&page=2`), | ||
).toBeTruthy(); | ||
}); | ||
|
||
test("Should pass on valid protected API urls with additional params", () => { | ||
expect( | ||
isProtectedEndpoint(`${BASE_PATH}quotes/random?anime=naruto&character=naruto`), | ||
).toBeTruthy(); | ||
expect( | ||
isProtectedEndpoint(`${BASE_PATH}quotes/random/?character=naruto&anime=naruto`), | ||
).toBeTruthy(); | ||
expect( | ||
isProtectedEndpoint(`${BASE_PATH}quotes/random/?character=naruto&anime=naruto&page=2`), | ||
).toBeTruthy(); | ||
test("Should pass on protected API urls with additional unknown params", () => { | ||
// Accepts additional not pre-defined params like `foo` here but only in the presence | ||
// of a premium query param which is `character` in this case below. | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/?character=naruto&foo=bar`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/?foo=bar`)).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
test("Should pass on protected API urls with additional unknown params", () => { | ||
// Accepts additional not pre-defined params like `foo` here but only in the presence | ||
// of a premium query param which is `character` in this case below. | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/?character=naruto&foo=bar`)).toBeTruthy(); | ||
expect(isProtectedEndpoint(`${BASE_PATH}quotes/random/?foo=bar`)).toBeFalsy(); | ||
describe("Standard path based endpoints", () => { | ||
test("Should pass on protected API urls", () => { | ||
expect(isProtectedEndpoint(`${BASE_PATH}anime/1`)); | ||
expect(isProtectedEndpoint(`${BASE_PATH}anime/1/`)); | ||
expect(isProtectedEndpoint(`${BASE_PATH}anime/foo`)); | ||
}); | ||
}); |