-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): ✨ add
delete
route for chapters
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
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,33 @@ | ||
import { FastifyInstance } from 'fastify' | ||
|
||
import { RequestGeneric } from '#config/typing/requests' | ||
import ChapterDeleteHelper from '#helpers/routes/ChapterDeleteHelper' | ||
import SharedHelper from '#helpers/shared' | ||
|
||
async function _delete(fastify: FastifyInstance) { | ||
fastify.delete<RequestGeneric>('/books/:asin/chapters', async (request, reply) => { | ||
// Setup common helper first | ||
const commonHelpers = new SharedHelper() | ||
// First, check ASIN validity | ||
if (!commonHelpers.checkAsinValidity(request.params.asin)) { | ||
reply.code(400) | ||
throw new Error('Bad ASIN') | ||
} | ||
|
||
// Setup helper | ||
const { redis } = fastify | ||
const helper = new ChapterDeleteHelper(request.params.asin, redis) | ||
|
||
// Call helper handler | ||
const isHandled = await helper.handler() | ||
|
||
if (!isHandled) { | ||
reply.code(404) | ||
throw new Error(`${request.params.asin} not found in the database`) | ||
} | ||
|
||
return { message: `${request.params.asin} deleted` } | ||
}) | ||
} | ||
|
||
export default _delete |
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,49 @@ | ||
import { FastifyRedis } from '@fastify/redis' | ||
|
||
import { ChapterDocument } from '#config/models/Chapter' | ||
import PaprAudibleChapterHelper from '#helpers/database/audible/PaprAudibleChapterHelper' | ||
import RedisHelper from '#helpers/database/RedisHelper' | ||
|
||
export default class ChapterDeleteHelper { | ||
asin: string | ||
paprHelper: PaprAudibleChapterHelper | ||
redisHelper: RedisHelper | ||
originalChapter: ChapterDocument | null = null | ||
constructor(asin: string, redis: FastifyRedis | null) { | ||
this.asin = asin | ||
this.paprHelper = new PaprAudibleChapterHelper(this.asin, { | ||
update: undefined | ||
}) | ||
this.redisHelper = new RedisHelper(redis, 'chapter', this.asin) | ||
} | ||
|
||
async getChaptersFromPapr(): Promise<ChapterDocument | null> { | ||
return (await this.paprHelper.findOne()).data | ||
} | ||
|
||
/** | ||
* Actions to run when a deletion is requested | ||
*/ | ||
async deleteActions() { | ||
// 1. Delete the chapter from cache | ||
await this.redisHelper.deleteOne() | ||
|
||
// 2. Delete the chapter from DB | ||
return (await this.paprHelper.delete()).modified | ||
} | ||
|
||
/** | ||
* Main handler for the chapter delete route | ||
*/ | ||
async handler() { | ||
this.originalChapter = await this.getChaptersFromPapr() | ||
|
||
// If the chapter is already present | ||
if (this.originalChapter) { | ||
return this.deleteActions() | ||
} | ||
|
||
// If the chapter is not present | ||
return false | ||
} | ||
} |
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