Skip to content

Commit

Permalink
[Feat]: 보낸 스티커 수정 API 구현
Browse files Browse the repository at this point in the history
보낸 스티커 수정 API 구현

Related to: #76
  • Loading branch information
onpyeong committed Feb 6, 2024
1 parent 37562e5 commit 01226c8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/java/io/sobok/SobokSobok/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -45,7 +46,8 @@ public List<StickerResponse> 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);
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -47,7 +48,7 @@ public ResponseEntity<ApiResponse<List<StickerResponse>>> getStickerList() {
summary = "스티커 전송 API 메서드",
description = "스티커를 전송하는 메서드입니다."
)
public ResponseEntity<ApiResponse<SendStickerResponse>> sendSticker(
public ResponseEntity<ApiResponse<StickerActionResponse>> sendSticker(
@AuthenticationPrincipal User user,
@PathVariable Long scheduleId,
@RequestParam Long stickerId
Expand All @@ -59,4 +60,22 @@ public ResponseEntity<ApiResponse<SendStickerResponse>> sendSticker(
stickerService.sendSticker(user.getId(), scheduleId, stickerId)
));
}

@PutMapping("/my/{likeScheduleId}")
@Operation(
summary = "보낸 스티커 수정 API 메서드",
description = "보낸 스티커를 수정하는 메서드입니다."
)
public ResponseEntity<ApiResponse<StickerActionResponse>> 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)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import lombok.Builder;

@Builder
public record SendStickerResponse(
Long id,
public record StickerActionResponse(
Long likeScheduleId,
Long scheduleId,
Long senderId,
Long stickerId,
Expand Down

0 comments on commit 01226c8

Please sign in to comment.