Archived
refactor the main controller + add reactions
This commit is contained in:
7 files changed
+113
-82
No files matched your search
@@ -0,0 +1,78 @@
|
|||||||
|
import {
|
||||||
|
Body,
|
||||||
|
Controller,
|
||||||
|
Delete,
|
||||||
|
Get,
|
||||||
|
HttpCode,
|
||||||
|
HttpStatus,
|
||||||
|
Param,
|
||||||
|
ParseUUIDPipe,
|
||||||
|
Post,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { IssuesService } from './issues/issues.service';
|
||||||
|
import { IssueType } from './issues/entities/issue-type.enum';
|
||||||
|
import { CreateIssueDto } from './issues/dto/create-issue.dto';
|
||||||
|
import { CreateIssueCommentDto } from './issues/dto/create-issue-comment.dto';
|
||||||
|
|
||||||
|
@Controller()
|
||||||
|
export class AppController {
|
||||||
|
constructor(private readonly issues: IssuesService) {}
|
||||||
|
|
||||||
|
@Get('/issues/problems')
|
||||||
|
listProblems() {
|
||||||
|
return this.issues
|
||||||
|
.findAll(IssueType.problem)
|
||||||
|
.then((r) => r.map((i) => i.toList()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('/issues/suggestions')
|
||||||
|
listSuggestions() {
|
||||||
|
return this.issues
|
||||||
|
.findAll(IssueType.suggestion)
|
||||||
|
.then((r) => r.map((i) => i.toList()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get('/issues/:id')
|
||||||
|
async getIssue(@Param('id', ParseUUIDPipe) id: string) {
|
||||||
|
const data = await this.issues.findOne(id).then((i) => i.toDto());
|
||||||
|
const comments = await this.issues.getCommentsForIssue(id);
|
||||||
|
return { data, comments };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('/issues')
|
||||||
|
createIssue(@Body() dto: CreateIssueDto) {
|
||||||
|
return this.issues.create(dto);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('/issues/:id/comments')
|
||||||
|
addComment(
|
||||||
|
@Body() dto: CreateIssueCommentDto,
|
||||||
|
@Param('id', ParseUUIDPipe) id: string,
|
||||||
|
) {
|
||||||
|
return this.issues.addComment(id, dto).then((c) => c.toDto());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('/issues/:id/like')
|
||||||
|
@HttpCode(HttpStatus.ACCEPTED)
|
||||||
|
async addLike(@Param('id', ParseUUIDPipe) id: string) {
|
||||||
|
await this.issues.like(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post('/issues/:id/dislike')
|
||||||
|
@HttpCode(HttpStatus.ACCEPTED)
|
||||||
|
async addDislike(@Param('id', ParseUUIDPipe) id: string) {
|
||||||
|
await this.issues.dislike(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete('/issues/:id/like')
|
||||||
|
@HttpCode(HttpStatus.ACCEPTED)
|
||||||
|
async removeLike(@Param('id', ParseUUIDPipe) id: string) {
|
||||||
|
await this.issues.like(id, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete('/issues/:id/dislike')
|
||||||
|
@HttpCode(HttpStatus.ACCEPTED)
|
||||||
|
async removeDislike(@Param('id', ParseUUIDPipe) id: string) {
|
||||||
|
await this.issues.dislike(id, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { IssuesModule } from './issues/issues.module';
|
import { IssuesModule } from './issues/issues.module';
|
||||||
|
import { AppController } from './app.controller';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
|
controllers: [AppController],
|
||||||
imports: [
|
imports: [
|
||||||
TypeOrmModule.forRoot({
|
TypeOrmModule.forRoot({
|
||||||
type: 'postgres',
|
type: 'postgres',
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
import { OmitType, PartialType } from '@nestjs/mapped-types';
|
|
||||||
import { CreateIssueDto } from './create-issue.dto';
|
|
||||||
|
|
||||||
export class UpdateIssueDto extends PartialType(
|
|
||||||
OmitType(CreateIssueDto, ['type', 'username']),
|
|
||||||
) {}
|
|
||||||
@@ -36,17 +36,31 @@ export class Issue {
|
|||||||
@CreateDateColumn()
|
@CreateDateColumn()
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
|
|
||||||
|
@Column({ default: 0 })
|
||||||
|
likes: number;
|
||||||
|
|
||||||
|
@Column({ default: 0 })
|
||||||
|
dislikes: number;
|
||||||
|
|
||||||
toDto() {
|
toDto() {
|
||||||
|
return {
|
||||||
|
...this.toList(),
|
||||||
|
type: this.type,
|
||||||
|
description: this.description,
|
||||||
|
latitude: this.latitude,
|
||||||
|
longitude: this.longitude,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
toList() {
|
||||||
return {
|
return {
|
||||||
id: this.id,
|
id: this.id,
|
||||||
username: this.username,
|
username: this.username,
|
||||||
title: this.title,
|
title: this.title,
|
||||||
type: this.type,
|
|
||||||
status: this.status,
|
status: this.status,
|
||||||
description: this.description,
|
createdAt: this.createdAt.toISOString(),
|
||||||
latitude: this.latitude,
|
likes: this.likes,
|
||||||
longitude: this.longitude,
|
dislikes: this.dislikes,
|
||||||
createdAt: this.createdAt,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
import {
|
|
||||||
Controller,
|
|
||||||
Get,
|
|
||||||
Post,
|
|
||||||
Body,
|
|
||||||
Patch,
|
|
||||||
Param,
|
|
||||||
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 {
|
|
||||||
constructor(private readonly issuesService: IssuesService) {}
|
|
||||||
|
|
||||||
@Post()
|
|
||||||
create(@Body() createIssueDto: CreateIssueDto) {
|
|
||||||
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
|
|
||||||
.findAll(
|
|
||||||
direction?.toLowerCase() === 'desc' ||
|
|
||||||
direction?.toLowerCase() === 'descending',
|
|
||||||
)
|
|
||||||
.then((r) => r.map((i) => i.toDto()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get(':id')
|
|
||||||
findOne(@Param('id') id: string) {
|
|
||||||
return this.issuesService.findOne(id).then((i) => i.toDto());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Patch(':id')
|
|
||||||
update(@Param('id') id: string, @Body() updateIssueDto: UpdateIssueDto) {
|
|
||||||
return this.issuesService.update(id, updateIssueDto).then((i) => i.toDto());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Delete(':id')
|
|
||||||
@HttpCode(HttpStatus.NO_CONTENT)
|
|
||||||
remove(@Param('id') id: string) {
|
|
||||||
return this.issuesService.remove(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { IssuesService } from './issues.service';
|
import { IssuesService } from './issues.service';
|
||||||
import { IssuesController } from './issues.controller';
|
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { Issue } from './entities/issue.entity';
|
import { Issue } from './entities/issue.entity';
|
||||||
import { IssueComment } from './entities/issue-comment.entity';
|
import { IssueComment } from './entities/issue-comment.entity';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [TypeOrmModule.forFeature([Issue, IssueComment])],
|
imports: [TypeOrmModule.forFeature([Issue, IssueComment])],
|
||||||
controllers: [IssuesController],
|
|
||||||
providers: [IssuesService],
|
providers: [IssuesService],
|
||||||
exports: [IssuesService],
|
exports: [IssuesService],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||||
import { CreateIssueDto } from './dto/create-issue.dto';
|
import { CreateIssueDto } from './dto/create-issue.dto';
|
||||||
import { UpdateIssueDto } from './dto/update-issue.dto';
|
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Issue } from './entities/issue.entity';
|
import { Issue } from './entities/issue.entity';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { CreateIssueCommentDto } from './dto/create-issue-comment.dto';
|
import { CreateIssueCommentDto } from './dto/create-issue-comment.dto';
|
||||||
import { IssueComment } from './entities/issue-comment.entity';
|
import { IssueComment } from './entities/issue-comment.entity';
|
||||||
|
import { IssueType } from './entities/issue-type.enum';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class IssuesService {
|
export class IssuesService {
|
||||||
@@ -27,28 +27,33 @@ export class IssuesService {
|
|||||||
return this.comments.save(comment);
|
return this.comments.save(comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAll(descending: boolean) {
|
async getCommentsForIssue(id: string) {
|
||||||
return this.repository.find({
|
return this.comments.findBy({ issue: { id } });
|
||||||
order: { createdAt: descending ? 'DESC' : 'ASC' },
|
}
|
||||||
|
|
||||||
|
async findAll(type?: IssueType) {
|
||||||
|
return this.repository.findBy({
|
||||||
|
type,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async findOne(id: string) {
|
async findOne(id: string) {
|
||||||
const ent = this.repository.findOneBy({ id });
|
const ent = await this.repository.findOneBy({ id });
|
||||||
if (ent === null) {
|
if (ent === null) {
|
||||||
throw new NotFoundException('Issue not found');
|
throw new NotFoundException('Issue not found');
|
||||||
}
|
}
|
||||||
return ent;
|
return ent;
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, updateIssueDto: UpdateIssueDto) {
|
async like(id: string, remove?: boolean) {
|
||||||
const ent = await this.findOne(id);
|
const ent = await this.findOne(id);
|
||||||
this.repository.merge(ent, updateIssueDto);
|
ent.likes = remove ? ent.likes - 1 : ent.likes + 1;
|
||||||
return this.repository.save(ent);
|
return this.repository.save(ent);
|
||||||
}
|
}
|
||||||
|
|
||||||
async remove(id: string) {
|
async dislike(id: string, remove?: boolean) {
|
||||||
const ent = await this.findOne(id);
|
const ent = await this.findOne(id);
|
||||||
return this.repository.remove(ent);
|
ent.dislikes = remove ? ent.dislikes - 1 : ent.dislikes + 1;
|
||||||
|
return this.repository.save(ent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user