-
Notifications
You must be signed in to change notification settings - Fork 12
[Spring Core] 이동훈 과제 제출합니다. #6
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 all commits
e5ef0aa
7f2df35
d0de246
21325eb
6c106f1
108de36
2f77718
9783288
45f4efb
19bdd83
90ddaaf
d79cd6e
d8ff571
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 |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ bin/ | |
| out/ | ||
| !**/src/main/**/out/ | ||
| !**/src/test/**/out/ | ||
| *.yml | ||
|
|
||
| ### NetBeans ### | ||
| /nbproject/private/ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,10 @@ | |
| import java.net.URI; | ||
| import java.util.List; | ||
|
|
||
| import com.example.demo.validate.ArticleValidate; | ||
| import com.example.demo.validate.BoardValidate; | ||
| import com.example.demo.validate.MemberValidate; | ||
| import jakarta.validation.Valid; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
|
|
@@ -22,47 +26,66 @@ | |
| public class ArticleController { | ||
|
|
||
| private final ArticleService articleService; | ||
| private final ArticleValidate articleValidate; | ||
| private final BoardValidate boardValidate; | ||
| private final MemberValidate memberValidate; | ||
|
|
||
| public ArticleController(ArticleService articleService) { | ||
| public ArticleController(ArticleService articleService, | ||
| ArticleValidate articleValidate, | ||
| BoardValidate boardValidate, | ||
| MemberValidate memberValidate) { | ||
| this.articleService = articleService; | ||
| this.articleValidate = articleValidate; | ||
| this.boardValidate = boardValidate; | ||
| this.memberValidate = memberValidate; | ||
| } | ||
|
|
||
| @GetMapping("/articles") | ||
| public ResponseEntity<List<ArticleResponse>> getArticles( | ||
| @RequestParam Long boardId | ||
| @RequestParam Long boardId | ||
| ) { | ||
| List<ArticleResponse> response = articleService.getByBoardId(boardId); | ||
|
|
||
| articleValidate.validateResponseIsEmpty(response); | ||
|
|
||
| return ResponseEntity.ok(response); | ||
| } | ||
|
Comment on lines
43
to
52
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. controller에
Author
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. 그러면 컨트롤러에서는 validateResponseIsEmpty 같은 메소드를 사용하는 것이 아니라 validateCreateArticle, validateUpdateArticle과 같은 메소드를 호출하게 만들고 그 메소드 속에서 validateResponseIsEmpty를 호출하게 만들면 나중에 검증 로직에 수정사항이 있을 때 컨트롤러에서 수정할 일이 없을 것 같습니다! 위와 같은 방식으로 수정해두겠습니다. |
||
|
|
||
| @GetMapping("/articles/{id}") | ||
| public ResponseEntity<ArticleResponse> getArticle( | ||
| @PathVariable Long id | ||
| @PathVariable Long id | ||
| ) { | ||
| articleValidate.validateContainId(id); | ||
|
|
||
| ArticleResponse response = articleService.getById(id); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @PostMapping("/articles") | ||
| public ResponseEntity<ArticleResponse> crateArticle( | ||
| @RequestBody ArticleCreateRequest request | ||
| @Valid @RequestBody ArticleCreateRequest request | ||
| ) { | ||
| boardValidate.validateCreateContainId(request.boardId()); | ||
| memberValidate.validateCreateContainId(request.authorId()); | ||
|
|
||
| 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, | ||
| @Valid @RequestBody ArticleUpdateRequest request | ||
| ) { | ||
| boardValidate.validateUpdateContainId(request.boardId()); | ||
|
|
||
| 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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.example.demo.controller; | ||
|
|
||
| import com.example.demo.controller.dto.request.MemberCreateRequest; | ||
| import com.example.demo.controller.dto.request.LogInRequest; | ||
| import com.example.demo.security.JwtUtil; | ||
| import com.example.demo.service.AuthService; | ||
| import com.example.demo.validate.MemberValidate; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
|
|
||
| @Controller | ||
| public class AuthController { | ||
| private final AuthService authService; | ||
| private final MemberValidate memberValidate; | ||
| private final JwtUtil jwtUtil; | ||
|
|
||
| AuthController(AuthService authService, | ||
| MemberValidate memberValidate, | ||
| JwtUtil jwtUtil) { | ||
| this.authService = authService; | ||
| this.memberValidate = memberValidate; | ||
| this.jwtUtil = jwtUtil; | ||
| } | ||
|
|
||
| @PostMapping("/register") | ||
| public ResponseEntity<String> signUp(@RequestBody MemberCreateRequest request) { | ||
| memberValidate.validateExistEmail(request.email()); | ||
| authService.register(request); | ||
|
|
||
| System.out.println("회원가입 성공"); | ||
| return ResponseEntity.status(201).build(); | ||
| } | ||
|
|
||
| @PostMapping("/login") | ||
| public ResponseEntity<String> signIn(HttpServletResponse httpServletResponse, @RequestBody LogInRequest request) { | ||
| authService.logIn(request); | ||
|
|
||
| String token = jwtUtil.createJwt(request.email(), "USER", 60*60*1000L); | ||
| httpServletResponse.addHeader("Authorization", "Bearer " + token); | ||
| System.out.println("로그인 성공"); | ||
|
|
||
| return ResponseEntity.status(200).build(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record ArticleCreateRequest( | ||
| Long authorId, | ||
| Long boardId, | ||
| String title, | ||
| String description | ||
| @NotNull Long authorId, | ||
| @NotNull Long boardId, | ||
| @NotNull String title, | ||
| @NotNull String description | ||
| ) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record ArticleUpdateRequest( | ||
| Long boardId, | ||
| String title, | ||
| String description | ||
| ) { | ||
| @NotNull Long boardId, | ||
| @NotNull String title, | ||
| @NotNull String description | ||
| ) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record BoardCreateRequest( | ||
| String name | ||
| @NotNull String name | ||
| ) { | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record BoardUpdateRequest( | ||
| String name | ||
| @NotNull String name | ||
| ) { | ||
|
|
||
| } |
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.
적용 중이던 .gitignore에 추가로 작성하면 적용이 안됩니다! 방법을 찾아보세요..!