Skip to content

Commit

Permalink
refactor: StaffCode 값객체가 아닌 원시값 사용하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
xxeol2 committed Sep 28, 2023
1 parent 3edd22d commit bb03de0
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
import static java.util.stream.Collectors.joining;

import com.festago.festival.domain.Festival;
import com.festago.staff.domain.StaffVerificationCode;
import com.festago.staff.domain.StaffCode;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import org.springframework.stereotype.Component;

@Component
public class RandomStaffVerificationCodeProvider implements StaffVerificationCodeProvider {
public class RandomStaffCodeProvider implements StaffCodeProvider {

@Override
public StaffVerificationCode provide(Festival festival) {
public String provide(Festival festival) {
String abbreviation = festival.getSchool().findAbbreviation();
Random random = ThreadLocalRandom.current();
String code = random.ints(0, 10)
.mapToObj(String::valueOf)
.limit(StaffVerificationCode.RANDOM_CODE_LENGTH)
.limit(StaffCode.RANDOM_CODE_LENGTH)
.collect(joining());
return new StaffVerificationCode(abbreviation + code);
return abbreviation + code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.festago.staff.application;

import com.festago.festival.domain.Festival;

public interface StaffCodeProvider {

String provide(Festival festival);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.festago.festival.domain.Festival;
import com.festago.festival.repository.FestivalRepository;
import com.festago.staff.domain.StaffCode;
import com.festago.staff.domain.StaffVerificationCode;
import com.festago.staff.dto.StaffCodeResponse;
import com.festago.staff.repository.StaffCodeRepository;
import org.springframework.stereotype.Service;
Expand All @@ -18,10 +17,10 @@ public class StaffService {

private final StaffCodeRepository staffCodeRepository;
private final FestivalRepository festivalRepository;
private final StaffVerificationCodeProvider codeProvider;
private final StaffCodeProvider codeProvider;

public StaffService(StaffCodeRepository staffCodeRepository,
FestivalRepository festivalRepository, StaffVerificationCodeProvider codeProvider) {
FestivalRepository festivalRepository, StaffCodeProvider codeProvider) {
this.staffCodeRepository = staffCodeRepository;
this.festivalRepository = festivalRepository;
this.codeProvider = codeProvider;
Expand All @@ -32,15 +31,15 @@ public StaffCodeResponse createStaffCode(Long festivalId) {
if (staffCodeRepository.existsByFestival(festival)) {
throw new BadRequestException(ErrorCode.STAFF_CODE_EXIST);
}
StaffVerificationCode code = createVerificationCode(festival);
String code = createVerificationCode(festival);

StaffCode staffCode = staffCodeRepository.save(new StaffCode(code, festival));

return StaffCodeResponse.from(staffCode);
}

private StaffVerificationCode createVerificationCode(Festival festival) {
StaffVerificationCode code;
private String createVerificationCode(Festival festival) {
String code;
do {
code = codeProvider.provide(festival);
} while (staffCodeRepository.existsByCode(code));
Expand Down

This file was deleted.

16 changes: 9 additions & 7 deletions backend/src/main/java/com/festago/staff/domain/StaffCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,46 @@
import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.InternalServerException;
import com.festago.festival.domain.Festival;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

@Entity
public class StaffCode {

public static final int RANDOM_CODE_LENGTH = 4;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Embedded
@NotNull
private StaffVerificationCode code;
@Size(max = 30)
private String code;

@OneToOne(fetch = FetchType.LAZY)
private Festival festival;

protected StaffCode() {
}

public StaffCode(StaffVerificationCode code, Festival festival) {
public StaffCode(String code, Festival festival) {
this(null, code, festival);
}

public StaffCode(Long id, StaffVerificationCode code, Festival festival) {
public StaffCode(Long id, String code, Festival festival) {
checkNotNull(code, festival);
this.id = id;
this.code = code;
this.festival = festival;
}

private void checkNotNull(StaffVerificationCode code, Festival festival) {
private void checkNotNull(String code, Festival festival) {
if (code == null || festival == null) {
throw new InternalServerException(ErrorCode.INTERNAL_SERVER_ERROR);
}
Expand All @@ -50,7 +52,7 @@ public Long getId() {
return id;
}

public StaffVerificationCode getCode() {
public String getCode() {
return code;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public record StaffCodeResponse(
public static StaffCodeResponse from(StaffCode staffCode) {
return new StaffCodeResponse(
staffCode.getId(),
staffCode.getCode().getValue()
staffCode.getCode()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.festago.festival.domain.Festival;
import com.festago.staff.domain.StaffCode;
import com.festago.staff.domain.StaffVerificationCode;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -12,13 +11,13 @@ public interface StaffCodeRepository extends JpaRepository<StaffCode, Long> {

boolean existsByFestival(Festival festival);

boolean existsByCode(StaffVerificationCode code);
boolean existsByCode(String code);

@Query("""
SELECT sc
FROM StaffCode sc
LEFT JOIN FETCH sc.festival
WHERE sc.code.value = :code
WHERE sc.code = :code
""")
Optional<StaffCode> findByCodeWithFetch(@Param("code") String code);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import com.festago.festival.domain.Festival;
import com.festago.school.domain.School;
import com.festago.staff.domain.StaffVerificationCode;
import com.festago.support.FestivalFixture;
import com.festago.support.SchoolFixture;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand All @@ -13,9 +12,9 @@

@DisplayNameGeneration(ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class RandomStaffVerificationCodeProviderTest {
class RandomStaffCodeProviderTest {

RandomStaffVerificationCodeProvider codeProvider = new RandomStaffVerificationCodeProvider();
RandomStaffCodeProvider codeProvider = new RandomStaffCodeProvider();

@Test
void 생성() {
Expand All @@ -25,12 +24,12 @@ class RandomStaffVerificationCodeProviderTest {
Festival festival = FestivalFixture.festival().school(school).build();

// when
StaffVerificationCode code = codeProvider.provide(festival);
String code = codeProvider.provide(festival);

// then
assertSoftly(softly -> {
softly.assertThat(code.getValue()).startsWith(abbreviation);
softly.assertThat(code.getValue()).hasSize(abbreviation.length() + 4);
softly.assertThat(code).startsWith(abbreviation);
softly.assertThat(code).hasSize(abbreviation.length() + 4);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;

import com.festago.common.exception.BadRequestException;
Expand All @@ -14,7 +15,6 @@
import com.festago.festival.repository.FestivalRepository;
import com.festago.school.domain.School;
import com.festago.staff.domain.StaffCode;
import com.festago.staff.domain.StaffVerificationCode;
import com.festago.staff.dto.StaffCodeResponse;
import com.festago.staff.repository.StaffCodeRepository;
import com.festago.support.FestivalFixture;
Expand All @@ -39,7 +39,7 @@
class StaffServiceTest {

@Spy
StaffVerificationCodeProvider codeProvider;
StaffCodeProvider codeProvider;

@Mock
StaffCodeRepository staffCodeRepository;
Expand Down Expand Up @@ -68,7 +68,7 @@ void setUp() {
.willReturn(false);

SetUpMockito
.given(staffCodeRepository.existsByCode(any(StaffVerificationCode.class)))
.given(staffCodeRepository.existsByCode(anyString()))
.willReturn(false);

SetUpMockito
Expand Down Expand Up @@ -110,13 +110,13 @@ void setUp() {
String firstCode = "festa1234";
String secondCode = "festa5678";
given(codeProvider.provide(any(Festival.class)))
.willReturn(new StaffVerificationCode(firstCode))
.willReturn(new StaffVerificationCode(secondCode));
.willReturn(firstCode)
.willReturn(secondCode);

given(staffCodeRepository.existsByCode(any(StaffVerificationCode.class)))
given(staffCodeRepository.existsByCode(anyString()))
.willAnswer(invocation -> {
StaffVerificationCode code = invocation.getArgument(0);
return Objects.equals(code.getValue(), firstCode);
String code = invocation.getArgument(0);
return Objects.equals(code, firstCode);
});

// when
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.festago.school.domain.School;
import com.festago.school.repository.SchoolRepository;
import com.festago.staff.domain.StaffCode;
import com.festago.staff.domain.StaffVerificationCode;
import com.festago.support.FestivalFixture;
import com.festago.support.SchoolFixture;
import com.festago.support.StaffCodeFixture;
Expand Down Expand Up @@ -41,7 +40,7 @@ class 축제로_존재여부_확인 {
void 있으면_참() {
// given
Festival festival = saveFestival();
staffCodeRepository.save(StaffCodeFixture.staffCode().codeValue("festa1234").festival(festival).build());
staffCodeRepository.save(StaffCodeFixture.staffCode().code("festa1234").festival(festival).build());

// when
boolean result = staffCodeRepository.existsByFestival(festival);
Expand Down Expand Up @@ -71,10 +70,10 @@ class 코드로_존재여부_확인 {
// given
Festival festival = saveFestival();
String code = "festa1234";
staffCodeRepository.save(StaffCodeFixture.staffCode().codeValue(code).festival(festival).build());
staffCodeRepository.save(StaffCodeFixture.staffCode().code(code).festival(festival).build());

// when
boolean result = staffCodeRepository.existsByCode(new StaffVerificationCode(code));
boolean result = staffCodeRepository.existsByCode(code);

// then
assertThat(result).isTrue();
Expand All @@ -86,7 +85,7 @@ class 코드로_존재여부_확인 {
String code = "festa1234";

// when
boolean result = staffCodeRepository.existsByCode(new StaffVerificationCode(code));
boolean result = staffCodeRepository.existsByCode(code);

// then
assertThat(result).isFalse();
Expand All @@ -99,7 +98,7 @@ class 코드로_존재여부_확인 {
String code = "festa1234";
Festival festival = saveFestival();
StaffCode saved = staffCodeRepository.save(
StaffCodeFixture.staffCode().codeValue(code).festival(festival).build());
StaffCodeFixture.staffCode().code(code).festival(festival).build());

// when
Optional<StaffCode> result = staffCodeRepository.findByCodeWithFetch(code);
Expand All @@ -114,7 +113,6 @@ class 코드로_존재여부_확인 {

private Festival saveFestival() {
School school = schoolRepository.save(SchoolFixture.school().build());
Festival festival = festivalRepository.save(FestivalFixture.festival().school(school).build());
return festival;
return festivalRepository.save(FestivalFixture.festival().school(school).build());
}
}
Loading

0 comments on commit bb03de0

Please sign in to comment.