Skip to content

Commit

Permalink
refactor: DB에서 enum으로 구조 변경
Browse files Browse the repository at this point in the history
cc. #152
  • Loading branch information
songsunkook committed Jan 6, 2024
1 parent 29cca20 commit 82928a9
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.domain.dept.model.DeptInfo;
import in.koreatech.koin.domain.dept.model.DeptNum;
import in.koreatech.koin.domain.dept.model.Dept;

@JsonNaming(value = SnakeCaseStrategy.class)
public record DeptListItemResponse(String name, String curriculumLink, List<Long> deptNums) {

public static DeptListItemResponse from(DeptInfo deptInfo) {
public static DeptListItemResponse from(Dept dept) {
return new DeptListItemResponse(
deptInfo.getName(),
deptInfo.getCurriculumLink(),
deptInfo.getDeptNums().stream()
.map(DeptNum::getNumber)
.toList()
dept.getName(),
dept.getCurriculumLink(),
dept.getNumbers()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.domain.dept.model.DeptNum;
import in.koreatech.koin.domain.dept.model.Dept;

@JsonNaming(value = SnakeCaseStrategy.class)
public record DeptResponse(Long deptNum, String name) {

public static DeptResponse from(DeptNum deptNum) {
return new DeptResponse(deptNum.getNumber(), deptNum.getDeptInfo().getName());
public static DeptResponse from(Long findNumber, Dept dept) {
return new DeptResponse(findNumber, dept.getName());
}
}
44 changes: 44 additions & 0 deletions src/main/java/in/koreatech/koin/domain/dept/model/Dept.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package in.koreatech.koin.domain.dept.model;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import lombok.Getter;

@Getter
public enum Dept {
ARCHITECTURAL_ENGINEERING("건축공학부", List.of(72L), "https://cms3.koreatech.ac.kr/arch/1083/subview.do"),
EMPLOYMENT_SERVICE_POLICY_DEPARTMENT("고용서비스정책학과", List.of(85L), "https://www.koreatech.ac.kr/kor/CMS/UnivOrganMgr/subMain.do?mCode=MN451"),
MECHANICAL_ENGINEERING("기계공학부", List.of(20L), "https://cms3.koreatech.ac.kr/me/795/subview.do"),
DESIGN_ENGINEERING("디자인공학부", List.of(51L), "https://cms3.koreatech.ac.kr/ide/1047/subview.do"),
MECHATRONICS_ENGINEERING("메카트로닉스공학부", List.of(40L), "https://www.koreatech.ac.kr/kor/CMS/UnivOrganMgr/subMain.do?mCode=MN076"),
INDUSTRIAL_MANAGEMENT("산업경영학부", List.of(80L), "https://cms3.koreatech.ac.kr/sim/1167/subview.do"),
NEW_ENERGY_MATERIALS_CHEMICAL_ENGINEERING("에너지신소재화학공학부", List.of(74L), "https://cms3.koreatech.ac.kr/ace/992/subview.do"),
ELECTRICAL_AND_ELECTRONIC_COMMUNICATION_ENGINEERING("전기전자통신공학부", List.of(61L), "https://cms3.koreatech.ac.kr/ite/842/subview.do"),
COMPUTER_SCIENCE("컴퓨터공학부", List.of(35L, 36L), "https://cse.koreatech.ac.kr/page_izgw21"),
;

private final String name;
private final List<Long> numbers;
private final String curriculumLink;

Dept(String name, List<Long> numbers, String curriculumLink) {
this.name = name;
this.numbers = numbers;
this.curriculumLink = curriculumLink;
}

public static Optional<Dept> findByNumber(Long number) {
for (Dept dept : Dept.values()) {
if (dept.numbers.contains(number)) {
return Optional.of(dept);
}
}
return Optional.empty();
}

public static List<Dept> findAll() {
return Arrays.stream(values()).toList();
}
}
47 changes: 0 additions & 47 deletions src/main/java/in/koreatech/koin/domain/dept/model/DeptInfo.java

This file was deleted.

40 changes: 0 additions & 40 deletions src/main/java/in/koreatech/koin/domain/dept/model/DeptNum.java

This file was deleted.

23 changes: 0 additions & 23 deletions src/main/java/in/koreatech/koin/domain/dept/model/DeptNumId.java

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import in.koreatech.koin.domain.dept.model.DeptNum;
import in.koreatech.koin.domain.dept.dto.DeptResponse;
import in.koreatech.koin.domain.dept.dto.DeptListItemResponse;
import in.koreatech.koin.domain.dept.repository.DeptInfoRepository;
import in.koreatech.koin.domain.dept.repository.DeptNumRepository;
import in.koreatech.koin.domain.dept.dto.DeptResponse;
import in.koreatech.koin.domain.dept.model.Dept;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class DeptService {

private final DeptInfoRepository deptInfoRepository;
private final DeptNumRepository deptNumRepository;

public DeptResponse findById(Long id) {
DeptNum deptNum = deptNumRepository.findByNumber(id)
Dept dept = Dept.findByNumber(id)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 학부 코드입니다."));

return DeptResponse.from(deptNum);
return DeptResponse.from(id, dept);
}

public List<DeptListItemResponse> findAll() {
return deptInfoRepository.findAll()
return Dept.findAll()
.stream()
.map(DeptListItemResponse::from)
.toList();
Expand Down
83 changes: 13 additions & 70 deletions src/test/java/in/koreatech/koin/acceptance/DeptApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,29 @@

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import in.koreatech.koin.AcceptanceTest;
import in.koreatech.koin.domain.dept.model.DeptInfo;
import in.koreatech.koin.domain.dept.model.DeptNum;
import in.koreatech.koin.domain.dept.repository.DeptInfoRepository;
import in.koreatech.koin.domain.dept.repository.DeptNumRepository;
import in.koreatech.koin.domain.dept.model.Dept;
import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;

class DeptApiTest extends AcceptanceTest {

@Autowired
private DeptInfoRepository deptInfoRepository;

@Autowired
private DeptNumRepository deptNumRepository;

@Test
@DisplayName("학과 번호를 통해 학과 이름을 조회한다.")
void findDeptNameByDeptNumber() {
// given
DeptInfo deptInfo = DeptInfo.builder()
.name("컴퓨터공학부")
.curriculumLink("https://cse.koreatech.ac.kr/page_izgw21")
.build();

deptInfoRepository.save(deptInfo);

DeptNum deptNum1 = DeptNum.builder()
.deptInfo(deptInfo)
.number(35L)
.build();
DeptNum deptNum2 = DeptNum.builder()
.deptInfo(deptInfo)
.number(36L)
.build();

deptNumRepository.save(deptNum1);
deptNumRepository.save(deptNum2);
Dept dept = Dept.COMPUTER_SCIENCE;

// when then
ExtractableResponse<Response> response = RestAssured
.given()
.log().all()
.when()
.log().all()
.param("dept_num", deptNum1.getNumber())
.param("dept_num", dept.getNumbers().get(0))
.get("/dept")
.then()
.log().all()
Expand All @@ -62,8 +35,8 @@ void findDeptNameByDeptNumber() {

assertSoftly(
softly -> {
softly.assertThat(response.body().jsonPath().getString("dept_num")).isEqualTo(deptNum1.getNumber().toString());
softly.assertThat(response.body().jsonPath().getString("name")).isEqualTo(deptInfo.getName());
softly.assertThat(response.body().jsonPath().getLong("dept_num")).isEqualTo(dept.getNumbers().get(0));
softly.assertThat(response.body().jsonPath().getString("name")).isEqualTo(dept.getName());
}
);
}
Expand All @@ -72,36 +45,7 @@ void findDeptNameByDeptNumber() {
@DisplayName("모든 학과 정보를 조회한다.")
void findAllDepts() {
//given
final int DEPT_SIZE = 2;

DeptInfo deptInfo1 = DeptInfo.builder()
.name("컴퓨터공학부")
.curriculumLink("https://cse.koreatech.ac.kr/page_izgw21")
.build();
DeptInfo deptInfo2 = DeptInfo.builder()
.name("기계공학부")
.curriculumLink("https://cms3.koreatech.ac.kr/me/795/subview.do")
.build();

deptInfoRepository.save(deptInfo1);
deptInfoRepository.save(deptInfo2);

DeptNum deptNum1_1 = DeptNum.builder()
.deptInfo(deptInfo1)
.number(35L)
.build();
DeptNum deptNum1_2 = DeptNum.builder()
.deptInfo(deptInfo1)
.number(36L)
.build();
DeptNum deptNum2 = DeptNum.builder()
.deptInfo(deptInfo2)
.number(20L)
.build();

deptNumRepository.save(deptNum1_1);
deptNumRepository.save(deptNum1_2);
deptNumRepository.save(deptNum2);
final int DEPT_SIZE = Dept.values().length;

//when then
ExtractableResponse<Response> response = RestAssured
Expand All @@ -117,14 +61,13 @@ void findAllDepts() {

assertSoftly(
softly -> {
softly.assertThat(response.body().jsonPath().getList(".").size()).isEqualTo(DEPT_SIZE);
softly.assertThat(response.body().jsonPath().getString("[0].name")).isEqualTo(deptInfo2.getName());
softly.assertThat(response.body().jsonPath().getString("[1].name")).isEqualTo(deptInfo1.getName());
softly.assertThat(response.body().jsonPath().getString("[0].curriculum_link")).isEqualTo(deptInfo2.getCurriculumLink());
softly.assertThat(response.body().jsonPath().getString("[1].curriculum_link")).isEqualTo(deptInfo1.getCurriculumLink());
softly.assertThat(response.body().jsonPath().getLong("[0].dept_nums[0]")).isEqualTo(deptInfo2.getDeptNums().get(0));
softly.assertThat(response.body().jsonPath().getLong("[1].dept_nums[0]")).isEqualTo(deptInfo1.getDeptNums().get(0));
softly.assertThat(response.body().jsonPath().getLong("[1].dept_nums[1]")).isEqualTo(deptInfo1.getDeptNums().get(1));
softly.assertThat(response.body().jsonPath().getList(".").size())
.isEqualTo(DEPT_SIZE);
for (int i = 0; i < DEPT_SIZE; i++) {
softly.assertThat(response.body().jsonPath().getString(String.format("[%d].name", i))).isNotEmpty();
softly.assertThat(response.body().jsonPath().getString(String.format("[%d].curriculum_link", i))).isNotEmpty();
softly.assertThat(response.body().jsonPath().getString(String.format("[%d].dept_nums[0]", i))).isNotEmpty();
}
}
);
}
Expand Down

0 comments on commit 82928a9

Please sign in to comment.