Skip to content

Commit

Permalink
Merge pull request #111 from Roger13579/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
y0000ga authored May 17, 2024
2 parents 78443e0 + 94de3e3 commit a36e680
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/controller/commentController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextFunction, Response } from 'express';
import { NewCommentDTO } from '../dto/newCommentDto';
import { CommentService } from '../service/commentService';
import { ICommentProductReq } from '../types/comment.type';
import { ICommentProductReq, IDeleteCommentsReq } from '../types/comment.type';
import { CustomResponseType } from '../types/customResponseType';
import { BaseController } from './baseController';

Expand All @@ -24,4 +24,20 @@ export class CommentController extends BaseController {
{ comment },
);
};

public readonly deleteComments = async (
req: IDeleteCommentsReq,
res: Response,
next: NextFunction,
) => {
const info = await this.commentService.deleteComments(
req.body.commentIds,
next,
);
return this.formatResponse(
CustomResponseType.OK_MESSAGE,
CustomResponseType.OK,
info ? { deletedCount: info.deletedCount } : {},
);
};
}
4 changes: 4 additions & 0 deletions src/repository/commentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ export class CommentRepository {
const { comment } = newCommentDto;
return await CommentModel.create(comment);
};

public deleteComments = async (ids: string[]) => {
return await CommentModel.deleteMany({ _id: { $in: ids } });
};
}
32 changes: 32 additions & 0 deletions src/routes/commentRoute.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CommentController } from '../controller/commentController';
import { IsAdmin } from '../middleware/isAdmin';
import { UserVerify } from '../middleware/userVerify';
import { CreateCommentPipe } from '../validator/comment/createComment.pipe';
import { DeleteCommentsPipe } from '../validator/comment/deleteComments.pipe';
import { BaseRoute } from './baseRoute';

export class CommentRoute extends BaseRoute {
Expand Down Expand Up @@ -44,5 +46,35 @@ export class CommentRoute extends BaseRoute {
this.usePipe(CreateCommentPipe),
this.responseHandler(this.controller.commentProduct),
);

this.router.delete(
'/v1/comment',
/**
* #swagger.tags = ['Comment']
* #swagger.summary = '批次刪除評論'
* #swagger.security=[{"Bearer": []}]
*/
/*
#swagger.parameters['obj'] = {
in: 'body',
description: '欲刪除的評論 id 列表',
schema: {
$ref:"#/definitions/CustomDeleteCommentsObj"
}
}
*/
/*
#swagger.responses[200] = {
description:'OK',
schema:{
$ref: "#/definitions/DeleteSuccess"
}
}
*/
UserVerify,
IsAdmin,
this.usePipe(DeleteCommentsPipe),
this.responseHandler(this.controller.deleteComments),
);
}
}
2 changes: 1 addition & 1 deletion src/routes/productRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export class ProductRoute extends BaseRoute {
#swagger.responses[200] = {
description:'OK',
schema:{
$ref: "#/definitions/DeleteProductsSuccess"
$ref: "#/definitions/DeleteSuccess"
}
}
*/
Expand Down
14 changes: 14 additions & 0 deletions src/service/commentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ export class CommentService {
CustomResponseType.EXISTED_COMMENT_MESSAGE,
CustomResponseType.EXISTED_COMMENT,
);
return;
}
return next(err);
});
};

public deleteComments = async (ids: string[], next: NextFunction) => {
return await this.commentRepository.deleteComments(ids).catch((err) => {
if (err.name === 'CastError') {
throwError(
CustomResponseType.INVALID_DELETE_COMMENT_MESSAGE + 'commentId',
CustomResponseType.INVALID_DELETE_COMMENT,
);
return;
}
return next(err);
});
Expand Down
13 changes: 12 additions & 1 deletion src/service/productService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import { checkDateOrder } from '../utils/common';
import { AppError, createErrorMsg, throwError } from '../utils/errorHandler';
import { HttpStatus } from '../types/responseType';
import { EditProductDTO } from '../dto/editProductsDto';
import { CommentRepository } from '../repository/commentRepository';

const logger = log4js.getLogger(`ProductRepository`);

export class ProductService {
private readonly productRepository: ProductRepository =
new ProductRepository();

private readonly commentRepository: CommentRepository =
new CommentRepository();

private readonly defaultProjection = {
_id: 1,
title: 1,
Expand Down Expand Up @@ -181,7 +185,14 @@ export class ProductService {
};

public deleteProducts = async (ids: string[]) => {
return this.productRepository.deleteProducts(ids);
const { deletedCount } = await this.productRepository.deleteProducts(ids);

if (deletedCount > 0) {
// 真的有商品刪掉了,就要去把相應的 comment 刪掉
await this.commentRepository.deleteComments(ids);
}

return { deletedCount };
};

public editProducts = async (editProductDto: EditProductDTO) => {
Expand Down
18 changes: 18 additions & 0 deletions src/swagger/definition/comment/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@ export const CustomCreateCommentObj = {
},
},
};

/**
* @description 自己定義的 definition,適用於包含刪除評論 id 列表 request.body
*/
export const CustomDeleteCommentsObj = {
type: 'object',
required: ['commentIds'],
properties: {
commentIds: {
type: 'array',
items: {
type: 'string',
example: 'thisIsAnId',
description: '要刪除的評論 id',
},
},
},
};
8 changes: 8 additions & 0 deletions src/swagger/definition/comment/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ export const CreateCommentSuccess = {
},
},
};

export const DeleteSuccess = {
$status: CustomResponseType.OK,
$message: CustomResponseType.OK_MESSAGE,
$data: {
$deletedCount: 1,
},
};
6 changes: 6 additions & 0 deletions src/types/comment.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ export interface ICommentProductReq extends IUserReq {
status: Status;
};
}

export interface IDeleteCommentsReq extends IUserReq {
body: {
commentIds: string[];
};
}
4 changes: 4 additions & 0 deletions src/types/customResponseType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ export const enum CustomResponseType {

EXISTED_COMMENT = '6519',
EXISTED_COMMENT_MESSAGE = '該使用者已經對該商品進行評論',

INVALID_COMMENT_PRODUCT = '6520',
INVALID_COMMENT_PRODUCT_MESSAGE = '無效的新增評論:',

INVALID_DELETE_COMMENT = '6521',
INVALID_DELETE_COMMENT_MESSAGE = '無效的刪除評論:',
}
29 changes: 29 additions & 0 deletions src/validator/comment/deleteComments.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { body } from 'express-validator';
import { PipeBase } from '../pipe.base';
import { CustomResponseType } from '../../types/customResponseType';

export class DeleteCommentsPipe extends PipeBase {
public transform = () => [
body('commentIds')
.exists()
.withMessage(
CustomResponseType.INVALID_DELETE_COMMENT_MESSAGE + 'commentIds',
)
.isArray()
.custom(this.isNotEmptyArray)
.withMessage(
CustomResponseType.INVALID_DELETE_COMMENT_MESSAGE + 'commentIds',
),
body('commentIds.*')
.exists()
.isString()
.withMessage(
CustomResponseType.INVALID_DELETE_COMMENT_MESSAGE + 'commentId',
),
this.validationHandler,
];

constructor() {
super();
}
}

0 comments on commit a36e680

Please sign in to comment.