Archived
add issue type and comments
This commit is contained in:
9 files changed
+85
-4
No files matched your search
@@ -0,0 +1,8 @@
|
||||
import { IsString, MaxLength, MinLength } from 'class-validator';
|
||||
|
||||
export class CreateIssueCommentDto {
|
||||
@IsString()
|
||||
@MinLength(1)
|
||||
@MaxLength(255)
|
||||
content: string;
|
||||
}
|
||||
@@ -1,4 +1,11 @@
|
||||
import { IsNumber, IsString, MaxLength, MinLength } from 'class-validator';
|
||||
import {
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsString,
|
||||
MaxLength,
|
||||
MinLength,
|
||||
} from 'class-validator';
|
||||
import { IssueType } from '../entities/issue-type.enum';
|
||||
|
||||
export class CreateIssueDto {
|
||||
@IsString()
|
||||
@@ -11,6 +18,9 @@ export class CreateIssueDto {
|
||||
@MaxLength(255)
|
||||
description: string;
|
||||
|
||||
@IsEnum(IssueType)
|
||||
type: IssueType;
|
||||
|
||||
@IsNumber()
|
||||
latitude: number;
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { OmitType, PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateIssueDto } from './create-issue.dto';
|
||||
|
||||
export class UpdateIssueDto extends PartialType(CreateIssueDto) {}
|
||||
export class UpdateIssueDto extends PartialType(
|
||||
OmitType(CreateIssueDto, ['type']),
|
||||
) {}
|
||||
@@ -0,0 +1,30 @@
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
ManyToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
import { Issue } from './issue.entity';
|
||||
|
||||
@Entity('issue_comments')
|
||||
export class IssueComment {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@ManyToOne(() => Issue, { onDelete: 'CASCADE', lazy: true })
|
||||
issue: Promise<Issue>;
|
||||
|
||||
@Column()
|
||||
content: string;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
toDto() {
|
||||
return {
|
||||
content: this.content,
|
||||
createdAt: this.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export enum IssueType {
|
||||
problem = 'PROBLEM',
|
||||
suggestion = 'SUGGESTION',
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
PrimaryGeneratedColumn,
|
||||
} from 'typeorm';
|
||||
import { IssueStatus } from './issue-status.enum';
|
||||
import { IssueType } from './issue-type.enum';
|
||||
|
||||
@Entity('issues')
|
||||
export class Issue {
|
||||
@@ -23,6 +24,9 @@ export class Issue {
|
||||
@Column()
|
||||
longitude: string;
|
||||
|
||||
@Column({ type: 'enum', enum: IssueType })
|
||||
type: IssueType;
|
||||
|
||||
@Column({ type: 'enum', enum: IssueStatus, default: IssueStatus.new })
|
||||
status: IssueStatus;
|
||||
|
||||
@@ -33,6 +37,7 @@ export class Issue {
|
||||
return {
|
||||
id: this.id,
|
||||
title: this.title,
|
||||
type: this.type,
|
||||
status: this.status,
|
||||
description: this.description,
|
||||
latitude: this.latitude,
|
||||
|
||||
@@ -8,10 +8,12 @@ import {
|
||||
Delete,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
ParseUUIDPipe,
|
||||
} from '@nestjs/common';
|
||||
import { IssuesService } from './issues.service';
|
||||
import { CreateIssueDto } from './dto/create-issue.dto';
|
||||
import { UpdateIssueDto } from './dto/update-issue.dto';
|
||||
import { CreateIssueCommentDto } from './dto/create-issue-comment.dto';
|
||||
|
||||
@Controller('issues')
|
||||
export class IssuesController {
|
||||
@@ -22,6 +24,14 @@ export class IssuesController {
|
||||
return this.issuesService.create(createIssueDto).then((i) => i.toDto());
|
||||
}
|
||||
|
||||
@Post(':id/comments')
|
||||
createComment(
|
||||
@Param('id', ParseUUIDPipe) id: string,
|
||||
@Body() dto: CreateIssueCommentDto,
|
||||
) {
|
||||
return this.issuesService.addComment(id, dto).then((c) => c.toDto());
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Param('direction') direction: string) {
|
||||
return this.issuesService
|
||||
|
||||
@@ -3,9 +3,10 @@ import { IssuesService } from './issues.service';
|
||||
import { IssuesController } from './issues.controller';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Issue } from './entities/issue.entity';
|
||||
import { IssueComment } from './entities/issue-comment.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Issue])],
|
||||
imports: [TypeOrmModule.forFeature([Issue, IssueComment])],
|
||||
controllers: [IssuesController],
|
||||
providers: [IssuesService],
|
||||
exports: [IssuesService],
|
||||
|
||||
@@ -4,11 +4,15 @@ import { UpdateIssueDto } from './dto/update-issue.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Issue } from './entities/issue.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CreateIssueCommentDto } from './dto/create-issue-comment.dto';
|
||||
import { IssueComment } from './entities/issue-comment.entity';
|
||||
|
||||
@Injectable()
|
||||
export class IssuesService {
|
||||
constructor(
|
||||
@InjectRepository(Issue) private readonly repository: Repository<Issue>,
|
||||
@InjectRepository(IssueComment)
|
||||
private readonly comments: Repository<IssueComment>,
|
||||
) {}
|
||||
|
||||
async create(createIssueDto: CreateIssueDto) {
|
||||
@@ -16,6 +20,13 @@ export class IssuesService {
|
||||
return this.repository.save(issue);
|
||||
}
|
||||
|
||||
async addComment(id: string, { content }: CreateIssueCommentDto) {
|
||||
const issue = await this.findOne(id);
|
||||
const comment = this.comments.create({ content });
|
||||
comment.issue = Promise.resolve(issue);
|
||||
return this.comments.save(comment);
|
||||
}
|
||||
|
||||
async findAll(descending: boolean) {
|
||||
return this.repository.find({
|
||||
order: { createdAt: descending ? 'DESC' : 'ASC' },
|
||||
|
||||
Reference in new issue
Block a user