From 5abfe5a386807b8290dd9c15a382b64e3127812b Mon Sep 17 00:00:00 2001 From: conganhhcmus Date: Fri, 15 Mar 2024 01:53:25 +0700 Subject: [PATCH] feat: update chapter & comic api --- src/controllers/comics.ts | 10 +++++++++- src/models/comics.ts | 1 + src/routers/comics.ts | 4 +++- src/services/comics.ts | 17 ++++++++++++++++- src/types/comics.ts | 2 ++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/controllers/comics.ts b/src/controllers/comics.ts index 12c6722..c4ed860 100644 --- a/src/controllers/comics.ts +++ b/src/controllers/comics.ts @@ -50,7 +50,15 @@ export const updateComic = async (req: Request, res: Response) => { export const getComicInfo = async (req: Request, res: Response) => { const { id } = req.params; - const result = await comicService.getComicInfoById(id); + const result = await comicService.getComicById(id); + + return res.json(result); +}; + +export const deleteComic = async (req: Request, res: Response) => { + const { id } = req.params; + + const result = await comicService.deleteComicById(id); return res.json(result); }; diff --git a/src/models/comics.ts b/src/models/comics.ts index 5a994ca..939e7f9 100644 --- a/src/models/comics.ts +++ b/src/models/comics.ts @@ -9,6 +9,7 @@ const ComicSchema = new mongoose.Schema({ follower: { type: Number, required: false, default: 0 }, genres: { type: [Types.ObjectId], required: true }, status: { type: Number, required: false, default: 0 }, + recommend: { type: Boolean, required: false, default: false }, totalViews: { type: Number, required: false, default: 0 }, author: { type: String, required: false, default: 0 }, createTime: { type: Date, required: true }, diff --git a/src/routers/comics.ts b/src/routers/comics.ts index 5bcb871..9383805 100644 --- a/src/routers/comics.ts +++ b/src/routers/comics.ts @@ -1,4 +1,4 @@ -import { addComic, getComicInfo, recentUpdatedComics, recommendComics, topComics, updateComic } from './../controllers/comics'; +import { addComic, deleteComic, getComicInfo, recentUpdatedComics, recommendComics, topComics, updateComic } from './../controllers/comics'; import { Router } from 'express'; import { searchComics } from '@/controllers/comics'; import { verifyAccessToken } from '@/middlewares/authToken'; @@ -107,4 +107,6 @@ export default (router: Router) => { router.put('/comics/:id', verifyAccessToken, isAdmin, updateComic); router.get('/comics/:id', getComicInfo); + + router.delete('/comics/:id', verifyAccessToken, isAdmin, deleteComic); }; diff --git a/src/services/comics.ts b/src/services/comics.ts index befbd00..29b87e6 100644 --- a/src/services/comics.ts +++ b/src/services/comics.ts @@ -17,6 +17,8 @@ export const getRecommendComics = (page: number): ComicBaseData => { result = result.concat({ _id: index.toString(), name: `Test ${index}`, + status: 0, + recommend: true, thumbnail: `https://source.unsplash.com/random/300x200?sig=${index}`, chapters: [ { @@ -55,6 +57,8 @@ export const getRecentUpdatedComics = (page: number): ComicData => { _id: index.toString(), name: `Test ${index}`, otherName: [], + status: 0, + recommend: true, description: `description ${index}`, thumbnail: `https://source.unsplash.com/random/300x200?sig=${index}`, chapters: [ @@ -101,6 +105,8 @@ export const getTopComics = (type: string, page: number): ComicData => { _id: index.toString(), name: `Test ${index}`, otherName: [], + recommend: false, + status: 0, description: `description ${index}`, thumbnail: `https://source.unsplash.com/random/300x200?sig=${index}`, chapters: [ @@ -151,8 +157,17 @@ export const updateComicById = async (id: string, comic: Comic) => { return result; }; -export const getComicInfoById = async (id: string) => { +export const getComicById = async (id: string) => { const result = await comicRepository.getComicById(id); return result; }; + +export const deleteComicById = async (id: string) => { + // remove all chapter + await chapterRepository.deleteChapterByComicId(id); + + const result = await comicRepository.deleteComicById(id); + + return result; +}; diff --git a/src/types/comics.ts b/src/types/comics.ts index 1f6ce5d..a8167ff 100644 --- a/src/types/comics.ts +++ b/src/types/comics.ts @@ -2,6 +2,8 @@ export interface ComicBaseResponse { _id: string; name: string; thumbnail: string; + recommend: boolean; + status: number; chapters: { _id: string; name: string;