-
Notifications
You must be signed in to change notification settings - Fork 0
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
3차 세미나 실습 과제 #13
base: main
Are you sure you want to change the base?
3차 세미나 실습 과제 #13
Changes from all commits
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,9 +1,13 @@ | ||
package org.sopt.practice.common.dto; | ||
|
||
public record SuccessStatusResponse( | ||
int status, String message | ||
int status, String message, Object data | ||
) { | ||
public static SuccessStatusResponse of(SuccessMessage successMessage){ | ||
return new SuccessStatusResponse(successMessage.getStatus(), successMessage.getMessage()); | ||
return new SuccessStatusResponse(successMessage.getStatus(), successMessage.getMessage(), null); | ||
} | ||
public static SuccessStatusResponse of(int status, String message, Object data) { | ||
return new SuccessStatusResponse(status, message, data); | ||
} | ||
Comment on lines
+9
to
+11
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. Object대신 제네릭을 사용해보시는 것을 추천드립니다! |
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.sopt.practice.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.sopt.practice.common.dto.SuccessMessage; | ||
import org.sopt.practice.common.dto.SuccessStatusResponse; | ||
import org.sopt.practice.domain.Post; | ||
import org.sopt.practice.dto.post.PostCreateRequest; | ||
import org.sopt.practice.dto.post.PostResponse; | ||
import org.sopt.practice.service.PostService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1") | ||
@RequiredArgsConstructor | ||
public class PostController { | ||
|
||
private final PostService postService; | ||
|
||
@PostMapping("/post") | ||
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. rest api에서는 단수형보다 복수형이 더 선호된다고 알고 있습니다! 그리고 글 작성시에 /api/v1/post로 보내시고 있는데 |
||
public ResponseEntity<SuccessStatusResponse> writePost(@RequestHeader Long blogId, @RequestHeader Long memberId, | ||
@Valid @RequestBody PostCreateRequest postCreateRequest) { | ||
return ResponseEntity.status(HttpStatus.CREATED).header("Location", postService.writePost(blogId, memberId, postCreateRequest)) | ||
.body(SuccessStatusResponse.of(SuccessMessage.POST_CREATE_SUCCESS)); | ||
Comment on lines
+28
to
+29
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. status와 헤더를 직접 입력하는 대신에 .created를 사용해보아도 좋을 것 같아요 |
||
} | ||
|
||
@GetMapping("/post") | ||
public ResponseEntity<SuccessStatusResponse> findAllPostsByBlogId(@RequestHeader Long blogId, @RequestHeader Long memberId) { | ||
List<Post> posts = postService.findAllPostsByBlogId(blogId, memberId); | ||
List<PostResponse> responses = posts.stream() | ||
.map(post -> new PostResponse(post.getId(), post.getTitle(), post.getContent())) | ||
.collect(Collectors.toList()); | ||
Comment on lines
+35
to
+37
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. dto로의 변환 작업은 service layer에서 수행하는 것이 어떨까요? 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. 더해서 PostResponse에서 생성자가 아닌 정적 팩토리 메서드 패턴으로 매개변수로 Post 자체를 받으면 |
||
return ResponseEntity.ok(SuccessStatusResponse.of( | ||
SuccessMessage.POST_ALL_FIND_SUCCESS.getStatus(), | ||
SuccessMessage.POST_ALL_FIND_SUCCESS.getMessage(), | ||
responses | ||
)); | ||
} | ||
|
||
@GetMapping("/post/{postId}") | ||
public ResponseEntity<SuccessStatusResponse> getPostById(@RequestHeader Long blogId, @RequestHeader Long memberId, | ||
@PathVariable Long postId) { | ||
Post post = postService.findPostById(blogId, memberId, postId); | ||
PostResponse postResponse = new PostResponse(post.getId(), post.getTitle(), post.getContent()); | ||
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. 여기도 마찬가지로 service에서 변환해주는 것이 좋을 것 같습니다. |
||
return ResponseEntity.ok(new SuccessStatusResponse( | ||
SuccessMessage.POST_FIND_SUCCESS.getStatus(), | ||
SuccessMessage.POST_FIND_SUCCESS.getMessage(), | ||
postResponse | ||
)); | ||
Comment on lines
+50
to
+54
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. Response에도 정적 팩터리 메서드 패턴을 사용해도 좋을 것 같아요! |
||
} | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package org.sopt.practice.dto; | ||
package org.sopt.practice.dto.blog; | ||
|
||
public record BlogCreateRequest(String title, String description) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.sopt.practice.dto.blog; | ||
|
||
import jakarta.validation.constraints.Size; | ||
|
||
public record BlogTitleUpdateRequest( | ||
@Size(max = 5, message = "블로그 제목이 최대 글자 수(5자)를 초과했습니다.") | ||
String title | ||
) { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.sopt.practice.dto.member; | ||
|
||
import org.sopt.practice.domain.Part; | ||
|
||
public record MemberCreateDto(String name, Part part, int age) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.sopt.practice.dto.post; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record PostCreateRequest( | ||
@NotBlank(message = "제목을 입력해주세요.") | ||
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. @notblank 어노테이션은 처음 보는데 입력이 공백으로 처리되는 걸 막아주는 건가요? 궁금해서 남깁니당 |
||
@Size(max = 100, message = "제목은 100자 이하로 작성해주세요.") | ||
String title, | ||
@NotBlank(message = "내용을 입력해주세요.") | ||
@Size(max = 1000, message = "내용은 1000자 이하로 작성해주세요.") | ||
String content) { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.sopt.practice.dto.post; | ||
|
||
public record PostResponse(Long id, String title, String content) { | ||
} |
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.
위에 작성하신 것처럼 SuccessMessage로 캡슐화해서 넘겨주는 것은 어떨까요?