Skip to content

Commit

Permalink
feat: update chapter & comic api
Browse files Browse the repository at this point in the history
  • Loading branch information
conganhhcmus committed Mar 14, 2024
1 parent 5169b4f commit 5abfe5a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/controllers/comics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
1 change: 1 addition & 0 deletions src/models/comics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
4 changes: 3 additions & 1 deletion src/routers/comics.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
};
17 changes: 16 additions & 1 deletion src/services/comics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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;
};
2 changes: 2 additions & 0 deletions src/types/comics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export interface ComicBaseResponse {
_id: string;
name: string;
thumbnail: string;
recommend: boolean;
status: number;
chapters: {
_id: string;
name: string;
Expand Down

0 comments on commit 5abfe5a

Please sign in to comment.