Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

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
Expand Up @@ -55,6 +55,26 @@ public ApiResponse<?> postDailies(@AuthenticationPrincipal UserDetails userDetai
return ApiResponse.createSuccessWithNoContent(); // 공통 API를 반환하기 위한 ApiResponse 객체 사용
}

// Daily 수정
@PutMapping("/daily/{dailyId}")
@ResponseStatus(HttpStatus.OK)
Copy link
Copy Markdown
Owner

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 코드를 반환 해주는 것이 더 바람직 합니다.

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}")
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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)
Expand Down
3 changes: 3 additions & 0 deletions Daily/src/main/java/com/HelloWorld/Daily/entity/Daily.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ public static Daily of(DailyDTO.RequestDTO dailyDTO, Member member) {
.build();
}

public void updateDaily(DailyDTO.RequestDTO dailyDTO) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

DailyDTO.RequestDTO 객체의 isItIsPublic 메서드의 이름의 가독성이 조금 떨어지는 것 같습니다. 간단하게 isPublic()으로 변경해도 좋아보입니다. 아니면 특별히 해당 네이밍을 하신 이유가 있으신지 궁금합니다.

this.isPublic = dailyDTO.isItIsPublic();
}
}
32 changes: 32 additions & 0 deletions Daily/src/main/java/com/HelloWorld/Daily/entity/DailyContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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;
}
}
35 changes: 35 additions & 0 deletions Daily/src/main/java/com/HelloWorld/Daily/service/DailyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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){
Expand Down
Loading