modify delete comment
This commit is contained in:
parent
50dbb06b29
commit
427997f1cb
@ -1,16 +1,11 @@
|
|||||||
import {
|
import { Body, Controller, Delete, Get, Post, Query, SerializeOptions } from '@nestjs/common';
|
||||||
Body,
|
|
||||||
Controller,
|
|
||||||
Delete,
|
|
||||||
Get,
|
|
||||||
Param,
|
|
||||||
ParseUUIDPipe,
|
|
||||||
Post,
|
|
||||||
Query,
|
|
||||||
SerializeOptions,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
|
|
||||||
import { CreateCommentDto, QueryCommentDto, QueryCommentTreeDto } from '../dtos/comment.dto';
|
import {
|
||||||
|
CreateCommentDto,
|
||||||
|
DeleteCommentDto,
|
||||||
|
QueryCommentDto,
|
||||||
|
QueryCommentTreeDto,
|
||||||
|
} from '../dtos/comment.dto';
|
||||||
import { CommentService } from '../services';
|
import { CommentService } from '../services';
|
||||||
|
|
||||||
@Controller('comment')
|
@Controller('comment')
|
||||||
@ -38,9 +33,9 @@ export class CommentController {
|
|||||||
return this.service.create(data);
|
return this.service.create(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
@Delete()
|
||||||
@SerializeOptions({ groups: ['comment-detail'] })
|
@SerializeOptions({ groups: ['comment-detail'] })
|
||||||
async delete(@Param('id', new ParseUUIDPipe()) id: string) {
|
async delete(@Body() data: DeleteCommentDto) {
|
||||||
return this.service.delete(id);
|
return this.service.delete(data.ids);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,13 +46,13 @@ export class QueryCommentTreeDto extends PickType(QueryCommentDto, ['post']) {}
|
|||||||
|
|
||||||
@DtoValidation()
|
@DtoValidation()
|
||||||
export class CreateCommentDto {
|
export class CreateCommentDto {
|
||||||
@MaxLength(1000, { message: '' })
|
@MaxLength(1000, { message: 'The length of the comment content cannot exceed $constraint1' })
|
||||||
@IsNotEmpty({ message: '' })
|
@IsNotEmpty({ message: 'Comment content cannot be empty' })
|
||||||
body: string;
|
body: string;
|
||||||
|
|
||||||
@IsDataExist(PostEntity, { message: 'The post does not exist' })
|
@IsDataExist(PostEntity, { message: 'The post does not exist' })
|
||||||
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
@IsUUID(undefined, { message: 'The ID format is incorrect' })
|
||||||
@IsDefined({ message: 'The ID must be specified' })
|
@IsDefined({ message: 'The post ID must be specified' })
|
||||||
post: string;
|
post: string;
|
||||||
|
|
||||||
@IsDataExist(CommentEntity, { message: 'The parent comment does not exist' })
|
@IsDataExist(CommentEntity, { message: 'The parent comment does not exist' })
|
||||||
@ -62,3 +62,11 @@ export class CreateCommentDto {
|
|||||||
@Transform(({ value }) => (value === 'null' ? null : value))
|
@Transform(({ value }) => (value === 'null' ? null : value))
|
||||||
parent?: string;
|
parent?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DtoValidation()
|
||||||
|
export class DeleteCommentDto {
|
||||||
|
@IsDataExist(CommentEntity)
|
||||||
|
@IsUUID(undefined, { each: true, always: true, message: 'The ID format is incorrect' })
|
||||||
|
@IsDefined({ each: true, message: 'The ID must be specified' })
|
||||||
|
ids: string[];
|
||||||
|
}
|
||||||
|
@ -2,7 +2,7 @@ import { ForbiddenException, Injectable } from '@nestjs/common';
|
|||||||
|
|
||||||
import { isNil } from 'lodash';
|
import { isNil } from 'lodash';
|
||||||
|
|
||||||
import { EntityNotFoundError, SelectQueryBuilder } from 'typeorm';
|
import { EntityNotFoundError, In, SelectQueryBuilder } from 'typeorm';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
CreateCommentDto,
|
CreateCommentDto,
|
||||||
@ -63,9 +63,9 @@ export class CommentService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id: string) {
|
async delete(ids: string[]) {
|
||||||
const comment = await this.repository.findOneOrFail({ where: { id: id ?? null } });
|
const comments = await this.repository.find({ where: { id: In(ids) } });
|
||||||
return this.repository.remove(comment);
|
return this.repository.remove(comments);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async getPost(id: string) {
|
protected async getPost(id: string) {
|
||||||
|
@ -6,6 +6,7 @@ import {
|
|||||||
ValidationOptions,
|
ValidationOptions,
|
||||||
registerDecorator,
|
registerDecorator,
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
|
import { isArray, isNil } from 'lodash';
|
||||||
import { ObjectType, Repository, DataSource } from 'typeorm';
|
import { ObjectType, Repository, DataSource } from 'typeorm';
|
||||||
|
|
||||||
type Condition = {
|
type Condition = {
|
||||||
@ -18,9 +19,11 @@ type Condition = {
|
|||||||
export class DataExistConstraint implements ValidatorConstraintInterface {
|
export class DataExistConstraint implements ValidatorConstraintInterface {
|
||||||
constructor(private dataSource: DataSource) {}
|
constructor(private dataSource: DataSource) {}
|
||||||
|
|
||||||
|
errorValues: any[] = [];
|
||||||
|
|
||||||
async validate(value: any, validationArguments?: ValidationArguments) {
|
async validate(value: any, validationArguments?: ValidationArguments) {
|
||||||
let repo: Repository<any>;
|
let repo: Repository<any>;
|
||||||
if (!value) {
|
if (isNil(value)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
let map = 'id';
|
let map = 'id';
|
||||||
@ -30,6 +33,22 @@ export class DataExistConstraint implements ValidatorConstraintInterface {
|
|||||||
} else {
|
} else {
|
||||||
repo = this.dataSource.getRepository(validationArguments.constraints[0]);
|
repo = this.dataSource.getRepository(validationArguments.constraints[0]);
|
||||||
}
|
}
|
||||||
|
if (isArray(value)) {
|
||||||
|
const values = value as any[];
|
||||||
|
const validationResults = await Promise.all(
|
||||||
|
values.map(async (val) => {
|
||||||
|
if (isNil(val)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const item = await repo.findOne({ where: { [map]: val } });
|
||||||
|
if (isNil(item)) {
|
||||||
|
this.errorValues.push(val);
|
||||||
|
}
|
||||||
|
return !isNil(item);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return validationResults.every((isValid) => isValid);
|
||||||
|
}
|
||||||
const item = await repo.findOne({ where: { [map]: value } });
|
const item = await repo.findOne({ where: { [map]: value } });
|
||||||
return !!item;
|
return !!item;
|
||||||
}
|
}
|
||||||
@ -37,6 +56,11 @@ export class DataExistConstraint implements ValidatorConstraintInterface {
|
|||||||
if (!validationArguments.constraints[0]) {
|
if (!validationArguments.constraints[0]) {
|
||||||
return 'Model not been specified!';
|
return 'Model not been specified!';
|
||||||
}
|
}
|
||||||
|
if (this.errorValues.length > 0) {
|
||||||
|
return `All instance of ${
|
||||||
|
validationArguments.constraints[0].name
|
||||||
|
} must been exists in databse!Errors are ${this.errorValues.join(',')}`;
|
||||||
|
}
|
||||||
return `All instance of ${validationArguments.constraints[0].name} must been exists in databse!`;
|
return `All instance of ${validationArguments.constraints[0].name} must been exists in databse!`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user