-
Notifications
You must be signed in to change notification settings - Fork 0
[#1]; refact : 4계층 적용 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e1545d3
cadaeae
0090110
b327878
db2688e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| --- | ||
| name: "Refact Report" | ||
| about: 버그 리포트를 작성할 때 사용하는 템플릿입니다. | ||
| title: "[REFACT] " | ||
| labels: "refact" | ||
| assignees: '' | ||
| --- | ||
|
|
||
| ### 🔬 재현 절차 | ||
| 1. ... | ||
| 2. ... | ||
| 3. ... | ||
|
|
||
| ### 🤔 예상 결과 | ||
| ### 💻 환경 | ||
| - **OS:** - **Browser:** |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package mission.post.application; | ||
|
|
||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import mission.post.business.PostService; | ||
| import mission.post.application.dto.CreatePostRequest; | ||
| import mission.post.application.dto.PostCreateResponse; | ||
| import mission.post.application.dto.UpdatePostRequest; | ||
| import mission.post.business.command.CreatePostCommand; | ||
| import mission.post.business.command.UpdatePostCommand; | ||
| import mission.post.business.result.PostResult; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/posts") | ||
| @RequiredArgsConstructor | ||
| public class PostController { | ||
| private final PostService postService; | ||
|
|
||
| @PostMapping | ||
| public PostCreateResponse create(@RequestBody @Valid CreatePostRequest req) { | ||
| var cmd = new CreatePostCommand(req.email(), req.password(), req.title(), req.content()); | ||
| PostResult result = postService.create(cmd); | ||
| return new PostCreateResponse(result.articleId(), result.email(), result.title(), result.content()); | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public PostCreateResponse update(@PathVariable Long id, @RequestBody @Valid UpdatePostRequest req) { | ||
| var cmd = new UpdatePostCommand(id, req.email(), req.password(), req.title(), req.content()); | ||
| PostResult result = postService.update(cmd); | ||
| return new PostCreateResponse(result.articleId(), result.email(), result.title(), result.content()); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| @ResponseStatus(HttpStatus.NO_CONTENT) | ||
| public void delete(@PathVariable Long id, @RequestBody Map<String, String> body) { | ||
| String email = body.get("email"); | ||
| String password = body.get("password"); | ||
| postService.delete(id, email, password); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package mission.post.dto; | ||
| package mission.post.application.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package mission.post.dto; | ||
| package mission.post.application.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package mission.post.business; | ||
|
|
||
| import jakarta.transaction.Transactional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import mission.post.business.command.CreatePostCommand; | ||
| import mission.post.business.command.UpdatePostCommand; | ||
| import mission.post.business.result.PostResult; | ||
| import mission.post.domain.Post; | ||
| import mission.post.implement.PostReader; | ||
| import mission.post.implement.PostWriter; | ||
| import mission.user.domain.User; | ||
| import mission.user.implement.AuthVerifier; | ||
| import mission.user.implement.UserReader; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PostService { | ||
| private final PostReader postReader; | ||
| private final PostWriter postWriter; | ||
| private final UserReader userReader; | ||
| private final AuthVerifier authVerifier; | ||
|
|
||
| @Transactional | ||
| public PostResult create(CreatePostCommand createPostCommand) { | ||
| User author = userReader.getByEmail(createPostCommand.email()); | ||
| authVerifier.verifyUserEmailAndPassword(author, createPostCommand.email(), createPostCommand.password()); | ||
|
|
||
| Post saved = postWriter.save( | ||
| Post.builder() | ||
| .title(createPostCommand.title()) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 엥 제가 빌더패턴말고 정적팩토리메소드를 한번 써보라고했었는데.........왜 써야할까요? |
||
| .content(createPostCommand.content()) | ||
| .author(author) | ||
| .build() | ||
| ); | ||
|
|
||
| return new PostResult(saved.getId(), saved.getAuthor().getEmail(), saved.getTitle(), saved.getContent()); | ||
| } | ||
|
|
||
| @Transactional | ||
| public PostResult update(UpdatePostCommand updatePostCommand) { | ||
| Post post = postReader.getById(updatePostCommand.id()); | ||
| authVerifier.verifyUserEmailAndPassword(post.getAuthor(), updatePostCommand.email(), updatePostCommand.password()); | ||
| post.edit(updatePostCommand.title(), updatePostCommand.content()); | ||
|
|
||
| return new PostResult( | ||
| post.getId(), | ||
| post.getAuthor().getEmail(), | ||
| post.getTitle(), | ||
| post.getContent() | ||
| ); | ||
| } | ||
| @Transactional | ||
| public void delete(Long id, String email, String password){ | ||
| Post post = postReader.getById(id); | ||
|
|
||
| authVerifier.verifyUserEmailAndPassword(post.getAuthor(), email, password); | ||
|
|
||
| postWriter.delete(post); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package mission.post.business.command; | ||
|
|
||
| public record CreatePostCommand ( | ||
| String email, | ||
| String password, | ||
| String title, | ||
| String content | ||
| ) { | ||
| public CreatePostCommand { | ||
| if (title == null || title.isBlank()) throw new IllegalArgumentException("title is null or empty"); | ||
| if (content == null || content.isBlank()) throw new IllegalArgumentException("content is null or empty"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package mission.post.business.command; | ||
|
|
||
| public record UpdatePostCommand( | ||
| Long id, | ||
| String email, | ||
| String password, | ||
| String title, | ||
| String content | ||
| ) { | ||
| public UpdatePostCommand{ | ||
| if(id==null) throw new IllegalArgumentException("id is null"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if(title==null || title.isBlank()) throw new IllegalArgumentException("title is null or empty"); | ||
| if(content==null || content.isBlank()) throw new IllegalArgumentException("content is null or empty"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package mission.post.business.result; | ||
|
|
||
| public record PostResult( | ||
| Long articleId, | ||
| String email, | ||
| String title, | ||
| String content | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package mission.post.implement; | ||
|
|
||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import mission.post.domain.Post; | ||
| import mission.post.repository.PostRepository; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class PostReader { | ||
| private final PostRepository postRepository; | ||
|
|
||
| public Post getById(Long id) { | ||
| return postRepository.findById(id) | ||
| .orElseThrow(()->new IllegalArgumentException("post not found")); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package mission.post.implement; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import mission.post.domain.Post; | ||
| import mission.post.repository.PostRepository; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class PostWriter { | ||
| private final PostRepository postRepository; | ||
|
|
||
| public Post save(Post post){return postRepository.save(post);} | ||
| public void delete(Post post){postRepository.delete(post);} | ||
|
|
||
| } |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new로 command를 생성하는 이유가 있을까요?