-
Notifications
You must be signed in to change notification settings - Fork 4
과제 1-1 976520 과제제출 #4
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
Open
976520
wants to merge
15
commits into
master
Choose a base branch
from
task/jw
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
997f9da
feat
976520 95a6357
refector: Autowired 대신 생성자 주입을 사용하도록 변경했습니다.
976520 c492feb
refector: service layer에서 responseentitiy를 제거하고 contorler에서 처리하도록 했습니다.
976520 6ea9a4d
perf: getAllArticles에 캐싱을 적용하였습니다.
976520 22f6815
perf: ArticleService와 ArticleController에 비동기 처리 로직을 추가하였습니다
976520 2693c8f
Refector: javax.validation 라이브러리를 사용하여 입력값 검증을 하였습니다.
976520 e9038a3
feat: @controlleradvice를 통해 전역 예외 처리 클래스를 생성했습니다. 또한 최적화를 위해 임의로 생성한 …
976520 492e11e
Refector: ArticleServiceImpl 클래스가 ArticleService 인터페이스를 구현하도록 했습니다.
976520 e637cad
refector: 분리..?
976520 c78214f
refector: 이게맞나
976520 f212afb
refector: 이게맞나
976520 fcc4152
Refector: setter pattern을 과감히 제거하여 article entity의 state 변경을 캡슐화하였습니다.
976520 7da4863
Refector: 걍 지울게 ㅋㅋ
976520 2834d52
Refector: 예외 처리 로직 분리
976520 1e100e9
refector: remove Boilerplate code
976520 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
70 changes: 70 additions & 0 deletions
70
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/controller/ArticleController.java
This file contains hidden or 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,70 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.controller; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.Article; | ||
import com.gsm._8th.class4.backed.task._1._1.dto.ArticleRequestDTO; | ||
import com.gsm._8th.class4.backed.task._1._1.service.ArticleService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@RestController | ||
@RequestMapping("/articles") | ||
@RequiredArgsConstructor | ||
public class ArticleController { | ||
|
||
@Qualifier("articleServiceImpl") | ||
private final ArticleService articleService; | ||
|
||
@GetMapping | ||
public CompletableFuture<ResponseEntity<List<Article>>> getAllArticles() { | ||
return articleService.getAllArticles().thenApply(ResponseEntity::ok); | ||
} | ||
|
||
@GetMapping("/{articleId}") | ||
public CompletableFuture<ResponseEntity<Article>> getArticleById(@PathVariable Long articleId) { | ||
return articleService.getArticleById(articleId) | ||
.thenApply(article -> article.map(ResponseEntity::ok) | ||
.orElseGet(() -> ResponseEntity.notFound().build())); | ||
} | ||
|
||
@PostMapping | ||
public CompletableFuture<ResponseEntity<Article>> createArticle(@RequestBody ArticleRequestDTO articleDTO) { | ||
validateArticleDTO(articleDTO); | ||
Article article = articleDTO.toEntity(); | ||
return articleService.createArticle(article).thenApply(ResponseEntity::ok); | ||
} | ||
|
||
@PatchMapping("/{articleId}") | ||
public CompletableFuture<ResponseEntity<Article>> updateArticle(@PathVariable Long articleId, @RequestBody ArticleRequestDTO articleDTO) { | ||
validateArticleDTO(articleDTO); | ||
Article articleDetails = articleDTO.toEntity(); | ||
return articleService.updateArticle(articleId, articleDetails) | ||
.thenApply(updatedArticle -> updatedArticle.map(ResponseEntity::ok) | ||
.orElseGet(() -> ResponseEntity.notFound().build())); | ||
} | ||
|
||
@DeleteMapping("/{articleId}") | ||
public CompletableFuture<ResponseEntity<Void>> deleteArticle(@PathVariable Long articleId) { | ||
return articleService.deleteArticle(articleId) | ||
.thenApply(deleted -> deleted ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build()); | ||
} | ||
|
||
private void validateArticleDTO(ArticleRequestDTO articleDTO) { | ||
if (articleDTO.getTitle() == null || articleDTO.getTitle().isBlank()) { | ||
throw new IllegalArgumentException("Title에 뭐라도 써주세요..."); | ||
} | ||
if (articleDTO.getTitle().length() > 100) { | ||
throw new IllegalArgumentException("Title은 100자를 넘을 수 없습니다."); | ||
} | ||
if (articleDTO.getContent() == null || articleDTO.getContent().isBlank()) { | ||
throw new IllegalArgumentException("Content에 뭐라도 써주세요..."); | ||
} | ||
if (articleDTO.getContent().length() > 1000) { | ||
throw new IllegalArgumentException("Content는 1000자를 넘을 수 없습니다."); | ||
} | ||
} | ||
Comment on lines
+57
to
+69
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. |
||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/domain/Article.java
This file contains hidden or 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,30 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.global.entity.BaseIdxEntity; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Entity | ||
@Table(name = "articles") | ||
public class Article extends BaseIdxEntity { | ||
private String title; | ||
private String content; | ||
|
||
@Builder | ||
public Article(String title, String content) { | ||
this.title = title; | ||
this.content = content; | ||
} | ||
|
||
public void update(String title, String content) { | ||
this.title = title; | ||
this.content = content; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/domain/ArticleDTO.java
This file contains hidden or 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 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.AllArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class ArticleDTO { | ||
private String title; | ||
private String content; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/domain/ArticleMapper.java
This file contains hidden or 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 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.domain; | ||
|
||
public class ArticleMapper { | ||
public static ArticleDTO toDTO(Article article) { | ||
return new ArticleDTO(article.getTitle(), article.getContent()); | ||
} | ||
|
||
public static Article toEntity(ArticleDTO articleDTO) { | ||
return Article.builder() | ||
.title(articleDTO.getTitle()) | ||
.content(articleDTO.getContent()) | ||
.build(); | ||
} | ||
} |
2 changes: 0 additions & 2 deletions
2
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/domain/README.md
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/dto/ArticleRequestDTO.java
This file contains hidden or 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,33 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.dto; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.Article; | ||
|
||
public class ArticleRequestDTO { | ||
|
||
private String title; | ||
|
||
private String content; | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
Comment on lines
+11
to
+25
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.
|
||
|
||
public Article toEntity() { | ||
return Article.builder() | ||
.title(this.title) | ||
.content(this.content) | ||
.build(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/global/GlobalExceptionHandler.java
This file contains hidden or 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,16 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.global; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<String> handleException(Exception e) { | ||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
} | ||
snowykte0426 marked this conversation as resolved.
Show resolved
Hide resolved
|
10 changes: 1 addition & 9 deletions
10
...main/java/com/gsm/_8th/class4/backed/task/_1/_1/global/annotation/task/info/TaskInfo.java
This file contains hidden or 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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/global/config/WebConfig.java
This file contains hidden or 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,17 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.global.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") | ||
.allowedOrigins("*") | ||
.allowedMethods("*") | ||
.allowedHeaders("*"); | ||
} | ||
} |
This file contains hidden or 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
9 changes: 9 additions & 0 deletions
9
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/repository/ArticleRepository.java
This file contains hidden or 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,9 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.repository; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.Article; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ArticleRepository extends JpaRepository<Article, Long> { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/service/ArticleService.java
This file contains hidden or 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,19 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.service; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.Article; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public interface ArticleService { | ||
|
||
CompletableFuture<List<Article>> getAllArticles(); | ||
|
||
CompletableFuture<Optional<Article>> getArticleById(Long id); | ||
|
||
CompletableFuture<Article> createArticle(Article article); | ||
|
||
CompletableFuture<Optional<Article>> updateArticle(Long id, Article articleDetails); | ||
|
||
CompletableFuture<Boolean> deleteArticle(Long id); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/gsm/_8th/class4/backed/task/_1/_1/service/impl/ArticleServiceImpl.java
This file contains hidden or 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,65 @@ | ||
package com.gsm._8th.class4.backed.task._1._1.service.impl; | ||
|
||
import com.gsm._8th.class4.backed.task._1._1.domain.Article; | ||
import com.gsm._8th.class4.backed.task._1._1.repository.ArticleRepository; | ||
import com.gsm._8th.class4.backed.task._1._1.service.ArticleService; | ||
import com.gsm._8th.class4.backed.task._1._1.util.ExceptionHandlerUtil; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Service | ||
public class ArticleServiceImpl implements ArticleService { | ||
|
||
private final ArticleRepository articleRepository; | ||
|
||
public ArticleServiceImpl(ArticleRepository articleRepository) { | ||
this.articleRepository = articleRepository; | ||
} | ||
|
||
@Override | ||
@Async | ||
public CompletableFuture<List<Article>> getAllArticles() { | ||
return CompletableFuture.supplyAsync(() -> articleRepository.findAll()) | ||
.exceptionally(ex -> ExceptionHandlerUtil.handleListException(ex)); | ||
} | ||
|
||
@Override | ||
@Async | ||
public CompletableFuture<Optional<Article>> getArticleById(Long id) { | ||
return CompletableFuture.supplyAsync(() -> articleRepository.findById(id)) | ||
.exceptionally(ex -> ExceptionHandlerUtil.handleOptionalException(ex)); | ||
} | ||
|
||
@Override | ||
@Async | ||
public CompletableFuture<Article> createArticle(Article article) { | ||
return CompletableFuture.supplyAsync(() -> articleRepository.save(article)) | ||
.exceptionally(ex -> ExceptionHandlerUtil.handleException(ex, null)); | ||
} | ||
|
||
@Override | ||
@Async | ||
public CompletableFuture<Optional<Article>> updateArticle(Long id, Article articleDetails) { | ||
return CompletableFuture.supplyAsync(() -> articleRepository.findById(id) | ||
.map(existingArticle -> { | ||
existingArticle.update(articleDetails.getTitle(), articleDetails.getContent()); | ||
return articleRepository.save(existingArticle); | ||
})) | ||
.exceptionally(ex -> ExceptionHandlerUtil.handleOptionalException(ex)); | ||
} | ||
|
||
@Override | ||
@Async | ||
public CompletableFuture<Boolean> deleteArticle(Long id) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
Optional<Article> article = articleRepository.findById(id); | ||
article.ifPresent(articleRepository::delete); | ||
return article.isPresent(); | ||
}) | ||
.exceptionally(ex -> ExceptionHandlerUtil.handleBooleanException(ex)); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
보통 DELETE 요청의 대한 응답에는 메세지 Body가 없습니다!(204 No Content)
클라이언트에 true false로 알려주기 보단 비즈니스 로직 중간에서 예외를 throw 하고 그걸
GlobalExceptionHandler
에서 캐치해서 응답을 생성하는 방식으로 클라이언트에 처리결과를 알려주고 성공하면 204를 줍니다Spring에서 ResponseEntity의 status를 204로 설정하거나 그냥 메서드 반환형을
void
로 하고@ResponseStatus
어노테이션을 적용하면 됩니다