-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from bmviniciuss/feature/issue-20
feature(issue-20): update post
- Loading branch information
Showing
19 changed files
with
561 additions
and
14 deletions.
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,5 @@ | ||
export interface PresentationUpdatedPost { | ||
title: string | ||
content: string | ||
userId: 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
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
4 changes: 2 additions & 2 deletions
4
...es/post/useCases/getPost/GetPostErrors.ts → src/modules/post/shared/PostErrors.ts
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
export namespace GetPostErrors { | ||
export namespace PostErrors { | ||
export class PostNotFound extends Error { | ||
constructor () { | ||
super('Post não existe') | ||
this.name = 'GetPostErrors.PostNotFound' | ||
this.name = 'PostErrors.PostNotFound' | ||
} | ||
} | ||
} |
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
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
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,14 @@ | ||
import { User } from '@prisma/client' | ||
|
||
import { PresentationPost } from '../../models/PresentationPost' | ||
|
||
export interface UpdatePostInputDTO { | ||
postId: string | ||
title: string | ||
content: string | ||
userPerformingOperation: User | ||
} | ||
|
||
export interface UpdatePost { | ||
execute(data: UpdatePostInputDTO): Promise<PresentationPost> | ||
} |
58 changes: 58 additions & 0 deletions
58
src/modules/post/useCases/updatePost/UpdatePostController.ts
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,58 @@ | ||
import { Controller } from '../../../../shared/infra/http/Controller' | ||
import { badRequest, HttpAuthenticatedRequest, HttpResponse, notFound, ok, serverError } from '../../../../shared/infra/http/http' | ||
import { Validator } from '../../../../validation/Validator' | ||
import { PresentationUpdatedPost } from '../../models/mapper/PresentationUpdatedPost' | ||
import { PostErrors } from '../../shared/PostErrors' | ||
import { UpdatePost, UpdatePostInputDTO } from './UpdatePost' | ||
import { UpdatePostError } from './UpdatePostErrors' | ||
|
||
export type UpdatePostControllerRequest = HttpAuthenticatedRequest<{ | ||
title: string | ||
content: string | ||
}, { | ||
postId: string | ||
}> | ||
|
||
export class UpdatePostController extends Controller { | ||
constructor ( | ||
private readonly validator: Validator, | ||
private readonly updatePost: UpdatePost | ||
) { | ||
super() | ||
} | ||
|
||
async execute (request: UpdatePostControllerRequest): Promise<HttpResponse<any>> { | ||
try { | ||
const validationError = this.validator.validate(request.body) | ||
if (validationError) return badRequest(validationError) | ||
|
||
const updatePostPayload: UpdatePostInputDTO = { | ||
title: request.body!.title, | ||
content: request.body!.content, | ||
postId: request.params!.postId, | ||
userPerformingOperation: request.authenticatedUser | ||
} | ||
|
||
const updatedPost = await this.updatePost.execute(updatePostPayload) | ||
|
||
const presenationUpdatedPost:PresentationUpdatedPost = { | ||
title: updatedPost.title, | ||
content: updatedPost.content, | ||
userId: updatedPost.user.id | ||
} | ||
|
||
return ok(presenationUpdatedPost) | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
switch (error.constructor) { | ||
case UpdatePostError.UnauthorizedToUpdatePostError: | ||
return badRequest(error) | ||
case PostErrors.PostNotFound: | ||
return notFound(error) | ||
} | ||
} | ||
|
||
return serverError(error) | ||
} | ||
} | ||
} |
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,8 @@ | ||
export namespace UpdatePostError { | ||
export class UnauthorizedToUpdatePostError extends Error { | ||
constructor () { | ||
super('Usuário não autorizado') | ||
this.name = 'UpdatePostError.UnauthorizedToUpdatePostError' | ||
} | ||
} | ||
} |
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,27 @@ | ||
import { PresentationPost } from '../../models/PresentationPost' | ||
import { PostRepository } from '../../repos/PostRepository' | ||
import { PostErrors } from '../../shared/PostErrors' | ||
import { UpdatePost, UpdatePostInputDTO } from './UpdatePost' | ||
import { UpdatePostError } from './UpdatePostErrors' | ||
|
||
export class UpdatePostUseCase implements UpdatePost { | ||
constructor ( | ||
private readonly postRepository: PostRepository | ||
) {} | ||
|
||
async execute (data: UpdatePostInputDTO): Promise<PresentationPost> { | ||
const post = await this.postRepository.loadById(data.postId) | ||
if (!post) throw new PostErrors.PostNotFound() | ||
|
||
if (post.user.id !== data.userPerformingOperation.id) { | ||
throw new UpdatePostError.UnauthorizedToUpdatePostError() | ||
} | ||
|
||
return await this.postRepository.update( | ||
data.postId, | ||
{ | ||
title: data.title, | ||
content: data.content | ||
}) | ||
} | ||
} |
Oops, something went wrong.