Skip to content

Commit

Permalink
refactor: Builder 패턴 적용 추가, Test 코드 수정, @transactional 원래대로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeuk520 committed Mar 30, 2024
1 parent ec4c0a1 commit 4211fc6
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 61 deletions.
14 changes: 9 additions & 5 deletions src/main/java/konkuk/travelmate/domain/Course.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package konkuk.travelmate.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Course {

@Id
Expand All @@ -29,4 +25,12 @@ public class Course {
@JoinColumn(name = "h_id")
private Health health;

@Builder
private Course(String name, String region, String description, String imageUrl, Health health) {
this.name = name;
this.region = region;
this.description = description;
this.imageUrl = imageUrl;
this.health = health;
}
}
35 changes: 26 additions & 9 deletions src/main/java/konkuk/travelmate/domain/Health.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,54 @@
import jakarta.persistence.*;
import lombok.*;

import java.util.Map;

import static java.lang.Integer.parseInt;

@Entity
@Getter
@RequiredArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Health {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "h_id")
private Long healthId;

@NonNull
private Integer walk;

@NonNull
private Integer see;

@NonNull
private Integer talk;

@NonNull
private Integer listen;

@NonNull
private Integer depression;

@NonNull
private Integer bipolarDisorder;

@NonNull
private Integer iq;

@Builder
private Health(Integer walk, Integer see, Integer talk, Integer listen, Integer depression, Integer bipolarDisorder, Integer iq) {
this.walk = walk;
this.see = see;
this.talk = talk;
this.listen = listen;
this.depression = depression;
this.bipolarDisorder = bipolarDisorder;
this.iq = iq;
}

public static Health of(Map<String, String> signupMap) {
return Health.builder()
.walk(parseInt(signupMap.get("walk")))
.see(parseInt(signupMap.get("see")))
.talk(parseInt(signupMap.get("talk")))
.listen(parseInt(signupMap.get("listen")))
.depression(parseInt(signupMap.get("depression")))
.bipolarDisorder(parseInt(signupMap.get("bipolarDisorder")))
.iq(parseInt(signupMap.get("iq")))
.build();
}
}
3 changes: 1 addition & 2 deletions src/main/java/konkuk/travelmate/domain/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Request {

@Id
Expand All @@ -35,7 +34,7 @@ public class Request {
private Course course;

@Builder
public Request(TravelType type, RequestState state, Timestamp startTime, Timestamp endTime, User disabled, Course course) {
private Request(TravelType type, RequestState state, Timestamp startTime, Timestamp endTime, User disabled, Course course) {
this.type = type;
this.state = state;
this.startTime = startTime;
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/konkuk/travelmate/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,29 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "u_id")
private Long userId;

@NonNull
private String name;

private String email;

@NonNull
private String password;

@NonNull
private String phoneNum;

@NonNull
private Role role;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "h_id")
private Health health;

public User(@NonNull String name, @NonNull String email, @NonNull String password, @NonNull String phoneNum, @NonNull Role role, Health health) {
@Builder
private User(String name, String email, String password, String phoneNum, Role role, Health health) {
this.name = name;
this.email = email;
this.password = password;
Expand All @@ -41,6 +37,17 @@ public User(@NonNull String name, @NonNull String email, @NonNull String passwor
this.health = health;
}

public static User of(String name, String email, String string, String phoneNum, Role role, Health health) {
return User.builder()
.name(name)
.email(email)
.password(builder().password)
.phoneNum(phoneNum)
.role(role)
.health(health)
.build();
}


public void setHealth(Health health){
this.health=health;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class MatchingService {

private final MatchingRepository matchingRepository;

@Transactional(readOnly = true)
public List<GetVolunteerMatchingResponse> getMatchResults(String email) {
log.info("[MatchingService.getMatchResults]");
return matchingRepository.findVolunteerMatchingResultsByEmail(email);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/konkuk/travelmate/service/RequestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class RequestService {

private final RequestRepository requestRepository;
private final UserRepository userRepository;
private final CourseRepository courseRepository;
private final MatchingRepository matchingRepository;

@Transactional
public void requestMatching(Long disabledId, Long courseId, PostRequestRequest requestForm) {

log.info("[RequestService.requestMatching]");
Expand All @@ -38,6 +36,7 @@ public void requestMatching(Long disabledId, Long courseId, PostRequestRequest r
requestRepository.save(request);
}

@Transactional(readOnly = true)
public List<GetRequestsResponse> showRequests(GetRequestsRequest request) {
log.info("[RequestService.showRequests]");
return requestRepository.findRequestsByStateAndHealthLevel(
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/konkuk/travelmate/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import java.util.Optional;
import java.util.UUID;

import static java.lang.Integer.*;
import static java.util.Objects.*;
import static java.util.UUID.*;

@Service
@RequiredArgsConstructor
public class UserService {
Expand All @@ -27,19 +31,21 @@ public Optional<User> findByEmail(String email) {


public User joinDisable(OAuth2User OAuth2User, Map<String, String> signupMap, Role role) {
Health health = new Health(Integer.parseInt(signupMap.get("see")), Integer.parseInt(signupMap.get("walk")),
Integer.parseInt(signupMap.get("talk")), Integer.parseInt(signupMap.get("listen")), Integer.parseInt(signupMap.get("iq")),
Integer.parseInt(signupMap.get("bipolar_disorder")), Integer.parseInt(signupMap.get("depression")));
User User = new User(Objects.requireNonNull(OAuth2User.getAttribute("name")), Objects.requireNonNull(OAuth2User.getAttribute("email")), UUID.randomUUID().toString(), signupMap.get("phoneNum"), role, health);
Health health = Health.of(signupMap);
User user = User.of(requireNonNull(OAuth2User.getAttribute("name")).toString(),
requireNonNull(OAuth2User.getAttribute("email")).toString(),
randomUUID().toString(), signupMap.get("phoneNum"), role, health);
healthRepository.save(health);
UserRepository.save(User);
return User;
UserRepository.save(user);
return user;
}

public User joinVolunteer(OAuth2User OAuth2User, Map<String, String> signupMap, Role role) {
User User = new User(Objects.requireNonNull(OAuth2User.getAttribute("name")), Objects.requireNonNull(OAuth2User.getAttribute("email")), UUID.randomUUID().toString(), signupMap.get("phoneNum"), role, null);
UserRepository.save(User);
return User;
User user = User.of(requireNonNull(OAuth2User.getAttribute("name")).toString(),
requireNonNull(OAuth2User.getAttribute("email")),
randomUUID().toString(),signupMap.get("phoneNum"), role, null);
UserRepository.save(user);
return user;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,28 @@ class MatchingRepositoryTest {
void findVolunteerMatchingResultsByEmail() {

//given
Health health = new Health(1L, 0, 0, 0, 0, 0, 0, 0);
healthRepository.save(health);
User disabled = new User(1L, "d1", "d1@gmail.com", "d1", "01012341234", Role.DISABLED, health);
userRepository.save(disabled);
Health health = createHealth(0, 0, 0, 0, 0, 0, 0);
Health health2 = createHealth(0, 0, 0, 0, 0, 0, 0);
Health health3 = createHealth(0, 0, 0, 0, 0, 0, 0);
healthRepository.saveAll(List.of(health, health2, health3));

Health health2 = new Health(2L, 0, 0, 0, 0, 0, 0, 0);
healthRepository.save(health2);
Course course = new Course(1L, "서울여행", "서울", "서울여행123", "asdf@ewaf.com", health2);
courseRepository.save(course);
User disabled = createUser("d1", "d1@gmail.com", "d1", "01012341234", Role.DISABLED, health);
User volunteer = createUser( "v1", "v1@gmail.com", "v1", "01012341234", Role.VOLUNTEER, null);
userRepository.saveAll(List.of(disabled, volunteer));

Health health3 = new Health(3L, 0, 0, 0, 0, 0, 0, 0);
healthRepository.save(health3);
Course course2 = new Course(2L, "부산여행", "부산", "부산여행123", "asdf@ewaf.com", health3);
courseRepository.save(course2);
Course course = createCourse("서울여행", "서울", "서울여행123", "asdf@ewaf.com", health2);
Course course2 = createCourse("부산여행", "부산", "부산여행123", "asdf@ewaf.com", health3);
courseRepository.saveAll(List.of(course, course2));


User volunteer = new User(2L, "v1", "v1@gmail.com", "v1", "01012341234", Role.VOLUNTEER, null);
userRepository.save(volunteer);

Request request = new Request(1L, TravelType.ALL, RequestState.WAIT, Timestamp.valueOf("2024-02-22 14:54:00"),
Request request = createRequest(TravelType.ALL, RequestState.WAIT, Timestamp.valueOf("2024-02-22 14:54:00"),
Timestamp.valueOf("2024-02-22 14:54:00"), disabled, course);
requestRepository.save(request);

Request request2 = new Request(2L, TravelType.LODGING, RequestState.SUCCESS, Timestamp.valueOf("2024-02-22 14:54:00"),
Request request2 = createRequest(TravelType.LODGING, RequestState.SUCCESS, Timestamp.valueOf("2024-02-22 14:54:00"),
Timestamp.valueOf("2024-02-22 14:54:00"), disabled, course);
requestRepository.save(request2);
requestRepository.saveAll(List.of(request, request2));

Matching matching = new Matching(1L, 0, volunteer, request);
matchingRepository.save(matching);

Matching matching2 = new Matching(2L, 0, volunteer, request2);
matchingRepository.save(matching2);
Matching matching = Matching.createNullRatingMatching(volunteer, request);
Matching matching2 = Matching.createNullRatingMatching(volunteer, request2);
matchingRepository.saveAll(List.of(matching, matching2));

String email = volunteer.getEmail();

Expand All @@ -74,4 +64,48 @@ void findVolunteerMatchingResultsByEmail() {
TravelType.LODGING, RequestState.SUCCESS, "01012341234", "d1@gmail.com")
);
}

private Health createHealth(int walk, int see, int talk, int listen, int depression, int bipolarDisorder, int iq) {
return Health.builder()
.walk(walk)
.see(see)
.talk(talk)
.listen(listen)
.depression(depression)
.bipolarDisorder(bipolarDisorder)
.iq(iq)
.build();
}

private User createUser(String name, String email, String password, String phoneNum, Role role, Health health) {
return User.builder()
.name(name)
.email(email)
.password(password)
.phoneNum(phoneNum)
.role(role)
.health(health)
.build();
}

private Course createCourse(String name, String description, String region, String imageUrl, Health health) {
return Course.builder()
.name(name)
.description(description)
.region(region)
.imageUrl(imageUrl)
.health(health)
.build();
}

private Request createRequest(TravelType type, RequestState state, Timestamp startTime, Timestamp endTime, User disabled, Course course) {
return Request.builder()
.type(type)
.state(state)
.startTime(startTime)
.endTime(endTime)
.disabled(disabled)
.course(course)
.build();
}
}

0 comments on commit 4211fc6

Please sign in to comment.