Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class CreateCommentDto { | ||
message: string; | ||
publish_date: string; | ||
users_id: string; | ||
post_id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class UpdateCommentDto { | ||
message: string; | ||
publish_date: string; | ||
users_id: string; | ||
post_id: string; | ||
} |