-
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(helper): ✨ add
BookDeleteHelper
for delete route
- Loading branch information
Showing
2 changed files
with
56 additions
and
9 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,50 @@ | ||
import { FastifyRedis } from '@fastify/redis' | ||
|
||
import { BookDocument } from '#config/models/Book' | ||
import PaprAudibleBookHelper from '#helpers/database/audible/PaprAudibleBookHelper' | ||
import RedisHelper from '#helpers/database/RedisHelper' | ||
|
||
export default class BookDeleteHelper { | ||
asin: string | ||
paprHelper: PaprAudibleBookHelper | ||
redisHelper: RedisHelper | ||
originalBook: BookDocument | null = null | ||
constructor(asin: string, redis: FastifyRedis | null) { | ||
this.asin = asin | ||
this.paprHelper = new PaprAudibleBookHelper(this.asin, { | ||
seedAuthors: undefined, | ||
update: undefined | ||
}) | ||
this.redisHelper = new RedisHelper(redis, 'book', this.asin) | ||
} | ||
|
||
async getBookFromPapr(): Promise<BookDocument | null> { | ||
return (await this.paprHelper.findOne()).data | ||
} | ||
|
||
/** | ||
* Actions to run when a deletion is requested | ||
*/ | ||
async deleteActions() { | ||
// 1. Delete the book from cache | ||
await this.redisHelper.deleteOne() | ||
|
||
// 2. Delete the book from DB | ||
return (await this.paprHelper.delete()).modified | ||
} | ||
|
||
/** | ||
* Main handler for the book delete route | ||
*/ | ||
async handler() { | ||
this.originalBook = await this.getBookFromPapr() | ||
|
||
// If the book is already present | ||
if (this.originalBook) { | ||
return this.deleteActions() | ||
} | ||
|
||
// If the book is not present | ||
return false | ||
} | ||
} |