Skip to content

Commit

Permalink
refactor: 환영 메시지 저장 및 조회 서비스 로직 변경
Browse files Browse the repository at this point in the history
- NPE 방지를 위해 환영 메시지 저장 시 null 값이라도 DB에는 빈 리스트로 저장하도록 변경
- 서비스 레이어에서 필터링 하는 대신 프로젝션으로 바로 enum 객체를 가져올 수 있도록 변경
  • Loading branch information
hoonyworld committed Jan 23, 2025
1 parent 2485d1b commit aa40730
Showing 1 changed file with 15 additions and 43 deletions.
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 aa40730

Please sign in to comment.