Skip to content
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

Third seminar 과제 제출 #6

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sopt.org.ThirdSeminar.controller.post;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

폴더 구조를 아예 도메인 별로 나눴네용~


import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import sopt.org.ThirdSeminar.common.dto.ApiResponseDto;
import sopt.org.ThirdSeminar.controller.post.dto.request.PostCreateDto;
import sopt.org.ThirdSeminar.exception.SuccessStatus;
import sopt.org.ThirdSeminar.service.PostService;

import javax.validation.Valid;

@RestController
@RequiredArgsConstructor
public class PostController {

private final PostService postService;

@PostMapping("/post/{userId}")
public ApiResponseDto create(@PathVariable final Long userId, @RequestBody @Valid final PostCreateDto postCreateDto) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P5) 저는 userId를 request dto에 포함시켰는데, path variable로 따로 받아오게 하셨군요! 혹시 이렇게 하신 이유가 무엇인지 궁금해요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사실 큰 의미는 없고,,보통 유저는 인증 토큰 정보? 를 이용해서 userId를 찾아오는 방식을 썼었는데, 아직 유저 인증을 구현을 안 했다 보니 그냥 @PathVariable로 받아왔어요! 근데 이렇게 보니 post/1 이런식으로 있으면 이게 userId인지 postId인지 혼동이 올 수 있을 것 같긴 하네요..

return ApiResponseDto.success(SuccessStatus.POST_CREATE_SUCCESS, postService.create(userId, postCreateDto));
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 코드 게시물 생성 api 인가요? userid를 뒤에 추가해서 생성 하는 방법도 있군요..!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위에 동섭이 코멘트에 답변한 거처럼 사실 큰 의미는 없고,,보통 유저는 인증 토큰 정보? 를 이용해서 userId를 찾아오는 방식을 썼었는데, 아직 유저 인증을 구현을 안 했다 보니 그냥 @PathVariable로 받아왔어요! 근데 이렇게 보니 post/1 이런식으로 있으면 이게 userId인지 postId인지 혼동이 올 수 있을 것 같긴 하네요..

@GetMapping("/post/{postId}")
public ApiResponseDto getOne(@PathVariable final Long postId) {
return ApiResponseDto.success(SuccessStatus.POST_INQUIRY_SUCCESS, postService.getOne(postId));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sopt.org.ThirdSeminar.controller.post.dto.request;

import lombok.Getter;

import javax.validation.constraints.NotEmpty;

@Getter
public class PostCreateDto {

@NotEmpty(message = "제목은 필수입니다.")
private String title;

@NotEmpty(message = "내용은 필수입니다.")
private String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sopt.org.ThirdSeminar.controller.post.dto.response;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class PostResponseDto {
private String title;
private String content;
private String authorName;

public static PostResponseDto of(String title, String content, String authorName) {
return new PostResponseDto(title, content, authorName);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package sopt.org.ThirdSeminar.controller;
package sopt.org.ThirdSeminar.controller.user;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import sopt.org.ThirdSeminar.common.dto.ApiResponseDto;
import sopt.org.ThirdSeminar.controller.dto.request.UserRequestDto;
import sopt.org.ThirdSeminar.controller.dto.response.UserResponseDto;
import sopt.org.ThirdSeminar.controller.user.dto.request.UserRequestDto;
import sopt.org.ThirdSeminar.controller.user.dto.response.UserResponseDto;
import sopt.org.ThirdSeminar.exception.SuccessStatus;
import sopt.org.ThirdSeminar.service.UserService;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sopt.org.ThirdSeminar.controller.dto.request;
package sopt.org.ThirdSeminar.controller.user.dto.request;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sopt.org.ThirdSeminar.controller.dto.response;
package sopt.org.ThirdSeminar.controller.user.dto.response;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long id;

@Column(nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ public enum SuccessStatus {
/*
user
*/
SIGNUP_SUCCESS(HttpStatus.CREATED, "회원가입이 완료되었습니다.");
SIGNUP_SUCCESS(HttpStatus.CREATED, "회원가입이 완료되었습니다."),

/*
post
*/
POST_CREATE_SUCCESS(HttpStatus.CREATED, "게시물 생성이 완료되었습니다."),
POST_INQUIRY_SUCCESS(HttpStatus.OK, "게시물 조회가 완료되었습니다.")
;

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sopt.org.ThirdSeminar.infrastructure;

import org.springframework.stereotype.Repository;
import sopt.org.ThirdSeminar.domain.Post;

import java.util.Optional;

@Repository
public interface PostRepository extends org.springframework.data.repository.Repository<Post, Long> {

void save(Post post);

Optional<Post> findById(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import org.springframework.data.repository.Repository;
import sopt.org.ThirdSeminar.domain.User;

import java.util.Optional;

public interface UserRepository extends Repository<User, Long> {

void save(User user);

Optional<User> findById(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sopt.org.ThirdSeminar.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.ThirdSeminar.controller.post.dto.request.PostCreateDto;
import sopt.org.ThirdSeminar.controller.post.dto.response.PostResponseDto;
import sopt.org.ThirdSeminar.domain.Post;
import sopt.org.ThirdSeminar.domain.User;
import sopt.org.ThirdSeminar.infrastructure.PostRepository;

@Service
@RequiredArgsConstructor
public class PostService {

private final PostRepository postRepository;
private final UserService userService;

@Transactional
public PostResponseDto create(Long userId, PostCreateDto postCreateDto) {
User userById = userService.findUserById(userId);

Post post = Post.builder()
.title(postCreateDto.getTitle())
.content(postCreateDto.getContent())
.user(userById)
.build();

postRepository.save(post);

return PostResponseDto.of(post.getTitle(), post.getContent(), userById.getNickname());
}

@Transactional(readOnly = true)
public PostResponseDto getOne(Long postId) {
Post post = postRepository.findById(postId).orElseThrow();

return PostResponseDto.of(post.getTitle(), post.getContent(), post.getUser().getNickname());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sopt.org.ThirdSeminar.controller.dto.request.UserRequestDto;
import sopt.org.ThirdSeminar.controller.dto.response.UserResponseDto;
import sopt.org.ThirdSeminar.controller.user.dto.request.UserRequestDto;
import sopt.org.ThirdSeminar.controller.user.dto.response.UserResponseDto;
import sopt.org.ThirdSeminar.domain.User;
import sopt.org.ThirdSeminar.infrastructure.UserRepository;

Expand All @@ -26,4 +26,8 @@ public UserResponseDto create(UserRequestDto request) {

return UserResponseDto.of(user.getId(), user.getNickname());
}

public User findUserById(Long userId) {
return userRepository.findById(userId).orElseThrow();
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orElseThrow 로 바로 날리는 방법도 있는 거 알아갑니다..!