Skip to content

Commit

Permalink
[feat] 단어 카드 도메인 API 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hysong4u committed Feb 17, 2024
1 parent f0a35c4 commit 5230c32
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.Builder;

public record CardImageResponseDto(
Long id,
Long CardId,
String CardImageUrl,
String SignImageUrl
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@Builder
public record CardResponseDto(
Long id,
Long userCardId,
String name,
String cardImageUrl,
String signImageUrl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.comma.domain.card.repository;

import com.example.comma.domain.card.entity.Card;
import com.example.comma.domain.card.entity.UserCard;
import com.example.comma.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDateTime;
Expand All @@ -16,4 +18,6 @@ public interface UserCardRepository extends JpaRepository<UserCard, Long> {
Optional<UserCard> findUserCardByUserIdAndCardId(Long userId, Long cardId);

List<UserCard> findUserCardByUserIdAndCardIdNot(Long id, Long id1);

boolean existsByUserAndCard(User user, Card card);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.example.comma.global.error.ErrorCode;
import com.example.comma.global.error.exception.EntityNotFoundException;
import com.example.comma.global.error.exception.InvalidValueException;
import com.example.comma.global.error.exception.ConflictException;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -48,6 +49,10 @@ public void createCard(Long userId, Long cardId) {
Card card = cardRepository.findById(cardId)
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.CARD_NOT_FOUND));

if (userCardRepository.existsByUserAndCard(user, card)) {
throw new ConflictException(ErrorCode.USER_CARD_ALREADY_EXISTS);
}

UserCard userCard = new UserCard(user, card, false, true);

userCardRepository.save(userCard);
Expand Down Expand Up @@ -76,7 +81,7 @@ private List<CardResponseDto> convertToCardResponseDtos(List<UserCard> userCards
return userCards.stream()
.map(userCard -> {
Card card = userCard.getCard();
return new CardResponseDto(card.getId(), card.getName(), card.getCardImageUrl(), card.getSignImageUrl());
return new CardResponseDto(userCard.getId(), card.getName(), card.getCardImageUrl(), card.getSignImageUrl());
})
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum ErrorCode {
* 409 Conflict
*/
CONFLICT(HttpStatus.CONFLICT, "이미 존재하는 리소스입니다."),
USER_CARD_ALREADY_EXISTS(HttpStatus.CONFLICT, "이미 등록된 단어카드입니다."),

/**
* 500 Internal Server Error
Expand Down

0 comments on commit 5230c32

Please sign in to comment.