Skip to content

Commit

Permalink
feat: 馃幐 add comment api
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Apr 20, 2022
1 parent 15d2966 commit 18a5d8d
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/app.module.ts
Expand Up @@ -5,9 +5,10 @@ import { UserModule } from './user/user.module';
import { LocationModule } from './location/location.module';
import { PostModule } from './post/post.module';
import { TagModule } from './tag/tag.module';
import { CommentModule } from './comment/comment.module';

@Module({
imports: [UserModule, LocationModule, PostModule, TagModule],
imports: [UserModule, LocationModule, PostModule, TagModule, CommentModule],
controllers: [AppController],
providers: [AppService],
})
Expand Down
20 changes: 20 additions & 0 deletions src/comment/comment.controller.spec.ts
@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CommentController } from './comment.controller';
import { CommentService } from './comment.service';

describe('CommentController', () => {
let controller: CommentController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [CommentController],
providers: [CommentService],
}).compile();

controller = module.get<CommentController>(CommentController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
65 changes: 65 additions & 0 deletions src/comment/comment.controller.ts
@@ -0,0 +1,65 @@
import {
Controller,
Get,
Post,
Body,
Put,
Param,
Delete,
} from '@nestjs/common';
import { CommentService } from './comment.service';
import { CreateCommentDto } from './dto/createComment.dto';
import { UpdateCommentDto } from './dto/updateComment.dto';

@Controller('comment')
export class CommentController {
constructor(private readonly commentService: CommentService) {}

@Post()
async createComment(
@Body() createCommentDto: CreateCommentDto,
): Promise<any> {
const comment = await this.commentService.createComment(createCommentDto);

const response = { message: 'createComment', comment: comment };
return response;
}

@Get()
async getComments(): Promise<any> {
const comments = await this.commentService.getComments();

const response = { message: 'getComments', comments: comments };
return response;
}

@Get('/:id')
async getCommentById(@Param('id') id: string): Promise<any> {
const comment = await this.commentService.getCommentById(id);

const response = { message: 'getCommentById', comment: comment };
return response;
}

@Put('/:id')
async updateCommentById(
@Param('id') id: string,
@Body() updateCommentDto: UpdateCommentDto,
): Promise<any> {
const comment = await this.commentService.updateCommentById(
id,
updateCommentDto,
);

const response = { message: 'updateCommentById', comment: comment };
return response;
}

@Delete('/:id')
async deleteCommentById(@Param('id') id: string): Promise<any> {
const comment = await this.commentService.deleteCommentById(id);

const response = { message: 'deleteCommentById', comment: comment };
return response;
}
}
11 changes: 11 additions & 0 deletions src/comment/comment.module.ts
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { CommentService } from './comment.service';
import { CommentController } from './comment.controller';
import { PrismaService } from '../prisma.service';

@Module({
imports: [],
controllers: [CommentController],
providers: [CommentService, PrismaService],
})
export class CommentModule {}
18 changes: 18 additions & 0 deletions src/comment/comment.service.spec.ts
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CommentService } from './comment.service';

describe('CommentService', () => {
let service: CommentService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CommentService],
}).compile();

service = module.get<CommentService>(CommentService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
91 changes: 91 additions & 0 deletions src/comment/comment.service.ts
@@ -0,0 +1,91 @@
import { Injectable } from '@nestjs/common';
import { CreateCommentDto } from './dto/createComment.dto';
import { UpdateCommentDto } from './dto/updateComment.dto';
import { PrismaService } from '../prisma.service';
import { comment } from '@prisma/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';

dayjs.extend(utc);
dayjs.extend(timezone);

@Injectable()
export class CommentService {
constructor(private readonly prisma: PrismaService) {}

async createComment(createCommentDto: CreateCommentDto): Promise<comment> {
const comments = await this.prisma.comment.create({
data: {
message: createCommentDto.message,
publish_date: dayjs(createCommentDto.publish_date)
.tz('Asia/Hong_Kong')
.toISOString(),
users_id: createCommentDto.users_id,
post_id: createCommentDto.post_id,
},
include: {
owners: true,
post: true,
},
});
return comments;
}

async getComments(): Promise<any> {
const comments = await this.prisma.comment.findMany({
include: {
owners: true,
post: true,
},
});
return comments;
}

async getCommentById(id: string): Promise<any> {
const comment = await this.prisma.comment.findUnique({
where: {
id: id,
},
include: {
owners: true,
post: true,
},
});
return comment;
}

async updateCommentById(id: string, updateCommentDto: UpdateCommentDto) {
const comment = await this.prisma.comment.update({
where: {
id: id,
},
data: {
message: updateCommentDto.message,
publish_date: dayjs(updateCommentDto.publish_date)
.tz('Asia/Hong_Kong')
.toISOString(),
users_id: updateCommentDto.users_id,
post_id: updateCommentDto.post_id,
},
include: {
owners: true,
post: true,
},
});
return comment;
}

async deleteCommentById(id: string): Promise<any> {
const comment = await this.prisma.comment.delete({
where: {
id: id,
},
include: {
owners: true,
post: true,
},
});
return comment;
}
}
6 changes: 6 additions & 0 deletions src/comment/dto/createComment.dto.ts
@@ -0,0 +1,6 @@
export class CreateCommentDto {
message: string;
publish_date: string;
users_id: string;
post_id: string;
}
6 changes: 6 additions & 0 deletions src/comment/dto/updateComment.dto.ts
@@ -0,0 +1,6 @@
export class UpdateCommentDto {
message: string;
publish_date: string;
users_id: string;
post_id: string;
}

0 comments on commit 18a5d8d

Please sign in to comment.