diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 206efa8..70f2fff 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -8,7 +8,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 87a20fc..c7e4a35 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,9 @@ + + + diff --git a/.idea/modules.xml b/.idea/modules.xml index 0167d22..94496e3 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/Daily/src/main/java/com/HelloWorld/Daily/controller/DailyRestController.java b/Daily/src/main/java/com/HelloWorld/Daily/controller/DailyRestController.java index 61ffbd9..1c7ea5f 100644 --- a/Daily/src/main/java/com/HelloWorld/Daily/controller/DailyRestController.java +++ b/Daily/src/main/java/com/HelloWorld/Daily/controller/DailyRestController.java @@ -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}") + @ResponseStatus(HttpStatus.OK) + public ApiResponse> deleteDaily(@PathVariable Long dailyId) { + + dailyService.deleteDaily(dailyId); + + return ApiResponse.createSuccessWithNoContent(); // 공통 API를 반환하기 위한 ApiResponse 객체 사용 + } + // 좋아요 기능 @PostMapping("/daily-like/{dailyId}") @ResponseStatus(HttpStatus.OK) diff --git a/Daily/src/main/java/com/HelloWorld/Daily/entity/Daily.java b/Daily/src/main/java/com/HelloWorld/Daily/entity/Daily.java index 2513dd4..4d136bd 100644 --- a/Daily/src/main/java/com/HelloWorld/Daily/entity/Daily.java +++ b/Daily/src/main/java/com/HelloWorld/Daily/entity/Daily.java @@ -27,4 +27,7 @@ public static Daily of(DailyDTO.RequestDTO dailyDTO, Member member) { .build(); } + public void updateDaily(DailyDTO.RequestDTO dailyDTO) { + this.isPublic = dailyDTO.isItIsPublic(); + } } diff --git a/Daily/src/main/java/com/HelloWorld/Daily/entity/DailyContent.java b/Daily/src/main/java/com/HelloWorld/Daily/entity/DailyContent.java index af798c7..a296657 100644 --- a/Daily/src/main/java/com/HelloWorld/Daily/entity/DailyContent.java +++ b/Daily/src/main/java/com/HelloWorld/Daily/entity/DailyContent.java @@ -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) { + this.thanks1 = null; + this.thanks2 = null; + this.thanks3 = null; + this.penitence1 = null; + this.penitence2 = null; + this.penitence3 = null; + } } diff --git a/Daily/src/main/java/com/HelloWorld/Daily/service/DailyService.java b/Daily/src/main/java/com/HelloWorld/Daily/service/DailyService.java index 0dbb9bc..c5af1c0 100644 --- a/Daily/src/main/java/com/HelloWorld/Daily/service/DailyService.java +++ b/Daily/src/main/java/com/HelloWorld/Daily/service/DailyService.java @@ -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) { +// 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 + 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){ diff --git a/Daily/src/main/resources/templates/myPage.html b/Daily/src/main/resources/templates/myPage.html index c468198..be8a637 100644 --- a/Daily/src/main/resources/templates/myPage.html +++ b/Daily/src/main/resources/templates/myPage.html @@ -81,6 +81,69 @@ My Page + + + + + + + Daily 작성하기 + + + + + + 공개 여부 + + + 감사1 + + + + 감사2 + + + + 감사3 + + + + 회개1 + + + + 회개2 + + + + 회개3 + + + + + + + + + + + + + + Daily 삭제하기 + + + 정말 지우시겠습니까? + + + + + @@ -101,7 +164,7 @@ My Page // alert const Toast = Swal.mixin({ toast: true, - position: 'center-center', + position: 'center', showConfirmButton: false, timer: 3000, timerProgressBar: true, @@ -218,9 +281,26 @@ My Page "" + "" + postList.penitence3 + "" + "" + "" + "" + - "" + - "" + svgIcon + "" + - "" + postList.dailyLikeCnt + "" + + "" + + "" + + "" + svgIcon + "" + + "" + postList.dailyLikeCnt + "" + + "" + + "" + + "수정" + + "삭제" + + "" + "" + "" + "" + @@ -244,5 +324,174 @@ My Page } }); } + + // 모달이 열릴 때 이벤트를 감지하고 폼에 데이터를 채우는 함수 + $('#staticBackdrop').on('show.bs.modal', function (event) { + // 클릭된 버튼을 가져옴 + var button = $(event.relatedTarget); + + // 버튼에 저장된 데이터를 가져옴 + var itIsPublic = button.data('itIsPublic'); + var thanks1 = button.data('thanks1'); + var thanks2 = button.data('thanks2'); + var thanks3 = button.data('thanks3'); + var penitence1 = button.data('penitence1'); + var penitence2 = button.data('penitence2'); + var penitence3 = button.data('penitence3'); + + // 모달 내의 폼 필드를 가져옴 + var modal = $(this); + modal.find('#itIsPublic').val(itIsPublic); + modal.find('#thanks1').val(thanks1); + modal.find('#thanks2').val(thanks2); + modal.find('#thanks3').val(thanks3); + modal.find('#penitence1').val(penitence1); + modal.find('#penitence2').val(penitence2); + modal.find('#penitence3').val(penitence3); + }); + + function buttonCreate(dailyId) { + document.getElementById('buttonContainer').innerHTML = + `저장 + Close`; + } + + function checkButtonCreate(dailyId) { + console.log("checkButtonCreate " + dailyId); + document.getElementById('checkButtonContainer').innerHTML = + `삭제 + Close`; + } + + // Daily 수정내용 저장 ajax + function ajax_send(dailyId){ + + var dataSerialized = $("#daily-form"); + + var data = $("#daily-form").serializeObject(); + + var dataJson = JSON.stringify(data); + + $.ajax({ + url : '/daily/' + dailyId, + data : dataJson, + method : 'PUT', + contentType: 'application/json', + success : function(data){ + $("#daily-form")[0].reset(); + + $('#staticBackdrop').modal('hide'); + + Toast.fire({ + icon: 'success', + title: '정상적으로 등록하였습니다.' + }); + + $("#postList").empty(); + postListDisplay(0, 5); + }, + + error : function(response, status, error){ + + if (response.status == 401) { + Toast.fire({ + icon: 'error', + title: '로그인 후 글을 작성해주세요.' + }); + } + + if (response.status == 400) { + Toast.fire({ + icon: 'error', + title: '이미 오늘 작성된 글이 있습니다.' + }); + } + + if (response.status == 412) { + // 중복된 값을 제거하기 위한 Set 사용 + const uniqueMessages = new Set(Object.values(response.responseJSON.data)); + + // Toast 제목에 작성 + const toastTitle = Array.from(uniqueMessages).join(', '); + + Toast.fire({ + icon: 'error', + title: toastTitle + }); + } + + $("#daily-form")[0].reset(); + + $('#staticBackdrop').modal('hide'); + }, + }); + + } + + // 직렬화 메서드 + jQuery.fn.serializeObject = function() { + var obj = null; + try { + if (this[0].tagName && this[0].tagName.toUpperCase() == "FORM") { + var arr = this.serializeArray(); + if (arr) { + obj = {}; + jQuery.each(arr, function() { + obj[this.name] = this.value; + }); + } + } + } catch (e) { + alert(e.message); + } finally { + } + + return obj; + }; + + // Daily 삭제 ajax + function ajax_send_delete(dailyId){ + + var dataSerialized = $("#daily-form"); + + var data = $("#daily-form").serializeObject(); + + var dataJson = JSON.stringify(data); + + $.ajax({ + url : '/daily-delete/' + dailyId, + data : dataJson, + method : 'POST', + contentType: 'application/json', + success : function(data){ + $("#daily-form")[0].reset(); + + $('#deleteCheckDrop').modal('hide'); + + Toast.fire({ + icon: 'success', + title: '정상적으로 삭제하였습니다.' + }); + + $("#postList").empty(); + postListDisplay(0, 5); + }, + + error : function(response, status, error){ + + if (response.status == 401) { + Toast.fire({ + icon: 'error', + title: '로그인 후 글을 삭제해주세요.' + }); + } + + $("#daily-form")[0].reset(); + + $('#deleteCheckDrop').modal('hide'); + }, + }); + + }
정말 지우시겠습니까?
" + postList.penitence3 + "
" + postList.dailyLikeCnt + "