-
Notifications
You must be signed in to change notification settings - Fork 12
[Spring Core] 신관규 과제 제출합니다. #14
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
b2f24ee
9db00f1
a1286bc
4c61209
c14562b
48743a0
9faed4e
57ba800
e3af0d3
e15f15f
71156f5
c51d743
9f4c138
02800bb
de1db1e
27c74e5
8856214
da4c91d
1807914
25b516d
2187c91
5826784
fd80235
c6965ad
e227a2e
48864b1
461422f
a3015bb
811f597
40513f8
2d87235
e4a3f4a
12201a3
eb0afbf
afa3ce9
989fbe3
266f23a
175404d
84ecd97
b5a9a86
996fff8
452b487
1eb19a3
520dcec
1e7596e
3206275
bf2ecb1
4a5f493
1248456
ac02295
14574b6
41b53df
8f41770
28d85d0
4c751e5
7301e71
f80d614
82a7c51
0b43b71
ec6e866
f17f522
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 @@ | ||
| package com.example.demo.config; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
|
||
| @Configuration | ||
| public class SecurityConfig { | ||
|
|
||
| @Bean | ||
| public PasswordEncoder passwordEncoder() { | ||
| return new BCryptPasswordEncoder(); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.example.demo.config; | ||
|
|
||
| import com.example.demo.interceptor.LoginCheckInterceptor; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
|
||
| @Configuration | ||
| public class WebConfig implements WebMvcConfigurer { | ||
|
|
||
| @Override | ||
| public void addInterceptors(InterceptorRegistry registry) { | ||
| registry.addInterceptor(new LoginCheckInterceptor()) | ||
| .order(1) | ||
| .addPathPatterns("/**") | ||
| .excludePathPatterns("/login", "/register", "/"); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,15 @@ | ||
| 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.service.ArticleService; | ||
| import jakarta.validation.Valid; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| public class ArticleController { | ||
|
|
@@ -29,42 +22,42 @@ public ArticleController(ArticleService articleService) { | |
|
|
||
| @GetMapping("/articles") | ||
| public ResponseEntity<List<ArticleResponse>> getArticles( | ||
| @RequestParam Long boardId | ||
| @RequestParam Long boardId | ||
|
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. 들여쓰신 이유가 있나용?
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. 인텔리제이 코드 정렬 기능(?)을 사용하니 들여쓰기가 들어간거 같습니다. |
||
| ) { | ||
| List<ArticleResponse> response = articleService.getByBoardId(boardId); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @GetMapping("/articles/{id}") | ||
| public ResponseEntity<ArticleResponse> getArticle( | ||
| @PathVariable Long id | ||
| @PathVariable Long id | ||
| ) { | ||
| ArticleResponse response = articleService.getById(id); | ||
| ArticleResponse response = articleService.getByArticleId(id); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @PostMapping("/articles") | ||
| public ResponseEntity<ArticleResponse> crateArticle( | ||
| @RequestBody ArticleCreateRequest request | ||
| public ResponseEntity<ArticleResponse> createArticle( | ||
| @Valid @RequestBody ArticleCreateRequest request | ||
| ) { | ||
| ArticleResponse response = articleService.create(request); | ||
| ArticleResponse response = articleService.createArticle(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 | ||
| ) { | ||
| ArticleResponse response = articleService.update(id, request); | ||
| ArticleResponse response = articleService.updateArticle(id, request); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| @DeleteMapping("/articles/{id}") | ||
| public ResponseEntity<Void> updateArticle( | ||
| @PathVariable Long id | ||
| public ResponseEntity<Void> deleteArticle( | ||
| @PathVariable Long id | ||
| ) { | ||
| articleService.delete(id); | ||
| articleService.deleteArticle(id); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.example.demo.controller; | ||
|
|
||
| import com.example.demo.controller.dto.request.LoginRequest; | ||
| import com.example.demo.controller.dto.response.MemberResponse; | ||
| import com.example.demo.service.LoginService; | ||
| import jakarta.servlet.http.Cookie; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import jakarta.validation.Valid; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.stereotype.Controller; | ||
| import org.springframework.web.bind.annotation.ModelAttribute; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| @Controller | ||
| public class LoginController { | ||
|
|
||
| private final LoginService loginService; | ||
|
|
||
| public LoginController(LoginService loginService) { | ||
| this.loginService = loginService; | ||
| } | ||
|
|
||
| @PostMapping("/login") | ||
| public ResponseEntity<MemberResponse> login( | ||
| @Valid @ModelAttribute LoginRequest loginRequest, | ||
| HttpServletResponse response, | ||
| HttpServletRequest request | ||
| ) throws IOException { | ||
| MemberResponse member = loginService.login(loginRequest); | ||
| if (member == null) { | ||
| response.sendRedirect(request.getContextPath()); | ||
| return ResponseEntity.badRequest().build(); | ||
| } | ||
|
|
||
| Cookie cookie = new Cookie("memberId", String.valueOf(member.id())); | ||
| cookie.setHttpOnly(true); | ||
| cookie.setSecure(true); | ||
|
|
||
| response.addCookie(cookie); | ||
| response.sendRedirect(request.getContextPath() + "/main"); | ||
| return ResponseEntity.ok(member); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,22 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| import com.example.demo.domain.Article; | ||
| import com.example.demo.domain.Board; | ||
| import com.example.demo.domain.Member; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record ArticleCreateRequest( | ||
| Long authorId, | ||
| Long boardId, | ||
| String title, | ||
| String description | ||
| @NotNull(message = "회원 아이디는 필수로 입력해야 합니다.") Long authorId, | ||
|
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. 어노테이션의 적극적인 사용..! |
||
| @NotNull(message = "게시판 아이디는 필수로 입력해야 합니다.") Long boardId, | ||
| @NotNull(message = "제목은 필수로 입력해야 합니다.") String title, | ||
| @NotNull(message = "게시물 내용은 필수로 입력해야 합니다.") String description | ||
| ) { | ||
|
|
||
| public Article toEntity(Board board, Member member) { | ||
| return Article.builder() | ||
| .member(member) | ||
| .board(board) | ||
| .title(this.title) | ||
| .content(this.description) | ||
| .build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| package com.example.demo.controller.dto.request; | ||
|
|
||
| import com.example.demo.domain.Board; | ||
| import jakarta.validation.constraints.NotNull; | ||
|
|
||
| public record BoardCreateRequest( | ||
| String name | ||
| @NotNull(message = "게시판 이름은 필수로 입력해야 합니다.") String name | ||
| ) { | ||
|
|
||
| public Board toEntity() { | ||
| return new Board(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.
개인적인 의견이긴 한데, 같은 dependencies들은 묶고, 나머지는 띄어쓰기 하는 게 조금 더 보기 좋더라구요
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.
따로 생각을 안 하고 추가만 했었는데, 말씀해 주신 것처럼 가독성 좋게 바꿔보겠습니다.
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.
굿 반영 잘 되었네요