Skip to content

Commit

Permalink
refactor: StudyRegion enum 타입 수정 및 StudyDay convert 메서드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
HanKwanJin committed Feb 8, 2023
1 parent 4d75f13 commit ba2fc8a
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.be.core.application.dto.request;

import com.example.be.core.domain.study.StudyDay;
import com.example.be.core.domain.study.StudyRegion;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDate;
import java.util.EnumSet;
Expand Down Expand Up @@ -47,8 +48,8 @@ public class StudyRequest {
@NotBlank
private String rule;

@Schema(type = "String", description = "지역, NULL")
private String region;
@Schema(type = "StudyRegion", description = "지역, NULL")
private StudyRegion region;

@Schema(type = "String", description = "스터디 하는 요일, NOT NULL")
@NotBlank
Expand All @@ -60,7 +61,7 @@ public class StudyRequest {

public StudyRequest(String title, String content, Integer level, String language,
String speakingTest, String grade, Integer maxCapacity, Integer minCapacity, String rule,
String region, String studyDay, String posterImage) {
StudyRegion region, String studyDay, String posterImage) {
this.title = title;
this.content = content;
this.level = level;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.be.core.application.dto.response;

import com.example.be.core.domain.study.StudyRegion;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import javax.validation.constraints.Positive;
Expand Down Expand Up @@ -39,8 +40,8 @@ public class StudyDetailResponse {
@Schema(type = "String", description = "대면, 비대면 규칙, NOT NULL")
private final String rule;

@Schema(type = "String", description = "지역, NULL")
private final String region;
@Schema(type = "StudyRegion", description = "지역, NULL")
private final StudyRegion region;

@Schema(type = "String", description = "스터디 하는 요일, NOT NULL")
private final String studyDay;
Expand All @@ -62,7 +63,7 @@ public class StudyDetailResponse {

public StudyDetailResponse(Long studyId, String title, String content, Integer level,
String language, String speakingTest, String grade, Integer maxCapacity, Integer minCapacity,
String rule, String region, String studyDay, String posterImage, Integer interestCount,
String rule, StudyRegion region, String studyDay, String posterImage, Integer interestCount,
Boolean isInterested, Boolean isLeader, MemberProfilesResponse memberProfiles) {
this.studyId = studyId;
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.example.be.core.domain.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
Expand Down Expand Up @@ -33,6 +35,8 @@ public class Member extends BaseEntity {
private String uniqueId;

private String profileImage;

@Enumerated(EnumType.STRING)
private MemberType memberType;
private Boolean alarmStatus;
private Integer point;
Expand Down
10 changes: 6 additions & 4 deletions be/src/main/java/com/example/be/core/domain/study/Study.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
Expand Down Expand Up @@ -43,8 +45,8 @@ public class Study extends BaseEntity {

private String rule;

@Convert(converter = StudyRegionConverter.class)
private String region;
@Enumerated(EnumType.STRING)
private StudyRegion region;

private Integer maxCapacity;

Expand All @@ -55,7 +57,7 @@ public class Study extends BaseEntity {
private String grade;

public Study(String title, String content, String posterImage, String language, Integer level,
EnumSet<StudyDay> studyDay, String rule, String region, Integer maxCapacity,
EnumSet<StudyDay> studyDay, String rule, StudyRegion region, Integer maxCapacity,
Integer minCapacity, String speakingTest, String grade) {
this.title = title;
this.content = content;
Expand All @@ -72,7 +74,7 @@ public Study(String title, String content, String posterImage, String language,
}

public void modify(String title, String content, String posterImage, String language, Integer level,
EnumSet<StudyDay> studyDay, String rule, String region, Integer maxCapacity,
EnumSet<StudyDay> studyDay, String rule, StudyRegion region, Integer maxCapacity,
Integer minCapacity, String speakingTest, String grade) {
this.title = title;
this.content = content;
Expand Down
12 changes: 10 additions & 2 deletions be/src/main/java/com/example/be/core/domain/study/StudyDay.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.example.be.core.domain.study;

import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Arrays;
import lombok.Getter;
import lombok.ToString;

@Getter
public enum StudyDay {
Expand All @@ -13,9 +14,16 @@ public enum StudyDay {
SATURDAY("토"),
SUNDAY("일");

private String studyDay;
private final String studyDay;

StudyDay(String studyDay) {
this.studyDay = studyDay;
}

public static StudyDay convert(String source) {
return Arrays.stream(values())
.filter(v -> v.studyDay.equals(source))
.findAny()
.orElseThrow(IllegalArgumentException::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ public class StudyDayConverter implements AttributeConverter<EnumSet<StudyDay>,
@Override
public String convertToDatabaseColumn(EnumSet<StudyDay> attribute) {
StringBuilder sb = new StringBuilder();
attribute.stream().forEach(e -> sb.append(e.getStudyDay() + ","));
attribute.forEach(e -> sb.append(e.getStudyDay()).append(","));
String result = sb.toString();
if (result.charAt(result.length() - 1) == ',') result = result.substring(0, result.length() - 1);
if (result.charAt(result.length() - 1) == ',') {
result = result.substring(0, result.length() - 1);
}
log.debug("[DB 에서 사용될 StudyDay 정보] : {}", result);
return result;
}

@Override
public EnumSet<StudyDay> convertToEntityAttribute(String dbData) {
if (dbData == null || dbData == "" || dbData.contains(".")) return EnumSet.noneOf(StudyDay.class);
if (dbData == null || dbData.equals("") || dbData.contains(".")) return EnumSet.noneOf(StudyDay.class);
EnumSet<StudyDay> attribute = EnumSet.noneOf(StudyDay.class);
String[] dbDataArray = StringUtils.trimAllWhitespace(dbData).toUpperCase().split(",");
Arrays.stream(dbDataArray).forEach(e -> attribute.add(StudyDay.valueOf(e)));
Arrays.stream(dbDataArray).forEach(e -> attribute.add(StudyDay.convert(e)));
log.debug("[Entity 에서 사용될 StudyDay 정보] : {}", attribute);
return attribute;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.be.core.domain.study;

import com.fasterxml.jackson.annotation.JsonCreator;
import java.util.Arrays;
import lombok.Getter;

@Getter
Expand All @@ -21,7 +23,7 @@ public enum StudyRegion {
JEJU("제주"),
ETC("기타");

private String region;
private final String region;

StudyRegion(String region) {
this.region = region;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.be.core.application.dto.request.StudyRequest;
import com.example.be.core.application.dto.response.StudyDetailResponse;
import com.example.be.core.domain.study.StudyDay;
import com.example.be.core.domain.study.StudyRegion;
import java.util.EnumSet;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand Down Expand Up @@ -37,7 +38,7 @@ void create_study() throws Exception {
5,
1,
"대면",
"서울",
StudyRegion.SEOUL,
"월,화,수",
"study"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.be.core.application.dto.request.StudyRequest;
import com.example.be.core.application.dto.response.StudyDetailResponse;
import com.example.be.core.domain.study.StudyDay;
import com.example.be.core.domain.study.StudyRegion;
import com.example.be.core.domain.study.StudyType;
import java.util.EnumSet;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -31,7 +32,7 @@ void normal_modify() throws Exception {
Long memberId = 1L;

StudyRequest request = new StudyRequest("수정 제목", "수정 내용", 5, "수정 언어", "수정 목표", "수정 자격증",
5, 1, "대면", "서울", "월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg");
5, 1, "대면", StudyRegion.SEOUL, "월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg");

//when
studyService.modify(memberId, studyId, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.example.be.core.application.dto.response.MemberProfilesResponse;
import com.example.be.core.application.dto.response.StudyDetailResponse;
import com.example.be.core.domain.study.StudyDay;
import com.example.be.core.domain.study.StudyRegion;
import com.example.be.core.web.InitControllerTest;
import com.example.be.core.web.StudyController;
import java.util.ArrayList;
Expand Down Expand Up @@ -45,9 +46,9 @@ void create_study() throws Exception {
Integer likeCount = 10;

StudyRequest request = new StudyRequest("스터디1", "내용1", 3, "english",
"OPIC", "AL", 5, 1, "대면", "서울", "월,화,수","https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg");
"OPIC", "AL", 5, 1, "대면", StudyRegion.SEOUL, "월,화,수","https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg");
StudyDetailResponse response = new StudyDetailResponse(memberId, "스터디1", "내용1", 3, "english",
"OPIC", "AL", 5, 1, "대면", "서울","월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg",likeCount, false, Boolean.TRUE, new MemberProfilesResponse("leader", new ArrayList<>()));
"OPIC", "AL", 5, 1, "대면", StudyRegion.SEOUL,"월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg",likeCount, false, Boolean.TRUE, new MemberProfilesResponse("leader", new ArrayList<>()));
BaseResponse<StudyDetailResponse> baseResponse = new BaseResponse<>(CREATE_STUDY_SUCCESS, response);

when(studyService.create(refEq(request), refEq(memberId)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.example.be.core.application.dto.response.MemberProfilesResponse;
import com.example.be.core.application.dto.response.StudyDetailResponse;
import com.example.be.core.domain.study.StudyDay;
import com.example.be.core.domain.study.StudyRegion;
import com.example.be.core.web.InitControllerTest;
import com.example.be.core.web.StudyController;
import java.util.ArrayList;
Expand Down Expand Up @@ -44,7 +45,7 @@ void find_detail_study_log() throws Exception {
Long studyId = 1L;
Integer likeCount = 10;
StudyDetailResponse response = new StudyDetailResponse(memberId, "스터디1", "내용1", 3, "english",
"OPIC", "AL", 5, 1, "대면", "서울","월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg",likeCount, false, Boolean.TRUE, new MemberProfilesResponse("leader", new ArrayList<>()));
"OPIC", "AL", 5, 1, "대면", StudyRegion.SEOUL,"월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg",likeCount, false, Boolean.TRUE, new MemberProfilesResponse("leader", new ArrayList<>()));
BaseResponse<StudyDetailResponse> baseResponse = new BaseResponse<>(
FIND_DETAIL_STUDY_SUCCESS, response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.example.be.core.application.dto.response.MemberProfilesResponse;
import com.example.be.core.application.dto.response.StudyDetailResponse;
import com.example.be.core.domain.study.StudyDay;
import com.example.be.core.domain.study.StudyRegion;
import com.example.be.core.web.InitControllerTest;
import com.example.be.core.web.StudyController;
import java.util.ArrayList;
Expand Down Expand Up @@ -46,9 +47,9 @@ void modify_study() throws Exception {
Long studyId = 1L;
Integer likeCount = 10;
StudyRequest request = new StudyRequest("스터디1", "내용1", 3, "english",
"OPIC", "AL", 5, 1, "대면", "서울", "월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg");
"OPIC", "AL", 5, 1, "대면", StudyRegion.SEOUL, "월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg");
StudyDetailResponse response = new StudyDetailResponse(memberId, "스터디1", "내용1", 3, "english",
"OPIC", "AL", 5, 1, "대면", "서울","월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg",likeCount, false, Boolean.TRUE, new MemberProfilesResponse("leader", new ArrayList<>()));
"OPIC", "AL", 5, 1, "대면", StudyRegion.SEOUL,"월,화,수", "https://haru-speak-s3.s3.ap-northeast-2.amazonaws.com/image/a4cd3848-b965-4504-90ce-b772398d7f11.jpeg",likeCount, false, Boolean.TRUE, new MemberProfilesResponse("leader", new ArrayList<>()));
BaseResponse<StudyDetailResponse> baseResponse = new BaseResponse<>(MODIFY_STUDY_SUCCESS, response);

when(studyService.modify(refEq(memberId), refEq(studyId), refEq(request)))
Expand Down
13 changes: 10 additions & 3 deletions be/src/test/java/com/example/be/tool/DataBaseConfigurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.example.be.core.domain.study.Study;
import com.example.be.core.domain.study.StudyDay;
import com.example.be.core.domain.study.StudyMember;
import com.example.be.core.domain.study.StudyRegion;
import com.example.be.core.repository.assignment.AssignmentMemberRepository;
import com.example.be.core.repository.assignment.AssignmentRepository;
import com.example.be.core.repository.member.MemberRepository;
Expand Down Expand Up @@ -74,6 +75,8 @@ public class DataBaseConfigurator implements InitializingBean {

private List<String> tableNames;

EnumSet<StudyDay> studyDay = EnumSet.allOf(StudyDay.class);

@Override
public void afterPropertiesSet() throws Exception {
entityManager.unwrap(Session.class).doWork(this::extractTableNames);
Expand Down Expand Up @@ -164,9 +167,9 @@ private void initStudy() {
"study-posterImage" + i,
"study-language" + i,
i,
EnumSet.of(StudyDay.MONDAY),
studyDay,
"study-rule" + i,
"study-region" + i,
StudyRegion.SEOUL,
i,
i,
"study-speakingTest" + i,
Expand All @@ -176,11 +179,15 @@ private void initStudy() {
for (int j = 1; j <= NUMBER_OF_MEMBER; j++) {
Member member = memberRepository.findById((long) j)
.orElseThrow(NotFoundMemberIdException::new);
boolean leader = Boolean.FALSE;
if(j == 1){
leader = Boolean.TRUE;
}
studyMemberRepository.save(
new StudyMember(
member,
study,
Boolean.TRUE
leader
)
);
}
Expand Down

0 comments on commit ba2fc8a

Please sign in to comment.