Skip to content

Commit

Permalink
refactor: 환영 메시지 태그 파싱 로직 단순화 (#542)
Browse files Browse the repository at this point in the history
* fix: 사용되지 않는 컨버터 제거

- @convert@type 간의 충돌
- @type이 이미 JSON 처리를 맡아 @convert를 덮어쓴 상황이라 필요없다는 어노테이션이라고 판단

* refactor: JPQL 대신 프로젝션으로 조회 방법 변경

* refactor: 환영 메시지 저장 및 조회 서비스 로직 변경

- NPE 방지를 위해 환영 메시지 저장 시 null 값이라도 DB에는 빈 리스트로 저장하도록 변경
- 서비스 레이어에서 필터링 하는 대신 프로젝션으로 바로 enum 객체를 가져올 수 있도록 변경
  • Loading branch information
hoonyworld authored Jan 24, 2025
1 parent 4ed669f commit 39f8fb1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<WelcomeMessageType> welcomeMessageTypes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tag, Integer> {
@Query("SELECT t.welcomeMessageTypes FROM Tag t WHERE t.lightningId = :lightningId")
Optional<String> findWelcomeMessageTypesByLightningId(@Param("lightningId") Integer lightningId);

Optional<WelcomeMessageTypeProjection> findByLightningId(Integer lightningId);
}
Original file line number Diff line number Diff line change
@@ -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<WelcomeMessageType> getWelcomeMessageTypes();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,8 +22,6 @@
@Transactional(readOnly = true)
public class TagV2ServiceImpl implements TagV2Service {

private static final String JSON_VALUE_SEPARATOR = ",";

private final TagRepository tagRepository;

// 여기에 createGeneralMeetingTag 메서드도 추가하면 될 것 같습니다 나중에! (추후 일반 모임에 태그 추가 시 작성)
Expand All @@ -38,56 +35,31 @@ public TagV2CreateTagResponseDto createLightningTag(List<String> welcomeMessageT
throw new BadRequestException(VALIDATION_EXCEPTION.getErrorCode());
}

List<WelcomeMessageType> 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<WelcomeMessageType> welcomeMessageTypeEnums = welcomeMessageTypes.stream()
.map(WelcomeMessageType::ofValue)
.toList();

return TagV2CreateTagResponseDto.from(tag.getId());
return saveTag(lightningId, welcomeMessageTypeEnums);
}

@Override
public List<WelcomeMessageType> 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<WelcomeMessageType> parseWelcomeMessageTypes(String jsonWelcomeMessageTypes) {
List<String> values = splitAndTrimJsonValues(jsonWelcomeMessageTypes);

return values.stream()
.map(this::convertToWelcomeMessageType)
.filter(Objects::nonNull)
.toList();
}

private List<String> 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<WelcomeMessageType> welcomeMessageTypeEnums) {
Tag tag = Tag.createLightningMeetingTag(TagType.LIGHTNING, lightningId, welcomeMessageTypeEnums);
tagRepository.save(tag);
return TagV2CreateTagResponseDto.from(tag.getId());
}
}

0 comments on commit 39f8fb1

Please sign in to comment.