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

refactor: 환영 메시지 태그 파싱 로직 단순화 #542

Merged
merged 3 commits into from
Jan 24, 2025
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
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());
}
}
Loading