Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/ISSUE_TEMPLATE/refact_request.md
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:**
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions src/main/java/mission/controller/HomeController.java

This file was deleted.

36 changes: 0 additions & 36 deletions src/main/java/mission/post/PostController.java

This file was deleted.

67 changes: 0 additions & 67 deletions src/main/java/mission/post/PostService.java

This file was deleted.

44 changes: 44 additions & 0 deletions src/main/java/mission/post/application/PostController.java
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());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new로 command를 생성하는 이유가 있을까요?

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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mission.post.dto;
package mission.post.application.dto;

public record PostCreateResponse(
Long articleId,
Expand Down
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;

Expand Down
61 changes: 61 additions & 0 deletions src/main/java/mission/post/business/PostService.java
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())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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);
}
}
13 changes: 13 additions & 0 deletions src/main/java/mission/post/business/command/CreatePostCommand.java
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");
}
}
15 changes: 15 additions & 0 deletions src/main/java/mission/post/business/command/UpdatePostCommand.java
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");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 간단한 validation은 어노테이션으로도 가능해요 @Valid, @notblank, @NotNull 같은거 공부해보시고 커스텀어노테이션으로도 도메인규칙이 적용된 검증이 가능해요

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");
}
}
9 changes: 9 additions & 0 deletions src/main/java/mission/post/business/result/PostResult.java
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
@@ -1,9 +1,9 @@
package mission.post;
package mission.post.domain;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.*;
import mission.user.User;
import mission.user.domain.User;

@Entity
@Table(name = "post")
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/mission/post/implement/PostReader.java
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"));
}
}
16 changes: 16 additions & 0 deletions src/main/java/mission/post/implement/PostWriter.java
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);}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mission.post;
package mission.post.repository;

import mission.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post, Long> {
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/mission/user/UserController.java

This file was deleted.

Loading