Skip to content

Commit

Permalink
Merge pull request #87 from study-hub-inu/refactor/SH-317-apply
Browse files Browse the repository at this point in the history
Refactor/sh 317 apply
  • Loading branch information
elyudwo authored Feb 12, 2024
2 parents 94b8f1d + c1da7f0 commit 9e7147e
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.swagger.v3.oas.annotations.Operation;
import kr.co.studyhubinu.studyhubserver.apply.dto.request.*;
import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindApplyResponse;
import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindMyRequestApplyResponse;
import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection;
import kr.co.studyhubinu.studyhubserver.apply.service.ApplyService;
import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindParticipateApplyResponse;
import kr.co.studyhubinu.studyhubserver.user.dto.data.UserId;
Expand All @@ -26,48 +28,56 @@ public class ApplyController {
@Operation(summary = "스터디 참여 신청", description = "studyId와 신청자가 작성한 글을 json 형태로 입력해주세요.")
@PostMapping("/v1/study")
public ResponseEntity<HttpStatus> enrollStudy(UserId userId, EnrollApplyRequest request) {
applyService.enroll(userId.getId(), request);
return ResponseEntity.ok().build();
}

@Operation(summary = "스터디 참여 신청 정보 수정",
description = "참여 신청한 유저의 Id, 해당 스터디 Id, 변경하려는 상태(ACCEPT, STANDBY, REJECT)를 보내주세요")
@PutMapping("/v1/study")
public ResponseEntity<HttpStatus> updateStudyInspection(UpdateApplyRequest request) {
applyService.update(request);
applyService.enroll(userId, request);
return ResponseEntity.ok().build();
}

@Operation(summary = "스터디 참여 신청 거절",
description = "JWT토큰 헤더에 보내주시면 됩니다!")
description = "JWT 헤더에 보내주시면 됩니다!")
@PutMapping("/v1/study-reject")
public ResponseEntity<HttpStatus> rejectApply(@RequestBody @Valid final RejectApplyRequest rejectApplyRequest, final UserId userId) {
applyService.rejectApply(rejectApplyRequest, userId.getId());
applyService.rejectApply(rejectApplyRequest, userId);
return ResponseEntity.ok().build();
}

@Operation(summary = "스터디 참여 신청 거절",
description = "JWT토큰 헤더에 보내주시면 됩니다!")
@Operation(summary = "스터디 참여 신청 수락",
description = "JWT 헤더에 보내주시면 됩니다!")
@PutMapping("/v1/study-accept")
public ResponseEntity<HttpStatus> acceptApply(@RequestBody @Valid final AcceptApplyRequest acceptApplyRequest, final UserId userId) {
applyService.acceptApply(acceptApplyRequest, userId.getId());
applyService.acceptApply(acceptApplyRequest, userId);
return ResponseEntity.ok().build();
}

@Operation(summary = "스터디 참여 신청 정보 조회", description = "해당 스터디 Id를 보내주세요.")
@Operation(summary = "스터디 참여 신청 정보 조회", description = "해당 스터디 Id, 신청 정보를 파라미터로 보내주세요.")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "페이지", required = true),
@ApiImplicitParam(name = "size", value = "사이즈", required = true)
@ApiImplicitParam(name = "size", value = "사이즈", required = true),
@ApiImplicitParam(name = "studyId", value = "스터디 식별자", required = true),
@ApiImplicitParam(name = "inspection", value = "상태", required = true)
})
@GetMapping("/v1/study")
public FindApplyResponse findStudyEnroll(FindApplyRequest request, @RequestParam int page, @RequestParam int size) {
return applyService.findApply(request, page, size);
@GetMapping("/v2/study")
public FindApplyResponse findStudyEnroll(@RequestParam Long studyId, @RequestParam Inspection inspection, @RequestParam int page, @RequestParam int size) {
return applyService.findApply(new FindApplyRequest(studyId, inspection), page, size);
}

@Operation(summary = "내가 참여한 스터디 목록", description = "헤더에 JWT토큰 보내주시면 됩니다")
@Operation(summary = "내가 참여한 스터디 목록", description = "헤더에 JWT 보내주시면 됩니다.")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "페이지", required = true),
@ApiImplicitParam(name = "size", value = "사이즈", required = true)
})
@GetMapping("/v1/participated-study")
public ResponseEntity<FindParticipateApplyResponse> getParticipateApply(UserId userId, @RequestParam int page, @RequestParam int size) {
return ResponseEntity.ok().body(applyService.getParticipateApply(userId.getId(), page, size));
return ResponseEntity.ok().body(applyService.getParticipateApply(userId, page, size));
}

@Operation(summary = "내가 신청한 스터디 목록", description = "헤더에 JWT 보내주시면 됩니다.")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "페이지", required = true),
@ApiImplicitParam(name = "size", value = "사이즈", required = true)
})
@GetMapping("/v1/study-request")
public ResponseEntity<FindMyRequestApplyResponse> getMyRequestApply(UserId userId, @RequestParam int page, @RequestParam int size) {
return ResponseEntity.ok(applyService.getMyRequestApply(userId, page, size));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kr.co.studyhubinu.studyhubserver.apply.dto.data;

import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection;
import lombok.Getter;

@Getter
public class RequestApplyData {

private final Long studyId;
private final String studyTitle;
private final Inspection inspection;
private final String introduce;

public RequestApplyData(Long studyId, String studyTitle, Inspection inspection, String introduce) {
this.studyId = studyId;
this.studyTitle = studyTitle;
this.inspection = inspection;
this.introduce = introduce;
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package kr.co.studyhubinu.studyhubserver.apply.dto.request;

import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection;
import lombok.Builder;
import lombok.Getter;

@Getter
public class FindApplyRequest {

private Long studyId;
private final Long studyId;
private final Inspection inspection;

@Builder
public FindApplyRequest(Long studyId) {
public FindApplyRequest(Long studyId, Inspection inspection) {
this.studyId = studyId;
this.inspection = inspection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package kr.co.studyhubinu.studyhubserver.apply.dto.response;

import kr.co.studyhubinu.studyhubserver.apply.dto.data.ParticipateApplyData;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.RequestApplyData;
import lombok.Getter;
import org.springframework.data.domain.Slice;

@Getter
public class FindMyRequestApplyResponse {

private Long totalCount;
Slice<RequestApplyData> requestStudyData;

public FindMyRequestApplyResponse(Long totalCount, Slice<RequestApplyData> requestStudyData) {
this.totalCount = totalCount;
this.requestStudyData = requestStudyData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class FindParticipateApplyResponse {
private Long totalCount;
Slice<ParticipateApplyData> participateStudyData;

public FindParticipateApplyResponse(Long totalCount, Slice<ParticipateApplyData> participateStudyData) {
this.totalCount = totalCount;
this.participateStudyData = participateStudyData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import kr.co.studyhubinu.studyhubserver.apply.dto.data.ApplyUserData;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.ParticipateApplyData;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.RequestApplyData;
import kr.co.studyhubinu.studyhubserver.apply.dto.request.FindApplyRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
Expand All @@ -11,4 +14,8 @@ public interface ApplyRepositoryCustom {
List<ApplyUserData> findByStudy(Long studyId, Pageable pageable);

List<ParticipateApplyData> findByUserIdAndInspection(Long userId, Pageable pageable);

List<ApplyUserData> findStudyByIdAndInspection(FindApplyRequest request, Pageable pageable);

List<RequestApplyData> findApplyByUserId(Long userId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.ApplyUserData;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.ParticipateApplyData;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.RequestApplyData;
import kr.co.studyhubinu.studyhubserver.apply.dto.request.FindApplyRequest;
import kr.co.studyhubinu.studyhubserver.apply.enums.Inspection;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -39,20 +41,50 @@ public List<ApplyUserData> findByStudy(Long studyId, final Pageable pageable) {

@Override
public List<ParticipateApplyData> findByUserIdAndInspection(Long userId, Pageable pageable) {
return jpaQueryFactory
.select(Projections.constructor(ParticipateApplyData.class,
studyEntity.major, studyEntity.title, studyEntity.content, studyEntity.chatUrl,
applyEntity.inspection, studyPostEntity.id.as("postId")))
.from(applyEntity)
.innerJoin(studyEntity).on(applyEntity.studyId.eq(studyEntity.id))
.innerJoin(studyPostEntity).on(studyEntity.id.eq(studyPostEntity.studyId))
.where(
applyEntity.userId.eq(userId)
.and(applyEntity.inspection.eq(Inspection.ACCEPT))
)
.orderBy(applyEntity.createdDate.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1)
.fetch();
return jpaQueryFactory
.select(Projections.constructor(ParticipateApplyData.class,
studyEntity.major, studyEntity.title, studyEntity.content, studyEntity.chatUrl,
applyEntity.inspection, studyPostEntity.id.as("postId")))
.from(applyEntity)
.innerJoin(studyEntity).on(applyEntity.studyId.eq(studyEntity.id))
.innerJoin(studyPostEntity).on(studyEntity.id.eq(studyPostEntity.studyId))
.where(
applyEntity.userId.eq(userId)
.and(applyEntity.inspection.eq(Inspection.ACCEPT))
)
.orderBy(applyEntity.createdDate.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1)
.fetch();
}

@Override
public List<ApplyUserData> findStudyByIdAndInspection(FindApplyRequest request, Pageable pageable) {
return jpaQueryFactory
.select(Projections.constructor(ApplyUserData.class,
userEntity.id, userEntity.nickname, userEntity.major, userEntity.imageUrl,
applyEntity.introduce, applyEntity.createdDate, applyEntity.inspection))
.from(applyEntity)
.innerJoin(userEntity).on(applyEntity.userId.eq(userEntity.id))
.where(applyEntity.studyId.eq(request.getStudyId())
.and(applyEntity.inspection.eq(request.getInspection())))
.orderBy(applyEntity.createdDate.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1)
.fetch();
}

@Override
public List<RequestApplyData> findApplyByUserId(Long userId, Pageable pageable) {
return jpaQueryFactory
.select(Projections.constructor(RequestApplyData.class,
applyEntity.studyId, studyEntity.title, applyEntity.inspection, applyEntity.introduce))
.from(applyEntity)
.innerJoin(studyEntity).on(applyEntity.studyId.eq(studyEntity.id))
.where(applyEntity.userId.eq(userId))
.orderBy(applyEntity.createdDate.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize() + 1)
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import kr.co.studyhubinu.studyhubserver.apply.domain.ApplyEntity;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.ApplyUserData;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.ParticipateApplyData;
import kr.co.studyhubinu.studyhubserver.apply.dto.data.RequestApplyData;
import kr.co.studyhubinu.studyhubserver.apply.dto.request.*;
import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindApplyResponse;
import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindMyRequestApplyResponse;
import kr.co.studyhubinu.studyhubserver.apply.dto.response.FindParticipateApplyResponse;
import kr.co.studyhubinu.studyhubserver.apply.repository.ApplyRepository;
import kr.co.studyhubinu.studyhubserver.apply.repository.RejectRepository;
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.study.PostNotFoundExceptionByStudyId;
import kr.co.studyhubinu.studyhubserver.exception.user.UserNotFoundException;
import kr.co.studyhubinu.studyhubserver.study.domain.StudyEntity;
import kr.co.studyhubinu.studyhubserver.study.repository.StudyRepository;
import kr.co.studyhubinu.studyhubserver.studypost.domain.StudyPostEntity;
import kr.co.studyhubinu.studyhubserver.studypost.repository.StudyPostRepository;
import kr.co.studyhubinu.studyhubserver.user.domain.UserEntity;
import kr.co.studyhubinu.studyhubserver.user.dto.data.UserId;
import kr.co.studyhubinu.studyhubserver.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
Expand All @@ -36,71 +36,62 @@ public class ApplyService {
private final UserRepository userRepository;
private final StudyRepository studyRepository;
private final ApplyRepository applyRepository;
private final StudyPostRepository studyPostRepository;
private final RejectRepository rejectRepository;

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

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

@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()).orElseThrow(ApplyNotFoundException::new);

isAccept(request);
applyEntity.update(request.getInspection());
}

public FindApplyResponse findApply(FindApplyRequest request, final int page, final int size) {
Pageable pageable = PageRequest.of(page, size);
Slice<ApplyUserData> userData = Converter.toSlice(pageable, applyRepository.findByStudy(request.getStudyId(), pageable));
Slice<ApplyUserData> userData = Converter.toSlice(pageable, applyRepository.findStudyByIdAndInspection(request, pageable));

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

private void validateSameRequest(UserEntity user, StudyEntity study) {
if(applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()).isPresent()) {
throw new SameUserRequestException();
}
}

public FindParticipateApplyResponse getParticipateApply(final Long userId, final int page, final int size) {
UserEntity user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
public FindParticipateApplyResponse getParticipateApply(final UserId userId, final int page, final int size) {
UserEntity user = userRepository.findById(userId.getId()).orElseThrow(UserNotFoundException::new);
final Pageable pageable = PageRequest.of(page, size);
Long totalCount = applyRepository.countByUserIdAndInspection(user.getId(), ACCEPT);
Slice<ParticipateApplyData> participateApplyData = Converter.toSlice
(pageable, applyRepository.findByUserIdAndInspection(user.getId(), pageable));
return new FindParticipateApplyResponse(totalCount, participateApplyData);
}

private void isAccept(UpdateApplyRequest request) {
if(request.getInspection().equals(ACCEPT)) {
StudyPostEntity post = studyPostRepository.findByStudyId(request.getStudyId()).orElseThrow(PostNotFoundExceptionByStudyId::new);
post.minusRemainingSeat();
}
}

@Transactional
public void rejectApply(final RejectApplyRequest rejectApplyRequest, final Long userId) {
UserEntity user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
public void rejectApply(final RejectApplyRequest rejectApplyRequest, final UserId userId) {
userRepository.findById(userId.getId()).orElseThrow(UserNotFoundException::new);
StudyEntity study = studyRepository.findById(rejectApplyRequest.getStudyId()).orElseThrow();
ApplyEntity applyEntity = applyRepository.findByUserIdAndStudyId(rejectApplyRequest.getRejectedUserId(), study.getId()).orElseThrow(ApplyNotFoundException::new);
applyEntity.updateReject();
rejectRepository.save(rejectApplyRequest.toRejectEntity());
}

@Transactional
public void acceptApply(AcceptApplyRequest acceptApplyRequest, Long userId) {
UserEntity user = userRepository.findById(userId).orElseThrow(UserNotFoundException::new);
public void acceptApply(AcceptApplyRequest acceptApplyRequest, UserId userId) {
userRepository.findById(userId.getId()).orElseThrow(UserNotFoundException::new);
StudyEntity study = studyRepository.findById(acceptApplyRequest.getStudyId()).orElseThrow();
ApplyEntity applyEntity = applyRepository.findByUserIdAndStudyId(acceptApplyRequest.getRejectedUserId(), study.getId()).orElseThrow(ApplyNotFoundException::new);
applyEntity.updateAccept();
}

public FindMyRequestApplyResponse getMyRequestApply(UserId userId, int page, int size) {
userRepository.findById(userId.getId()).orElseThrow(UserNotFoundException::new);
final Pageable pageable = PageRequest.of(page, size);
Slice<RequestApplyData> requestApplyData = Converter.toSlice(pageable, applyRepository.findApplyByUserId(userId.getId(), pageable));

return new FindMyRequestApplyResponse((long) size, requestApplyData);
}

private void validateSameRequest(UserEntity user, StudyEntity study) {
if(applyRepository.findByUserIdAndStudyId(user.getId(), study.getId()).isPresent()) {
throw new SameUserRequestException();
}
}
}
Loading

0 comments on commit 9e7147e

Please sign in to comment.