diff --git a/src/modules/post/post.controller.ts b/src/modules/post/post.controller.ts index 51bebea..846493a 100644 --- a/src/modules/post/post.controller.ts +++ b/src/modules/post/post.controller.ts @@ -1,5 +1,6 @@ -import { Controller, Post, Body } from '@nestjs/common'; +import { Controller, Post, Body, Get, Param, Put, Delete } from '@nestjs/common'; import { PostService } from './post.service'; +import { PostDto } from './post.dto'; @Controller('posts') export class PostController { @@ -8,7 +9,27 @@ export class PostController { ) { } @Post() - async store(@Body() data) { + async store(@Body() data: PostDto) { return await this.postService.store(data); } + + @Get() + async index() { + return await this.postService.index(); + } + + @Get(':id') + async show(@Param() id: string) { + return await this.postService.show(id); + } + + @Put(':id') + async update(@Param() id: string, @Body() data: Partial) { + return await this.postService.update(id, data); + } + + @Delete(':id') + async destroy(@Param() id: string) { + return await this.postService.destroy(id); + } } diff --git a/src/modules/post/post.dto.ts b/src/modules/post/post.dto.ts new file mode 100644 index 0000000..87eb738 --- /dev/null +++ b/src/modules/post/post.dto.ts @@ -0,0 +1,4 @@ +export class PostDto { + readonly title: string; + readonly body: string; +} \ No newline at end of file diff --git a/src/modules/post/post.entity.ts b/src/modules/post/post.entity.ts index 8ca7bca..381ee46 100644 --- a/src/modules/post/post.entity.ts +++ b/src/modules/post/post.entity.ts @@ -8,7 +8,7 @@ export class Post { @Column() title: string; - @Column('longtext') + @Column('longtext', {nullable: true}) body: string; @CreateDateColumn() diff --git a/src/modules/post/post.service.ts b/src/modules/post/post.service.ts index 8a4d974..610787b 100644 --- a/src/modules/post/post.service.ts +++ b/src/modules/post/post.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'; import { Repository } from 'typeorm'; import { Post } from './post.entity'; import { InjectRepository } from '@nestjs/typeorm'; +import { PostDto } from './post.dto'; @Injectable() export class PostService { @@ -10,8 +11,28 @@ export class PostService { private readonly postRepository: Repository, ) { } - async store(data) { + async store(data: PostDto) { const entity = await this.postRepository.create(data); return await this.postRepository.save(entity); } + + async index() { + const entities = await this.postRepository.find(); + return entities; + } + + async show(id: string) { + const entity = await this.postRepository.findOne(id); + return entity; + } + + async update(id: string, data: Partial) { + const result = await this.postRepository.update(id, data); + return result; + } + + async destroy(id: string) { + const result = await this.postRepository.delete(id); + return result; + } }