-
Notifications
You must be signed in to change notification settings - Fork 13
[Spring Core] 강인화 과제 제출합니다. #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: main
Are you sure you want to change the base?
Changes from 21 commits
bc460cb
6523f0f
8f46c93
0f8680a
3672b07
30ec81a
14da25c
7bdf905
167fd5c
4e57ccf
100b4df
4e257c0
8b32f21
b050442
ef348a1
43e903e
78042a1
bfaca10
9169d55
dfe6627
0dbe606
b60661c
aaec70e
efc13e2
47d00c7
e74feed
7f76725
a7b41c3
bbf44c6
28ff372
fe2be17
4b2ad7f
2384531
807ac1a
b468327
93acc08
b5abb8a
a145eab
9e5b1cf
c7aa9de
966fbac
0d35924
f3405c6
c2ba88a
93c1057
9b5c19e
7600bd2
5c65b1c
adbb0c0
21f7c37
98c649a
f0228e9
7ab68c1
e33138e
164a7c7
f16ec8c
4c30465
756e6eb
82c04a8
9efdca4
384823e
2947fbc
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 |
|---|---|---|
| @@ -1,68 +1,94 @@ | ||
| package com.example.demo.controller; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.List; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.example.demo.controller.dto.request.ArticleCreateRequest; | ||
| import com.example.demo.controller.dto.response.ArticleResponse; | ||
| import com.example.demo.controller.dto.request.ArticleUpdateRequest; | ||
| import com.example.demo.controller.dto.response.ArticleResponse; | ||
| import com.example.demo.exception.RestApiException; | ||
| import com.example.demo.exception.error.ArticleErrorCode; | ||
| import com.example.demo.exception.error.CommonErrorCode; | ||
| import com.example.demo.service.ArticleService; | ||
| import com.example.demo.service.BoardService; | ||
| import com.example.demo.service.MemberService; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| public class ArticleController { | ||
|
|
||
| private final ArticleService articleService; | ||
| private final MemberService memberService; | ||
| private final BoardService boardService; | ||
|
|
||
| public ArticleController(ArticleService articleService) { | ||
| public ArticleController(ArticleService articleService, MemberService memberService, BoardService boardService) { | ||
| this.articleService = articleService; | ||
| this.memberService = memberService; | ||
| this.boardService = boardService; | ||
| } | ||
|
|
||
| @GetMapping("/articles") | ||
| public ResponseEntity<List<ArticleResponse>> getArticles( | ||
| @RequestParam Long boardId | ||
| @RequestParam Long boardId | ||
|
||
| ) { | ||
| List<ArticleResponse> response = articleService.getByBoardId(boardId); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping("/articles/{id}") | ||
| public ResponseEntity<ArticleResponse> getArticle( | ||
| @PathVariable Long id | ||
| @PathVariable Long id | ||
| ) { | ||
| if (articleService.getArticles() | ||
| .stream() | ||
| .noneMatch(res -> res.id().equals(id))) { | ||
| throw new RestApiException(CommonErrorCode.RESOURCE_NOT_FOUND); | ||
| } | ||
| ArticleResponse response = articleService.getById(id); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @PostMapping("/articles") | ||
| public ResponseEntity<ArticleResponse> crateArticle( | ||
| @RequestBody ArticleCreateRequest request | ||
| @RequestBody ArticleCreateRequest request | ||
| ) { | ||
| boolean isNullExistence = request.boardId() == null | ||
| || request.authorId() == null | ||
| || request.title() == null | ||
| || request.description() == null; | ||
| if (isNullExistence) { | ||
| throw new RestApiException(CommonErrorCode.NULL_PARAMETER); | ||
| } | ||
| if (boardService.getBoards() | ||
| .stream() | ||
| .noneMatch(res -> res.id().equals(request.boardId())) | ||
|
||
| || memberService.getAll() | ||
| .stream() | ||
| .noneMatch(res -> res.id().equals(request.authorId()))) { | ||
| throw new RestApiException(ArticleErrorCode.REFERENCE_ERROR); | ||
| } | ||
| ArticleResponse response = articleService.create(request); | ||
| return ResponseEntity.created(URI.create("/articles/" + response.id())).body(response); | ||
| } | ||
|
|
||
| @PutMapping("/articles/{id}") | ||
| public ResponseEntity<ArticleResponse> updateArticle( | ||
| @PathVariable Long id, | ||
| @RequestBody ArticleUpdateRequest request | ||
| @PathVariable Long id, | ||
| @RequestBody ArticleUpdateRequest request | ||
| ) { | ||
| if (boardService.getBoards() | ||
| .stream() | ||
| .noneMatch(res -> res.id().equals(request.boardId()))) { | ||
| throw new RestApiException(ArticleErrorCode.REFERENCE_ERROR); | ||
| } | ||
| ArticleResponse response = articleService.update(id, request); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @DeleteMapping("/articles/{id}") | ||
| public ResponseEntity<Void> updateArticle( | ||
| @PathVariable Long id | ||
| @PathVariable Long id | ||
| ) { | ||
| articleService.delete(id); | ||
| return ResponseEntity.noContent().build(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,27 @@ | ||
| package com.example.demo.controller; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.example.demo.controller.dto.request.BoardCreateRequest; | ||
| import com.example.demo.controller.dto.request.BoardUpdateRequest; | ||
| import com.example.demo.controller.dto.response.BoardResponse; | ||
| import com.example.demo.exception.RestApiException; | ||
| import com.example.demo.exception.error.BoardErrorCode; | ||
| import com.example.demo.exception.error.CommonErrorCode; | ||
| import com.example.demo.service.ArticleService; | ||
| import com.example.demo.service.BoardService; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| public class BoardController { | ||
|
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. 여기도 |
||
|
|
||
| private final BoardService boardService; | ||
| private final ArticleService articleService; | ||
|
|
||
| public BoardController(BoardService boardService) { | ||
| public BoardController(BoardService boardService, ArticleService articleService) { | ||
| this.boardService = boardService; | ||
| this.articleService = articleService; | ||
| } | ||
|
|
||
| @GetMapping("/boards") | ||
|
|
@@ -34,13 +33,19 @@ public List<BoardResponse> getBoards() { | |
| public BoardResponse getBoard( | ||
| @PathVariable Long id | ||
| ) { | ||
| if (boardService.getBoards().stream().noneMatch(res -> res.id().equals(id))) { | ||
| throw new RestApiException(CommonErrorCode.RESOURCE_NOT_FOUND); | ||
| } | ||
| return boardService.getBoardById(id); | ||
| } | ||
|
|
||
| @PostMapping("/boards") | ||
| public BoardResponse createBoard( | ||
| @RequestBody BoardCreateRequest request | ||
| ) { | ||
| if (request.name() == null) { | ||
| throw new RestApiException(CommonErrorCode.NULL_PARAMETER); | ||
| } | ||
| return boardService.createBoard(request); | ||
| } | ||
|
|
||
|
|
@@ -56,6 +61,11 @@ public BoardResponse updateBoard( | |
| public ResponseEntity<Void> deleteBoard( | ||
| @PathVariable Long id | ||
| ) { | ||
| if (articleService.getArticles() | ||
| .stream() | ||
| .anyMatch(res -> res.boardId().equals(id))) { | ||
| throw new RestApiException(BoardErrorCode.ARTICLE_EXISTENCE); | ||
| } | ||
| boardService.deleteBoard(id); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,27 @@ | ||
| package com.example.demo.controller; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.example.demo.controller.dto.request.MemberCreateRequest; | ||
| import com.example.demo.controller.dto.request.MemberUpdateRequest; | ||
| import com.example.demo.controller.dto.response.MemberResponse; | ||
| import com.example.demo.exception.RestApiException; | ||
| import com.example.demo.exception.error.CommonErrorCode; | ||
| import com.example.demo.exception.error.MemberErrorCode; | ||
| import com.example.demo.service.ArticleService; | ||
| import com.example.demo.service.MemberService; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| public class MemberController { | ||
|
|
||
| private final MemberService memberService; | ||
| private final ArticleService articleService; | ||
|
|
||
| public MemberController(MemberService memberService) { | ||
| public MemberController(MemberService memberService, ArticleService articleService) { | ||
| this.memberService = memberService; | ||
| this.articleService = articleService; | ||
| } | ||
|
|
||
| @GetMapping("/members") | ||
|
|
@@ -33,33 +32,53 @@ public ResponseEntity<List<MemberResponse>> getMembers() { | |
|
|
||
| @GetMapping("/members/{id}") | ||
| public ResponseEntity<MemberResponse> getMember( | ||
| @PathVariable Long id | ||
| @PathVariable Long id | ||
| ) { | ||
| if (memberService.getAll().stream().noneMatch(res -> res.id().equals(id))) { | ||
| throw new RestApiException(CommonErrorCode.RESOURCE_NOT_FOUND); | ||
| } | ||
| MemberResponse response = memberService.getById(id); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @PostMapping("/members") | ||
| public ResponseEntity<MemberResponse> create( | ||
| @RequestBody MemberCreateRequest request | ||
| @RequestBody MemberCreateRequest request | ||
| ) { | ||
| boolean isNullExistence = request.name() == null | ||
| || request.email() == null | ||
| || request.password() == null; | ||
|
||
| if (isNullExistence) { | ||
| throw new RestApiException(CommonErrorCode.NULL_PARAMETER); | ||
| } | ||
| MemberResponse response = memberService.create(request); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @PutMapping("/members/{id}") | ||
| public ResponseEntity<MemberResponse> updateMember( | ||
| @PathVariable Long id, | ||
| @RequestBody MemberUpdateRequest request | ||
| @PathVariable Long id, | ||
| @RequestBody MemberUpdateRequest request | ||
| ) { | ||
| if (memberService.getAll() | ||
| .stream() | ||
| .filter(res -> !res.id().equals(id)) | ||
| .anyMatch(res -> res.email().equals(request.email()))) { | ||
| throw new RestApiException(MemberErrorCode.EMAIL_CONFLICT); | ||
| } | ||
| MemberResponse response = memberService.update(id, request); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @DeleteMapping("/members/{id}") | ||
| public ResponseEntity<Void> deleteMember( | ||
| @PathVariable Long id | ||
| @PathVariable Long id | ||
| ) { | ||
| if (articleService.getArticles() | ||
| .stream() | ||
| .anyMatch(res -> res.authorId().equals(id))) { | ||
| throw new RestApiException(MemberErrorCode.ARTICLE_EXISTENCE); | ||
| } | ||
| memberService.delete(id); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package com.example.demo.exception; | ||
|
|
||
| public record ErrorResponse(String status, String message) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.example.demo.exception; | ||
|
|
||
| import com.example.demo.exception.error.ErrorCode; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
| import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
|
||
| @ExceptionHandler(RestApiException.class) | ||
| public ResponseEntity<ErrorResponse> handleCustomException(RestApiException e) { | ||
| ErrorCode errorCode = e.getErrorCode(); | ||
| return handleExceptionInternal(errorCode); | ||
| } | ||
|
|
||
| private ErrorResponse makeErrorResponse(ErrorCode errorCode) { | ||
| return new ErrorResponse(errorCode.getHttpStatus().toString(), errorCode.getMessage()); | ||
| } | ||
|
|
||
| private ResponseEntity<ErrorResponse> handleExceptionInternal(ErrorCode errorCode) { | ||
| ErrorResponse rsp = makeErrorResponse(errorCode); | ||
| return ResponseEntity.status(errorCode.getHttpStatus()).body(rsp); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.example.demo.exception; | ||
|
|
||
| import com.example.demo.exception.error.ErrorCode; | ||
|
|
||
| public class RestApiException extends RuntimeException{ | ||
|
|
||
| private final ErrorCode errorCode; | ||
|
|
||
| public RestApiException(ErrorCode errorCode) { | ||
| this.errorCode = errorCode; | ||
| } | ||
|
|
||
| public ErrorCode getErrorCode() { | ||
| return errorCode; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.example.demo.exception.error; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public enum ArticleErrorCode implements ErrorCode{ | ||
|
|
||
| REFERENCE_ERROR(HttpStatus.BAD_REQUEST, "존재하지 않는 사용자 또는 게시판입니다."); | ||
|
|
||
| private final HttpStatus httpStatus; | ||
| private final String message; | ||
|
|
||
| ArticleErrorCode(HttpStatus httpStatus, String message) { | ||
| this.httpStatus = httpStatus; | ||
| this.message = message; | ||
| } | ||
|
|
||
| @Override | ||
| public HttpStatus getHttpStatus() { | ||
| return httpStatus; | ||
| } | ||
|
|
||
| @Override | ||
| public String getMessage() { | ||
| return message; | ||
| } | ||
|
|
||
| } |
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.
지금 공통적으로
"/articles"가 있는데@RequestMapping("/articles")를 해놓으면 해당하는 클래스에 공통적으로 적용돼서 뺄수 있습니다!