Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/kr/co/studyhubinu/studyhubserver/apply/service/ApplyService.java
  • Loading branch information
wellbeing-dough committed Jan 24, 2024
2 parents 546ced9 + ab47ed9 commit bb9782c
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public ApplyEntity(Inspection inspection, String introduce, Long study, Long use
this.userId = userId;
}

public static ApplyEntity of(Long userId, Long studyId, String introduce) {
return ApplyEntity.builder()
.userId(userId)
.study(studyId)
.introduce(introduce)
.inspection(Inspection.STANDBY)
.build();
}

public void update(Inspection inspection) {
this.inspection = inspection;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface ApplyRepository extends JpaRepository<ApplyEntity, Long>, ApplyRepositoryCustom {
ApplyEntity findByUserIdAndStudyId(Long userId, Long studyId);
Optional<ApplyEntity> findByUserIdAndStudyId(Long userId, Long studyId);

List<ApplyEntity> findByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,46 @@
package kr.co.studyhubinu.studyhubserver.apply.service;

import kr.co.studyhubinu.studyhubserver.apply.domain.ApplyEntity;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.ApplyUserData;
import kr.co.studyhubinu.studyhubserver.apply.dto.request.EnrollApplyRequest;
import kr.co.studyhubinu.studyhubserver.apply.dto.request.FindApplyRequest;
import kr.co.studyhubinu.studyhubserver.apply.dto.request.UpdateApplyRequest;
import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindApplyResponse;
import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection;
import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository;
import kr.co.studyhubinu.studyhubserver.common.dto.Converter;
import kr.co.studyhubinu.studyhubserver.exception.apply.ApplyNotFoundException;
import kr.co.studyhubinu.studyhubserver.exception.apply.SameUserRequestException;
import kr.co.studyhubinu.studyhubserver.exception.user.UserNotFoundException;
import kr.co.studyhubinu.studyhubserver.study.StudyRepository;
import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity;
import kr.co.studyhubinu.studyhubserver.user.domain.UserEntity;
import kr.co.studyhubinu.studyhubserver.user.repository.UserRepository;
import kr.co.studyhubinu.studyhubserver.apply.domain.ApplyEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

private final UserRepository userRepository;
private final StudyRepository studyRepository;
private final ApplyRepository applyRepository;

@Transactional
public void enroll(Long userId, EnrollApplyRequest request) {
UserEntity user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
StudyEntity study = studyRepository.findById(request.getStudyId()).orElseThrow();
ApplyEntity apply = toApplyEntity(user, study);
validateSameRequest(user, study);

applyRepository.save(apply);
applyRepository.save(ApplyEntity.of(user.getId(), study.getId(), request.getIntroduce()));
}

private ApplyEntity toApplyEntity(UserEntity user, StudyEntity study) {
return ApplyEntity.builder()
.userId(user.getId())
.study(study.getId())
.inspection(Inspection.STANDBY)
.build();
}

@Transactional
public void update(UpdateApplyRequest request) {
UserEntity user = userRepository.findById(request.getUserId()).orElseThrow(UserNotFoundException::new);
StudyEntity study = studyRepository.findById(request.getStudyId()).orElseThrow();
ApplyEntity applyEntity = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId());
ApplyEntity applyEntity = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()).orElseThrow(ApplyNotFoundException::new);
applyEntity.update(request.getInspection());
}

Expand All @@ -61,4 +50,10 @@ public FindApplyResponse findApply(FindApplyRequest request, final int page, fin

return new FindApplyResponse((long) size, userData);
}

private void validateSameRequest(UserEntity user, StudyEntity study) {
if(applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()).isPresent()) {
throw new SameUserRequestException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kr.co.studyhubinu.studyhubserver.exception.apply;

import kr.co.studyhubinu.studyhubserver.exception.StatusType;
import kr.co.studyhubinu.studyhubserver.exception.common.CustomException;

public class ApplyNotFoundException extends CustomException {

private final StatusType status;
private static final String message = "조회되는 요청이 없습니다.";

public ApplyNotFoundException() {
super(message);
this.status = StatusType.BAD_REQUEST;
}

@Override
public StatusType getStatus() {
return status;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package kr.co.studyhubinu.studyhubserver.exception.apply;

import kr.co.studyhubinu.studyhubserver.exception.StatusType;
import kr.co.studyhubinu.studyhubserver.exception.common.CustomException;

public class SameUserRequestException extends CustomException {

private final StatusType status;
private static final String message = "동일한 유저가 요청한 기록이 있습니다.";

public SameUserRequestException() {
super(message);
this.status = StatusType.BAD_REQUEST;
}

@Override
public StatusType getStatus() {
return status;
}

@Override
public String getMessage() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kr.co.studyhubinu.studyhubserver.apply.dto.data.ApplyUserData;
import kr.co.studyhubinu.studyhubserver.apply.dto.request.UpdateApplyRequest;
import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection;
import kr.co.studyhubinu.studyhubserver.exception.apply.ApplyNotFoundException;
import kr.co.studyhubinu.studyhubserver.study.StudyRepository;
import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity;
import kr.co.studyhubinu.studyhubserver.support.fixture.StudyEntityFixture;
Expand All @@ -21,6 +22,7 @@
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
Expand Down Expand Up @@ -83,13 +85,13 @@ class ApplyRepositoryTest {
applyRepository.save(applyEntity);
applyRepository.flush();

ApplyEntity apply = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId());
apply.update(updateApplyRequest.getInspection());
Optional<ApplyEntity> apply = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId());
apply.get().update(updateApplyRequest.getInspection());
applyRepository.flush();
ApplyEntity result = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId());
Optional<ApplyEntity> result = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId());

// then
assertThat(result.getInspection()).isEqualTo(Inspection.STANDBY);
assertThat(result.get().getInspection()).isEqualTo(Inspection.STANDBY);
}

@Test
Expand All @@ -109,7 +111,7 @@ class ApplyRepositoryTest {
userRepository.flush();

// when
ApplyEntity result = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId());
ApplyEntity result = applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()).orElseThrow(ApplyNotFoundException::new);

// then
assertAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindApplyResponse;
import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection;
import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository;
import kr.co.studyhubinu.studyhubserver.exception.apply.SameUserRequestException;
import kr.co.studyhubinu.studyhubserver.exception.user.UserNotFoundException;
import kr.co.studyhubinu.studyhubserver.study.StudyRepository;
import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity;
import kr.co.studyhubinu.studyhubserver.user.domain.UserEntity;
Expand All @@ -26,6 +28,7 @@
import java.util.List;
import java.util.Optional;

import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -53,18 +56,18 @@ class ApplyServiceTest {
EnrollApplyRequest request = EnrollApplyRequest.builder()
.studyId(1L)
.build();
Optional<UserEntity> user = Optional.ofNullable(UserEntity.builder().id(1L).build());
Optional<StudyEntity> study = Optional.ofNullable(StudyEntity.builder().id(1L).build());
UserEntity user = UserEntity.builder().id(1L).build();
StudyEntity study = StudyEntity.builder().id(1L).build();

when(userRepository.findById(anyLong())).thenReturn(user);
when(studyRepository.findById(anyLong())).thenReturn(study);
when(userRepository.findById(anyLong())).thenReturn(Optional.ofNullable(user));
when(studyRepository.findById(anyLong())).thenReturn(Optional.ofNullable(study));
when(applyRepository.save(any())).thenReturn(ApplyEntity.builder()
.userId(user.get().getId())
.study(study.get().getId())
.userId(user.getId())
.study(study.getId())
.build());

// when, then
applyService.enroll(user.get().getId(), request);
applyService.enroll(user.getId(), request);
}

@Test
Expand All @@ -80,7 +83,7 @@ class ApplyServiceTest {
Optional<StudyEntity> study = Optional.ofNullable(StudyEntity.builder().id(1L).build());
when(userRepository.findById(anyLong())).thenReturn(user);
when(studyRepository.findById(anyLong())).thenReturn(study);
when(applyRepository.findByUserIdAndStudyId(1L, 1L)).thenReturn(ApplyEntity.builder().build());
when(applyRepository.findByUserIdAndStudyId(1L, 1L)).thenReturn(Optional.ofNullable(ApplyEntity.builder().build()));

// when, then
applyService.update(request);
Expand Down

0 comments on commit bb9782c

Please sign in to comment.