diff --git a/src/main/java/io/sobok/SobokSobok/exception/ErrorCode.java b/src/main/java/io/sobok/SobokSobok/exception/ErrorCode.java index 771d020..35d2923 100644 --- a/src/main/java/io/sobok/SobokSobok/exception/ErrorCode.java +++ b/src/main/java/io/sobok/SobokSobok/exception/ErrorCode.java @@ -54,6 +54,7 @@ public enum ErrorCode { //sticker UNREGISTERED_STICKER(HttpStatus.NOT_FOUND, "등록되지 않은 스티커입니다."), ALREADY_SEND_STICKER(HttpStatus.CONFLICT, "이미 스티커를 전송했습니다."), + UNREGISTERED_LIKE_SCHEDULE(HttpStatus.NOT_FOUND, "스티커 전송기록이 존재하지 않습니다."), ; private final HttpStatus code; diff --git a/src/main/java/io/sobok/SobokSobok/exception/SuccessCode.java b/src/main/java/io/sobok/SobokSobok/exception/SuccessCode.java index 8c9e81a..df3d0db 100644 --- a/src/main/java/io/sobok/SobokSobok/exception/SuccessCode.java +++ b/src/main/java/io/sobok/SobokSobok/exception/SuccessCode.java @@ -45,6 +45,7 @@ public enum SuccessCode { //sticker GET_STICKER_LIST_SUCCESS(HttpStatus.OK, "스티커 전체 조회에 성공했습니다."), SEND_STICKER_SUCCESS(HttpStatus.OK, "스티커 전송에 성공했습니다."), + UPDATE_STICKER_SUCCESS(HttpStatus.OK, "보낸 스티커 수정에 성공했습니다."), ; private final HttpStatus code; diff --git a/src/main/java/io/sobok/SobokSobok/sticker/application/StickerService.java b/src/main/java/io/sobok/SobokSobok/sticker/application/StickerService.java index 616feac..1c60da5 100644 --- a/src/main/java/io/sobok/SobokSobok/sticker/application/StickerService.java +++ b/src/main/java/io/sobok/SobokSobok/sticker/application/StickerService.java @@ -6,6 +6,7 @@ import io.sobok.SobokSobok.exception.model.BadRequestException; import io.sobok.SobokSobok.exception.model.ConflictException; import io.sobok.SobokSobok.exception.model.ForbiddenException; +import io.sobok.SobokSobok.exception.model.NotFoundException; import io.sobok.SobokSobok.friend.infrastructure.FriendQueryRepository; import io.sobok.SobokSobok.pill.application.PillScheduleServiceUtil; import io.sobok.SobokSobok.pill.application.PillServiceUtil; @@ -16,7 +17,7 @@ import io.sobok.SobokSobok.sticker.domain.LikeSchedule; import io.sobok.SobokSobok.sticker.infrastructure.LikeScheduleRepository; import io.sobok.SobokSobok.sticker.infrastructure.StickerRepository; -import io.sobok.SobokSobok.sticker.ui.dto.SendStickerResponse; +import io.sobok.SobokSobok.sticker.ui.dto.StickerActionResponse; import io.sobok.SobokSobok.sticker.ui.dto.StickerResponse; import java.util.List; import java.util.stream.Collectors; @@ -45,7 +46,8 @@ public List getStickerList() { ).collect(Collectors.toList()); } - public SendStickerResponse sendSticker(Long userId, Long scheduleId, Long stickerId) { + @Transactional + public StickerActionResponse sendSticker(Long userId, Long scheduleId, Long stickerId) { UserServiceUtil.existsUserById(userRepository, userId); PillSchedule pillSchedule = PillScheduleServiceUtil.findPillScheduleById( pillScheduleRepository, scheduleId); @@ -73,8 +75,31 @@ public SendStickerResponse sendSticker(Long userId, Long scheduleId, Long sticke .build() ); - return SendStickerResponse.builder() - .id(likeSchedule.getId()) + return StickerActionResponse.builder() + .likeScheduleId(likeSchedule.getId()) + .scheduleId(likeSchedule.getScheduleId()) + .senderId(likeSchedule.getSenderId()) + .stickerId(likeSchedule.getStickerId()) + .createdAt(likeSchedule.getCreatedAt()) + .updatedAt(likeSchedule.getUpdatedAt()) + .build(); + } + + @Transactional + public StickerActionResponse updateSendSticker(Long userId, Long likeScheduleId, Long stickerId) { + UserServiceUtil.existsUserById(userRepository, userId); + LikeSchedule likeSchedule = likeScheduleRepository.findById(likeScheduleId) + .orElseThrow(() -> new NotFoundException(ErrorCode.UNREGISTERED_LIKE_SCHEDULE)); + StickerServiceUtil.existsStickerById(stickerRepository, stickerId); + + if(!likeSchedule.isLikeScheduleSender(userId)) { + throw new ForbiddenException(ErrorCode.FORBIDDEN_EXCEPTION); + } + + likeSchedule.changeSticker(stickerId); + + return StickerActionResponse.builder() + .likeScheduleId(likeSchedule.getId()) .scheduleId(likeSchedule.getScheduleId()) .senderId(likeSchedule.getSenderId()) .stickerId(likeSchedule.getStickerId()) diff --git a/src/main/java/io/sobok/SobokSobok/sticker/domain/LikeSchedule.java b/src/main/java/io/sobok/SobokSobok/sticker/domain/LikeSchedule.java index eb859d2..ea6eb93 100644 --- a/src/main/java/io/sobok/SobokSobok/sticker/domain/LikeSchedule.java +++ b/src/main/java/io/sobok/SobokSobok/sticker/domain/LikeSchedule.java @@ -6,6 +6,7 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import java.util.Objects; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -37,4 +38,12 @@ public LikeSchedule(Long scheduleId, Long senderId, Long stickerId) { this.senderId = senderId; this.stickerId = stickerId; } + + public void changeSticker(Long stickerId) { + this.stickerId = stickerId; + } + + public Boolean isLikeScheduleSender(Long userId) { + return Objects.equals(senderId, userId); + } } diff --git a/src/main/java/io/sobok/SobokSobok/sticker/ui/StickerController.java b/src/main/java/io/sobok/SobokSobok/sticker/ui/StickerController.java index 589af02..f04cd66 100644 --- a/src/main/java/io/sobok/SobokSobok/sticker/ui/StickerController.java +++ b/src/main/java/io/sobok/SobokSobok/sticker/ui/StickerController.java @@ -4,7 +4,7 @@ import io.sobok.SobokSobok.common.dto.ApiResponse; import io.sobok.SobokSobok.exception.SuccessCode; import io.sobok.SobokSobok.sticker.application.StickerService; -import io.sobok.SobokSobok.sticker.ui.dto.SendStickerResponse; +import io.sobok.SobokSobok.sticker.ui.dto.StickerActionResponse; import io.sobok.SobokSobok.sticker.ui.dto.StickerResponse; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -47,7 +48,7 @@ public ResponseEntity>> getStickerList() { summary = "스티커 전송 API 메서드", description = "스티커를 전송하는 메서드입니다." ) - public ResponseEntity> sendSticker( + public ResponseEntity> sendSticker( @AuthenticationPrincipal User user, @PathVariable Long scheduleId, @RequestParam Long stickerId @@ -59,4 +60,22 @@ public ResponseEntity> sendSticker( stickerService.sendSticker(user.getId(), scheduleId, stickerId) )); } + + @PutMapping("/my/{likeScheduleId}") + @Operation( + summary = "보낸 스티커 수정 API 메서드", + description = "보낸 스티커를 수정하는 메서드입니다." + ) + public ResponseEntity> updateSendSticker( + @AuthenticationPrincipal User user, + @PathVariable Long likeScheduleId, + @RequestParam Long stickerId + ) { + return ResponseEntity + .status(HttpStatus.OK) + .body(ApiResponse.success( + SuccessCode.UPDATE_STICKER_SUCCESS, + stickerService.updateSendSticker(user.getId(), likeScheduleId, stickerId) + )); + } } diff --git a/src/main/java/io/sobok/SobokSobok/sticker/ui/dto/SendStickerResponse.java b/src/main/java/io/sobok/SobokSobok/sticker/ui/dto/StickerActionResponse.java similarity index 79% rename from src/main/java/io/sobok/SobokSobok/sticker/ui/dto/SendStickerResponse.java rename to src/main/java/io/sobok/SobokSobok/sticker/ui/dto/StickerActionResponse.java index df24f30..9dd3b88 100644 --- a/src/main/java/io/sobok/SobokSobok/sticker/ui/dto/SendStickerResponse.java +++ b/src/main/java/io/sobok/SobokSobok/sticker/ui/dto/StickerActionResponse.java @@ -4,8 +4,8 @@ import lombok.Builder; @Builder -public record SendStickerResponse( - Long id, +public record StickerActionResponse( + Long likeScheduleId, Long scheduleId, Long senderId, Long stickerId,