diff --git a/main/src/main/java/org/sopt/makers/crew/main/entity/tag/Tag.java b/main/src/main/java/org/sopt/makers/crew/main/entity/tag/Tag.java index 2155f32c..f247d23e 100644 --- a/main/src/main/java/org/sopt/makers/crew/main/entity/tag/Tag.java +++ b/main/src/main/java/org/sopt/makers/crew/main/entity/tag/Tag.java @@ -6,11 +6,9 @@ import org.sopt.makers.crew.main.entity.common.BaseTimeEntity; import org.sopt.makers.crew.main.entity.tag.enums.TagType; import org.sopt.makers.crew.main.entity.tag.enums.WelcomeMessageType; -import org.sopt.makers.crew.main.entity.tag.enums.WelcomeTypeConverter; import io.hypersistence.utils.hibernate.type.json.JsonBinaryType; import jakarta.persistence.Column; -import jakarta.persistence.Convert; import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; @@ -51,7 +49,6 @@ public class Tag extends BaseTimeEntity { private Integer lightningId; @Column(name = "welcomeMessageTypes", columnDefinition = "jsonb") - @Convert(converter = WelcomeTypeConverter.class) @Type(JsonBinaryType.class) private List welcomeMessageTypes; diff --git a/main/src/main/java/org/sopt/makers/crew/main/entity/tag/TagRepository.java b/main/src/main/java/org/sopt/makers/crew/main/entity/tag/TagRepository.java index 00082270..5a519390 100644 --- a/main/src/main/java/org/sopt/makers/crew/main/entity/tag/TagRepository.java +++ b/main/src/main/java/org/sopt/makers/crew/main/entity/tag/TagRepository.java @@ -3,11 +3,7 @@ import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; public interface TagRepository extends JpaRepository { - @Query("SELECT t.welcomeMessageTypes FROM Tag t WHERE t.lightningId = :lightningId") - Optional findWelcomeMessageTypesByLightningId(@Param("lightningId") Integer lightningId); - + Optional findByLightningId(Integer lightningId); } diff --git a/main/src/main/java/org/sopt/makers/crew/main/entity/tag/WelcomeMessageTypeProjection.java b/main/src/main/java/org/sopt/makers/crew/main/entity/tag/WelcomeMessageTypeProjection.java new file mode 100644 index 00000000..ea1245e9 --- /dev/null +++ b/main/src/main/java/org/sopt/makers/crew/main/entity/tag/WelcomeMessageTypeProjection.java @@ -0,0 +1,9 @@ +package org.sopt.makers.crew.main.entity.tag; + +import java.util.List; + +import org.sopt.makers.crew.main.entity.tag.enums.WelcomeMessageType; + +public interface WelcomeMessageTypeProjection { + List getWelcomeMessageTypes(); +} diff --git a/main/src/main/java/org/sopt/makers/crew/main/entity/tag/enums/WelcomeTypeConverter.java b/main/src/main/java/org/sopt/makers/crew/main/entity/tag/enums/WelcomeTypeConverter.java deleted file mode 100644 index 8d40ccf1..00000000 --- a/main/src/main/java/org/sopt/makers/crew/main/entity/tag/enums/WelcomeTypeConverter.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.sopt.makers.crew.main.entity.tag.enums; - -import java.util.List; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -import jakarta.persistence.AttributeConverter; -import jakarta.persistence.Converter; - -@Converter -public class WelcomeTypeConverter implements AttributeConverter, String> { - private static final ObjectMapper objectMapper = new ObjectMapper(); - - @Override - public String convertToDatabaseColumn(List attribute) { - try { - // Enum -> 한글 값 리스트 -> JSON - List values = attribute.stream() - .map(WelcomeMessageType::getValue) - .toList(); - return objectMapper.writeValueAsString(values); - } catch (Exception e) { - throw new IllegalArgumentException("Error converting list to JSON: " + attribute, e); - } - } - - @Override - public List convertToEntityAttribute(String dbData) { - try { - // JSON -> 한글 값 리스트 -> Enum - List values = objectMapper.readValue(dbData, new TypeReference<>() { - }); - return values.stream() - .map(WelcomeMessageType::ofValue) - .toList(); - } catch (Exception e) { - throw new IllegalArgumentException("Error converting JSON to list: " + dbData, e); - } - } -} diff --git a/main/src/main/java/org/sopt/makers/crew/main/tag/v2/service/TagV2ServiceImpl.java b/main/src/main/java/org/sopt/makers/crew/main/tag/v2/service/TagV2ServiceImpl.java index f2540f53..5a3b56b3 100644 --- a/main/src/main/java/org/sopt/makers/crew/main/tag/v2/service/TagV2ServiceImpl.java +++ b/main/src/main/java/org/sopt/makers/crew/main/tag/v2/service/TagV2ServiceImpl.java @@ -2,16 +2,15 @@ import static org.sopt.makers.crew.main.global.exception.ErrorStatus.*; -import java.util.Arrays; +import java.util.Collections; import java.util.List; -import java.util.Objects; import org.sopt.makers.crew.main.entity.tag.Tag; import org.sopt.makers.crew.main.entity.tag.TagRepository; +import org.sopt.makers.crew.main.entity.tag.WelcomeMessageTypeProjection; import org.sopt.makers.crew.main.entity.tag.enums.TagType; import org.sopt.makers.crew.main.entity.tag.enums.WelcomeMessageType; import org.sopt.makers.crew.main.global.exception.BadRequestException; -import org.sopt.makers.crew.main.global.exception.NotFoundException; import org.sopt.makers.crew.main.tag.v2.dto.response.TagV2CreateTagResponseDto; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -23,8 +22,6 @@ @Transactional(readOnly = true) public class TagV2ServiceImpl implements TagV2Service { - private static final String JSON_VALUE_SEPARATOR = ","; - private final TagRepository tagRepository; // 여기에 createGeneralMeetingTag 메서드도 추가하면 될 것 같습니다 나중에! (추후 일반 모임에 태그 추가 시 작성) @@ -38,56 +35,31 @@ public TagV2CreateTagResponseDto createLightningTag(List welcomeMessageT throw new BadRequestException(VALIDATION_EXCEPTION.getErrorCode()); } - List welcomeMessageTypeEnums = null; - - if (welcomeMessageTypes != null) { - welcomeMessageTypeEnums = welcomeMessageTypes.stream() - .map(WelcomeMessageType::ofValue) - .toList(); + if (welcomeMessageTypes == null || welcomeMessageTypes.isEmpty()) { + return saveTag(lightningId, List.of()); } - Tag tag = Tag.createLightningMeetingTag(TagType.LIGHTNING, lightningId, welcomeMessageTypeEnums); - tagRepository.save(tag); + List welcomeMessageTypeEnums = welcomeMessageTypes.stream() + .map(WelcomeMessageType::ofValue) + .toList(); - return TagV2CreateTagResponseDto.from(tag.getId()); + return saveTag(lightningId, welcomeMessageTypeEnums); } @Override public List getWelcomeMessageTypesByLightningId(Integer lightningId) { - validateLightningId(lightningId); - - String jsonWelcomeMessageTypes = tagRepository.findWelcomeMessageTypesByLightningId(lightningId) - .orElseThrow(() -> new NotFoundException(TAG_NOT_FOUND_EXCEPTION.getErrorCode())); - - return parseWelcomeMessageTypes(jsonWelcomeMessageTypes); - } - - private void validateLightningId(Integer lightningId) { if (lightningId == null) { throw new BadRequestException(VALIDATION_EXCEPTION.getErrorCode()); } - } - - private List parseWelcomeMessageTypes(String jsonWelcomeMessageTypes) { - List values = splitAndTrimJsonValues(jsonWelcomeMessageTypes); - - return values.stream() - .map(this::convertToWelcomeMessageType) - .filter(Objects::nonNull) - .toList(); - } - private List splitAndTrimJsonValues(String jsonWelcomeMessageTypes) { - return Arrays.stream(jsonWelcomeMessageTypes.split(JSON_VALUE_SEPARATOR)) - .map(String::trim) - .toList(); + return tagRepository.findByLightningId(lightningId) + .map(WelcomeMessageTypeProjection::getWelcomeMessageTypes) + .orElse(Collections.emptyList()); } - private WelcomeMessageType convertToWelcomeMessageType(String value) { - try { - return WelcomeMessageType.valueOf(value); - } catch (IllegalArgumentException e) { - return null; - } + private TagV2CreateTagResponseDto saveTag(Integer lightningId, List welcomeMessageTypeEnums) { + Tag tag = Tag.createLightningMeetingTag(TagType.LIGHTNING, lightningId, welcomeMessageTypeEnums); + tagRepository.save(tag); + return TagV2CreateTagResponseDto.from(tag.getId()); } }