Skip to content

Commit

Permalink
feat: School domain 유니크 제약조건 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
seokjin8678 committed May 8, 2024
1 parent 5ba8311 commit 06209bc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ public Long createSchool(SchoolCreateCommand command) {
}

private void validateCreate(SchoolCreateCommand command) {
String domain = command.domain();
String name = command.name();
if (schoolRepository.existsByDomain(domain)) {
throw new BadRequestException(ErrorCode.DUPLICATE_SCHOOL_DOMAIN);
}
if (schoolRepository.existsByName(name)) {
throw new BadRequestException(ErrorCode.DUPLICATE_SCHOOL_NAME);
}
Expand All @@ -55,11 +51,7 @@ public void updateSchool(Long schoolId, SchoolUpdateCommand command) {
}

private void validateUpdate(School school, SchoolUpdateCommand command) {
String domain = command.domain();
String name = command.name();
if (!Objects.equals(school.getDomain(), domain) && schoolRepository.existsByDomain(domain)) {
throw new BadRequestException(ErrorCode.DUPLICATE_SCHOOL_DOMAIN);
}
if (!Objects.equals(school.getName(), name) && schoolRepository.existsByName(name)) {
throw new BadRequestException(ErrorCode.DUPLICATE_SCHOOL_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class School extends BaseTimeEntity {

@NotNull
@Size(max = MAX_DOMAIN_LENGTH)
@Column(unique = true)
private String domain;

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table school
drop key unique_domain;
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ class createSchool {
.build();

@Test
void 같은_도메인의_학교가_저장되어_있으면_예외가_발생한다() {
void 같은_도메인의_학교가_저장되어_있어도_예외가_발생하지_않는다() {
// given
schoolRepository.save(SchoolFixture.builder().domain("teco.ac.kr").build());

// when & then
assertThatThrownBy(() -> schoolCommandService.createSchool(command))
.isInstanceOf(BadRequestException.class)
.hasMessage(ErrorCode.DUPLICATE_SCHOOL_DOMAIN.getMessage());
// when
Long schoolId = schoolCommandService.createSchool(command);

// then
assertThat(schoolRepository.getOrThrow(schoolId).getDomain()).isEqualTo("teco.ac.kr");
}

@Test
Expand Down Expand Up @@ -113,15 +114,16 @@ void setUp() {
}

@Test
void 같은_도메인의_학교가_저장되어_있으면_예외가_발생한다() {
void 같은_도메인의_학교가_저장되어_있어도_예외가_발생하지_않는다() {
// given
Long schoolId = school.getId();
schoolRepository.save(SchoolFixture.builder().domain("teco.ac.kr").build());

// when & then
assertThatThrownBy(() -> schoolCommandService.updateSchool(schoolId, command))
.isInstanceOf(BadRequestException.class)
.hasMessage(ErrorCode.DUPLICATE_SCHOOL_DOMAIN.getMessage());
// when
schoolCommandService.updateSchool(schoolId, command);

// then
assertThat(schoolRepository.getOrThrow(schoolId).getDomain()).isEqualTo("teco.ac.kr");
}

@Test
Expand Down

0 comments on commit 06209bc

Please sign in to comment.