Skip to content
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

[Feat]: 스티커 전송 API 구현 #75

Merged
merged 1 commit into from
Feb 6, 2024
Merged
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
5 changes: 5 additions & 0 deletions src/main/java/io/sobok/SobokSobok/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public enum ErrorCode {
UNAUTHORIZED_PILL(HttpStatus.FORBIDDEN, "접근 권한이 없는 약입니다."),
NOT_SEND_PILL(HttpStatus.NOT_FOUND, "전송된 적이 없는 약입니다."),
UNREGISTERED_PILL_SCHEDULE(HttpStatus.NOT_FOUND, "등록되지 않은 약 일정입니다."),
UNCONSUMED_PILL(HttpStatus.BAD_REQUEST, "복용하지 않은 약입니다."),


// friend
Expand All @@ -49,6 +50,10 @@ public enum ErrorCode {

// external
INVALID_EXTERNAL_REQUEST_DATA(HttpStatus.BAD_REQUEST, "외부 API 요청에 잘못된 데이터가 전달됐습니다."),

//sticker
UNREGISTERED_STICKER(HttpStatus.NOT_FOUND, "등록되지 않은 스티커입니다."),
ALREADY_SEND_STICKER(HttpStatus.CONFLICT, "이미 스티커를 전송했습니다."),
;

private final HttpStatus code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum SuccessCode {

//sticker
GET_STICKER_LIST_SUCCESS(HttpStatus.OK, "스티커 전체 조회에 성공했습니다."),
SEND_STICKER_SUCCESS(HttpStatus.OK, "스티커 전송에 성공했습니다."),
;

private final HttpStatus code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
package io.sobok.SobokSobok.sticker.application;

import io.sobok.SobokSobok.auth.application.util.UserServiceUtil;
import io.sobok.SobokSobok.auth.infrastructure.UserRepository;
import io.sobok.SobokSobok.exception.ErrorCode;
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.friend.infrastructure.FriendQueryRepository;
import io.sobok.SobokSobok.pill.application.PillScheduleServiceUtil;
import io.sobok.SobokSobok.pill.application.PillServiceUtil;
import io.sobok.SobokSobok.pill.domain.Pill;
import io.sobok.SobokSobok.pill.domain.PillSchedule;
import io.sobok.SobokSobok.pill.infrastructure.PillRepository;
import io.sobok.SobokSobok.pill.infrastructure.PillScheduleRepository;
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.StickerResponse;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -13,6 +29,11 @@
public class StickerService {

private final StickerRepository stickerRepository;
private final UserRepository userRepository;
private final PillScheduleRepository pillScheduleRepository;
private final PillRepository pillRepository;
private final FriendQueryRepository friendQueryRepository;
private final LikeScheduleRepository likeScheduleRepository;

@Transactional
public List<StickerResponse> getStickerList() {
Expand All @@ -23,4 +44,42 @@ public List<StickerResponse> getStickerList() {
.build()
).collect(Collectors.toList());
}

public SendStickerResponse sendSticker(Long userId, Long scheduleId, Long stickerId) {
UserServiceUtil.existsUserById(userRepository, userId);
PillSchedule pillSchedule = PillScheduleServiceUtil.findPillScheduleById(
pillScheduleRepository, scheduleId);
StickerServiceUtil.existsStickerById(stickerRepository, stickerId);

if (!pillSchedule.getIsCheck()) {
throw new BadRequestException(ErrorCode.UNCONSUMED_PILL);
}

Pill pill = PillServiceUtil.findPillById(pillRepository, pillSchedule.getPillId());
Long receiverId = pill.getUserId();
if (!friendQueryRepository.isAlreadyFriend(userId, receiverId)) {
throw new ForbiddenException(ErrorCode.NOT_FRIEND);
}

if (likeScheduleRepository.existsBySenderIdAndScheduleId(userId, scheduleId)) {
throw new ConflictException(ErrorCode.ALREADY_SEND_STICKER);
}

LikeSchedule likeSchedule = likeScheduleRepository.save(
LikeSchedule.builder()
.scheduleId(scheduleId)
.senderId(userId)
.stickerId(stickerId)
.build()
);

return SendStickerResponse.builder()
.id(likeSchedule.getId())
.scheduleId(likeSchedule.getScheduleId())
.senderId(likeSchedule.getSenderId())
.stickerId(likeSchedule.getStickerId())
.createdAt(likeSchedule.getCreatedAt())
.updatedAt(likeSchedule.getUpdatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.sobok.SobokSobok.sticker.application;

import io.sobok.SobokSobok.exception.ErrorCode;
import io.sobok.SobokSobok.exception.model.NotFoundException;
import io.sobok.SobokSobok.sticker.infrastructure.StickerRepository;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class StickerServiceUtil {

public static void existsStickerById(StickerRepository stickerRepository, Long id) {
if (!stickerRepository.existsById(id)) {
throw new NotFoundException(ErrorCode.UNREGISTERED_STICKER);
}
}
}
40 changes: 40 additions & 0 deletions src/main/java/io/sobok/SobokSobok/sticker/domain/LikeSchedule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.sobok.SobokSobok.sticker.domain;

import io.sobok.SobokSobok.common.domain.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;

@Getter
@Entity
@DynamicInsert
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class LikeSchedule extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private Long scheduleId;

@Column(nullable = false)
private Long senderId;

@Column(nullable = false)
private Long stickerId;

@Builder
public LikeSchedule(Long scheduleId, Long senderId, Long stickerId) {
this.scheduleId = scheduleId;
this.senderId = senderId;
this.stickerId = stickerId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.sobok.SobokSobok.sticker.infrastructure;

import io.sobok.SobokSobok.sticker.domain.LikeSchedule;
import org.springframework.data.jpa.repository.JpaRepository;

public interface LikeScheduleRepository extends JpaRepository<LikeSchedule, Long> {

Boolean existsBySenderIdAndScheduleId(Long senderId, Long scheduleId);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package io.sobok.SobokSobok.sticker.ui;

import io.sobok.SobokSobok.auth.domain.User;
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.StickerResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -35,4 +41,22 @@ public ResponseEntity<ApiResponse<List<StickerResponse>>> getStickerList() {
stickerService.getStickerList()
));
}

@PostMapping("/{scheduleId}")
@Operation(
summary = "스티커 전송 API 메서드",
description = "스티커를 전송하는 메서드입니다."
)
public ResponseEntity<ApiResponse<SendStickerResponse>> sendSticker(
@AuthenticationPrincipal User user,
@PathVariable Long scheduleId,
@RequestParam Long stickerId
) {
return ResponseEntity
.status(HttpStatus.OK)
.body(ApiResponse.success(
SuccessCode.SEND_STICKER_SUCCESS,
stickerService.sendSticker(user.getId(), scheduleId, stickerId)
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.sobok.SobokSobok.sticker.ui.dto;

import java.time.LocalDateTime;
import lombok.Builder;

@Builder
public record SendStickerResponse(
Long id,
Long scheduleId,
Long senderId,
Long stickerId,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {

}
Loading