Skip to content

Commit 177f669

Browse files
committed
Create DTO for create and update
1 parent 21b16fd commit 177f669

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

src/dtos/postDTO.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ export interface IPostDTO {
77
updated_at?: string,
88
}
99

10-
export interface IPostInputDTO {
10+
export interface ICreatePostDTO {
1111
title: string,
1212
body: string,
1313
user_id: number,
14+
}
15+
16+
export interface IUpdatePostDTO {
17+
title: string,
18+
body: string,
1419
}

src/mappers/PostMapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IPostInputDTO, IPostDTO } from "../dtos/postDTO"
1+
import { ICreatePostDTO, IPostDTO } from "../dtos/postDTO"
22

33
export default class PostMapper{
44
public static toDTO(data: any): IPostDTO
@@ -13,7 +13,7 @@ export default class PostMapper{
1313
}
1414
}
1515

16-
public static toInputDTO(data: any): IPostInputDTO
16+
public static toInputDTO(data: any): ICreatePostDTO
1717
{
1818
return {
1919
title: data.title,
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { IPostDTO, IPostInputDTO } from "../../dtos/postDTO"
1+
import { IPostDTO, ICreatePostDTO, IUpdatePostDTO } from "../../dtos/postDTO"
22

33
export default interface IPostRepositoryInterface {
44
all(): Promise<IPostDTO[]>
55
find(id: number): Promise<IPostDTO>
6-
create(data: IPostInputDTO): Promise<IPostDTO>
7-
update(id: number, data: IPostInputDTO): Promise<IPostDTO>
6+
create(data: ICreatePostDTO): Promise<IPostDTO>
7+
update(id: number, data: IUpdatePostDTO): Promise<IPostDTO>
88
delete(id: number): Promise<any>
99
}

src/repositories/typeORM/postRepo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import IPostRepositoryInterface from '../interfaces/postInterface'
22
import { injectable } from "inversify"
33
import { Post } from "../../entity/Post"
4-
import { IPostDTO, IPostInputDTO } from "../../dtos/postDTO"
4+
import { IPostDTO, ICreatePostDTO, IUpdatePostDTO } from "../../dtos/postDTO"
55
import PostMapper from "../../mappers/PostMapper"
66

77

@@ -20,7 +20,7 @@ export default class PostRepository implements IPostRepositoryInterface {
2020
return PostMapper.toDTO(post);
2121
}
2222

23-
public async create(data: IPostInputDTO): Promise<IPostDTO> {
23+
public async create(data: ICreatePostDTO): Promise<IPostDTO> {
2424
const post = new Post();
2525
post.title = data.title;
2626
post.body = data.body;
@@ -29,7 +29,7 @@ export default class PostRepository implements IPostRepositoryInterface {
2929
return PostMapper.toDTO(post);
3030
}
3131

32-
public async update(id:number, data: IPostInputDTO): Promise<IPostDTO> {
32+
public async update(id:number, data: IUpdatePostDTO): Promise<IPostDTO> {
3333
const post = await Post.findOne(id);
3434
if (typeof data.title !== 'undefined' || data.title !== null) { post.title = data.title }
3535
if (typeof data.body !== 'undefined' || data.body !== null) { post.body = data.body }

0 commit comments

Comments
 (0)