-
Notifications
You must be signed in to change notification settings - Fork 2
Homework #1
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: homework
Are you sure you want to change the base?
Homework #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,26 @@ public ApiResponse<?> postDailies(@AuthenticationPrincipal UserDetails userDetai | |
| return ApiResponse.createSuccessWithNoContent(); // 공통 API를 반환하기 위한 ApiResponse 객체 사용 | ||
| } | ||
|
|
||
| // Daily 수정 | ||
| @PutMapping("/daily/{dailyId}") | ||
| @ResponseStatus(HttpStatus.OK) | ||
| public ApiResponse<?> updateDaily(@AuthenticationPrincipal UserDetails userDetails, @PathVariable Long dailyId, @Valid @RequestBody DailyDTO.RequestDTO requestDTO) { | ||
|
|
||
| dailyService.updateDaily(userDetails, dailyId, requestDTO); | ||
|
|
||
| return ApiResponse.createSuccessWithNoContent(); // 공통 API를 반환하기 위한 ApiResponse 객체 사용 | ||
| } | ||
|
|
||
| // Daily 삭제 | ||
| @PostMapping("/daily-delete/{dailyId}") | ||
|
Owner
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. 현재 Daily 삭제간 @PostMapping을 사용해서 Delete를 수행하고 있습니다. 그렇기에 uri에 직접적으로 행위인 "delete"를 적어줄 수 밖에 없습니다. Spring에서 REST API는 행위에 대한 명세는 HTTP method를 통해 표시하는걸 권장합니다. 그렇기에 @DeleteMapping 어노테이션을 사용하여 직접적으로 삭제하는 API임을 Client에게 설명해주는 것이 좋아보입니다. |
||
| @ResponseStatus(HttpStatus.OK) | ||
| public ApiResponse<?> deleteDaily(@PathVariable Long dailyId) { | ||
|
|
||
| dailyService.deleteDaily(dailyId); | ||
|
|
||
| return ApiResponse.createSuccessWithNoContent(); // 공통 API를 반환하기 위한 ApiResponse 객체 사용 | ||
| } | ||
|
|
||
| // 좋아요 기능 | ||
| @PostMapping("/daily-like/{dailyId}") | ||
| @ResponseStatus(HttpStatus.OK) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,4 +27,7 @@ public static Daily of(DailyDTO.RequestDTO dailyDTO, Member member) { | |
| .build(); | ||
| } | ||
|
|
||
| public void updateDaily(DailyDTO.RequestDTO dailyDTO) { | ||
|
Owner
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. DailyDTO.RequestDTO 객체의 isItIsPublic 메서드의 이름의 가독성이 조금 떨어지는 것 같습니다. 간단하게 isPublic()으로 변경해도 좋아보입니다. 아니면 특별히 해당 네이밍을 하신 이유가 있으신지 궁금합니다. |
||
| this.isPublic = dailyDTO.isItIsPublic(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,14 @@ | |
| import com.HelloWorld.Daily.dto.DailyDTO; | ||
| import jakarta.persistence.*; | ||
| import lombok.*; | ||
| import org.hibernate.annotations.DynamicUpdate; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @AllArgsConstructor | ||
| @DynamicUpdate | ||
| public class DailyContent extends Common { | ||
|
|
||
| @Id | ||
|
|
@@ -41,4 +43,34 @@ public static DailyContent of(Daily daily, DailyDTO.RequestDTO requestDTO){ | |
| .penitence3(requestDTO.getPenitence3()) | ||
| .build(); | ||
| } | ||
|
|
||
| public void updateDailyContent(DailyDTO.RequestDTO requestDTO) { | ||
| if (requestDTO.getThanks1() != null) { | ||
| this.thanks1 = requestDTO.getThanks1(); | ||
| } | ||
| if (requestDTO.getThanks2() != null) { | ||
| this.thanks2 = requestDTO.getThanks2(); | ||
| } | ||
| if (requestDTO.getThanks3() != null) { | ||
| this.thanks3 = requestDTO.getThanks3(); | ||
| } | ||
| if (requestDTO.getPenitence1() != null) { | ||
| this.penitence1 = requestDTO.getPenitence1(); | ||
| } | ||
| if (requestDTO.getPenitence2() != null) { | ||
| this.penitence2 = requestDTO.getPenitence2(); | ||
| } | ||
| if (requestDTO.getPenitence3() != null) { | ||
| this.penitence3 = requestDTO.getPenitence3(); | ||
| } | ||
| } | ||
|
|
||
| public void deleteDailyContent(DailyDTO.RequestDTO requestDTO) { | ||
|
Owner
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. Entity에 deleteDailyContent 메서드가 필요한 이유가 있을까요? |
||
| this.thanks1 = null; | ||
| this.thanks2 = null; | ||
| this.thanks3 = null; | ||
| this.penitence1 = null; | ||
| this.penitence2 = null; | ||
| this.penitence3 = null; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| import com.HelloWorld.Daily.repository.*; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.security.core.userdetails.User; | ||
| import org.springframework.security.core.userdetails.UserDetails; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
@@ -86,6 +87,40 @@ private void saveEntityAboutDaily(Member member, DailyDTO.RequestDTO requestDTO) | |
| dailyContentRepository.save(DailyContent.of(daily, requestDTO)); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void updateDaily(UserDetails userDetails, Long dailyId, DailyDTO.RequestDTO requestDTO) { | ||
| // if (userDetails == null) { | ||
|
Owner
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. 유저 권한 분기 로직을 주석 처리 한 이유가 궁금합니다! |
||
| // throw new NotExistMemberException(MessageCode.DOES_NOT_EXIST_MEMBER.getMessage()); | ||
| // } | ||
|
|
||
| Daily daily = dailyRepository.findById(dailyId) | ||
| .orElseThrow(() -> new NotExistMemberException(MessageCode.DOES_NOT_EXIST_MEMBER.getMessage())); | ||
|
|
||
| DailyContent dailyContent = dailyContentRepository.selectDailyContentByDaily(dailyId); | ||
|
|
||
| // String userName = userDetails.getUsername(); | ||
| // Member member = memberRepository.findByUserName(userName) | ||
| // .orElseThrow(() -> new NotExistMemberException(MessageCode.DOES_NOT_EXIST_MEMBER.getMessage())); | ||
|
|
||
| daily.updateDaily(requestDTO); | ||
| dailyContent.updateDailyContent(requestDTO); | ||
| } | ||
|
|
||
| @Transactional | ||
|
Owner
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. 현재 저희 Daily 프로젝트에서는 Runtime시 발생하는 예외들에 대해 RestExceptionHandler 객체를 이용해 핸들링 하고 있습니다. 이 방식을 통해 RuntimeException을 상속받아 더 명시적인 Custom Exception을 만들 수 있습니다. |
||
| public void deleteDaily(Long dailyId) { | ||
| if (dailyLikeRepository.existsById(dailyId)) { | ||
| dailyLikeRepository.deleteById(dailyId); | ||
| } | ||
| else { throw new RuntimeException("DailyLike not found with id: " + dailyId); } | ||
| if (dailyContentRepository.existsById(dailyId)) { | ||
| dailyContentRepository.deleteById(dailyId); | ||
| } | ||
| else { throw new RuntimeException("DailyContent not found with id: " + dailyId); } | ||
| if (dailyRepository.existsById(dailyId)) { | ||
| dailyRepository.deleteById(dailyId); | ||
| } | ||
| else { throw new RuntimeException("Daily not found with id: " + dailyId); } | ||
| } | ||
|
|
||
| // ResponseDTO 조회 및 객체화 | ||
| private DailyDTO.ResponseDTO getResponseDTO(Daily daily, String memberName){ | ||
|
|
||
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.
이 때 @ResponseStatus 어노테이션을 통해 성공시 200 반환 코드를 보내주고 있습니다. 그러나 PUT 메서드의 API의 경우 201 코드를 반환 해주는 것이 더 바람직 합니다.