Skip to content

Commit

Permalink
Refactor: recruit 도메인이 요청 멤버에 대한 정보 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tape4 committed Jul 26, 2024
1 parent 9ae99b8 commit 03d1298
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 85 deletions.
5 changes: 5 additions & 0 deletions src/main/java/umc/kkijuk/server/common/LoginUser.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package umc.kkijuk.server.common;

import lombok.Getter;

@Getter
public class LoginUser {
private Long id;
private static final LoginUser LOGIN_USER = new LoginUser(0L);

public LoginUser(Long id) {
this.id = id;
}

public static LoginUser get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import umc.kkijuk.server.common.domian.exception.RecruitOwnerMismatchException;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.common.domian.exception.ReviewRecruitNotMatchException;
import umc.kkijuk.server.common.domian.exception.ReviewRecruitMismatchException;
import umc.kkijuk.server.common.domian.response.ErrorResponse;

@RestControllerAdvice
Expand All @@ -20,8 +21,14 @@ public ErrorResponse resourceNotFoundException(ResourceNotFoundException excepti
}

@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(ReviewRecruitNotMatchException.class)
public ErrorResponse ReviewRecruitMatchException(ReviewRecruitNotMatchException exception) {
@ExceptionHandler(ReviewRecruitMismatchException.class)
public ErrorResponse ReviewRecruitMatchException(ReviewRecruitMismatchException exception) {
return new ErrorResponse(exception.getMessage());
}

@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(RecruitOwnerMismatchException.class)
public ErrorResponse RecruitOwnerMismatchException(RecruitOwnerMismatchException exception) {
return new ErrorResponse(exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package umc.kkijuk.server.common.domian.exception;

public class RecruitOwnerMismatchException extends RuntimeException {
public RecruitOwnerMismatchException() {
super("해당 공고에 대한 소유권을 가지고 있지 않습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package umc.kkijuk.server.common.domian.exception;

public class ReviewRecruitNotMatchException extends RuntimeException{
public ReviewRecruitNotMatchException(long recruitId, long reviewId) {
public class ReviewRecruitMismatchException extends RuntimeException{
public ReviewRecruitMismatchException(long recruitId, long reviewId) {
super("Recruit "+ recruitId + "에서 Review " + reviewId + "를 찾을수 없습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import umc.kkijuk.server.common.LoginUser;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.recruit.controller.port.RecruitService;
import umc.kkijuk.server.recruit.controller.response.RecruitInfoResponse;
import umc.kkijuk.server.recruit.controller.response.RecruitListByEndDateResponse;
Expand All @@ -31,13 +32,17 @@ public class RecruitController {
private final RecruitService recruitService;
private final ReviewService reviewService;

private final Member requestMember = Member.builder()
.id(LoginUser.get().getId())
.build();

@Operation(
summary = "지원 공고 생성",
description = "주어진 정보를 바탕으로 지원 공고 데이터를 생성합니다.")
@PostMapping
public ResponseEntity<RecruitIdResponse> create(@RequestBody @Valid RecruitCreate recruitCreate) {
LoginUser loginUser = LoginUser.get();
Recruit recruit = recruitService.create(recruitCreate);
// Member requestMember = memberService.findOne(LoginUser.get().getId());
Recruit recruit = recruitService.create(requestMember, recruitCreate);
return ResponseEntity
.status(HttpStatus.CREATED)
.body(RecruitIdResponse.from(recruit));
Expand All @@ -50,10 +55,11 @@ public ResponseEntity<RecruitIdResponse> create(@RequestBody @Valid RecruitCreat
@PutMapping("/{recruitId}")
public ResponseEntity<Long> update(@RequestBody @Valid RecruitUpdate recruitUpdate,
@PathVariable long recruitId) {
LoginUser loginUser = LoginUser.get();
// Member requestMember = memberService.findOne(LoginUser.get().getId());
Recruit recruit = recruitService.update(requestMember, recruitId, recruitUpdate);
return ResponseEntity
.status(HttpStatus.OK)
.body(recruitService.update(recruitId, recruitUpdate).getId());
.body(recruit.getId());
}

@Operation(
Expand All @@ -63,10 +69,11 @@ public ResponseEntity<Long> update(@RequestBody @Valid RecruitUpdate recruitUpda
@PatchMapping("/{recruitId}")
public ResponseEntity<Long> updateState(@RequestBody @Valid RecruitStatusUpdate recruitStatusUpdate,
@PathVariable long recruitId) {
LoginUser loginUser = LoginUser.get();
// Member requestMember = memberService.findOne(LoginUser.get().getId());
Recruit recruit = recruitService.updateStatus(requestMember, recruitId, recruitStatusUpdate);
return ResponseEntity
.ok()
.body(recruitService.updateStatus(recruitId, recruitStatusUpdate).getId());
.body(recruit.getId());
}

@Operation(
Expand All @@ -75,10 +82,11 @@ public ResponseEntity<Long> updateState(@RequestBody @Valid RecruitStatusUpdate
@Parameter(name = "recruitId", description = "지원 공고 ID", example = "1")
@DeleteMapping("/{recruitId}")
public ResponseEntity<Long> delete(@PathVariable long recruitId) {
LoginUser loginUser = LoginUser.get();
// Member requestMember = memberService.findOne(LoginUser.get().getId());
Recruit recruit = recruitService.disable(requestMember, recruitId);
return ResponseEntity
.ok()
.body(recruitService.disable(recruitId).getId());
.body(recruit.getId());
}

@Operation(
Expand All @@ -103,7 +111,8 @@ public ResponseEntity<RecruitInfoResponse> getRecruitInfo(@PathVariable long rec
public ResponseEntity<RecruitListByEndDateResponse> findAllRecruitListByEndTime(
@Parameter(name = "date", description = "지원 공고 마감 날짜", example = "2024-07-20")
@RequestParam LocalDate date) {
List<Recruit> recruits = recruitService.findAllByEndTime(date);
// Member requestMember = memberService.findOne(LoginUser.get().getId());
List<Recruit> recruits = recruitService.findAllByEndTime(requestMember, date);
return ResponseEntity
.ok()
.body(RecruitListByEndDateResponse.from(recruits));
Expand All @@ -117,7 +126,8 @@ public ResponseEntity<RecruitListByEndTimeAfterResponse> findAllRecruitListByEnd
@Parameter(name = "time", description = "이 시간 이후에 마감되는 공고를 요청", example = "2024-07-20 10:30")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
@RequestParam LocalDateTime time) {
List<Recruit> recruits = recruitService.findAllByEndTimeAfter(time);
// Member requestMember = memberService.findOne(LoginUser.get().getId());
List<Recruit> recruits = recruitService.findAllByEndTimeAfter(requestMember, time);
return ResponseEntity
.ok()
.body(RecruitListByEndTimeAfterResponse.from(recruits));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package umc.kkijuk.server.recruit.controller.port;

import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.recruit.domain.Recruit;
import umc.kkijuk.server.recruit.domain.RecruitCreate;
import umc.kkijuk.server.recruit.domain.RecruitStatusUpdate;
Expand All @@ -10,17 +12,18 @@
import java.util.List;

public interface RecruitService {
Recruit create(RecruitCreate recruitCreate);
@Transactional
Recruit create(Member member, RecruitCreate recruitCreate);

Recruit update(Long recruitId, RecruitUpdate recruitUpdate);
Recruit update(Member member, Long recruitId, RecruitUpdate recruitUpdate);

Recruit getById(long id);

Recruit updateStatus(long recruitId, RecruitStatusUpdate recruitStatusUpdate);
Recruit updateStatus(Member member, long recruitId, RecruitStatusUpdate recruitStatusUpdate);

Recruit disable(long recruitId);
Recruit disable(Member member, long recruitId);

List<Recruit> findAllByEndTime(LocalDate date);
List<Recruit> findAllByEndTime(Member member, LocalDate date);

List<Recruit> findAllByEndTimeAfter(LocalDateTime endTime);
List<Recruit> findAllByEndTimeAfter(Member member, LocalDateTime endTime);
}
9 changes: 7 additions & 2 deletions src/main/java/umc/kkijuk/server/recruit/domain/Recruit.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import java.util.ArrayList;
import java.util.List;

@Builder
@Getter
@Builder
public class Recruit {
private final Long id;
private final Long memberId;
private final String title;
private final RecruitStatus status;
private final LocalDateTime startTime;
Expand All @@ -22,8 +23,9 @@ public class Recruit {
private final boolean active;
private final LocalDateTime disabledTime;

public static Recruit from(RecruitCreate recruitCreate) {
public static Recruit from(Long memberId, RecruitCreate recruitCreate) {
return Recruit.builder()
.memberId(memberId)
.title(recruitCreate.getTitle())
.status(recruitCreate.getStatus())
.startTime(recruitCreate.getStartTime())
Expand All @@ -38,6 +40,7 @@ public static Recruit from(RecruitCreate recruitCreate) {
public Recruit update(RecruitUpdate recruitUpdate) {
return Recruit.builder()
.id(this.id)
.memberId(this.memberId)
.title(recruitUpdate.getTitle())
.status(recruitUpdate.getStatus())
.startTime(recruitUpdate.getStartTime())
Expand All @@ -52,6 +55,7 @@ public Recruit update(RecruitUpdate recruitUpdate) {
public Recruit updateStatus(RecruitStatusUpdate recruitStatusUpdate) {
return Recruit.builder()
.id(this.id)
.memberId(this.memberId)
.title(this.getTitle())
.status(recruitStatusUpdate.getStatus())
.startTime(this.getStartTime())
Expand All @@ -66,6 +70,7 @@ public Recruit updateStatus(RecruitStatusUpdate recruitStatusUpdate) {
public Recruit disable() {
return Recruit.builder()
.id(this.id)
.memberId(memberId)
.title(this.getTitle())
.status(this.getStatus())
.startTime(this.getStartTime())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class RecruitEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private Long memberId;

@Column(nullable = false)
private String title;

Expand All @@ -42,6 +45,7 @@ public class RecruitEntity {
public static RecruitEntity from(Recruit recruit) {
RecruitEntity recruitEntity = new RecruitEntity();
recruitEntity.id = recruit.getId();
recruitEntity.memberId = recruit.getMemberId();
recruitEntity.title = recruit.getTitle();
recruitEntity.status = recruit.getStatus();
recruitEntity.startTime = recruit.getStartTime();
Expand All @@ -57,6 +61,7 @@ public static RecruitEntity from(Recruit recruit) {
public Recruit toModel() {
return Recruit.builder()
.id(id)
.memberId(memberId)
.title(title)
.status(status)
.startTime(startTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
public interface RecruitJpaRepository extends JpaRepository<RecruitEntity, Long> {
Optional<RecruitEntity> findByIdAndActive(long id, boolean active);

@Query(value = "SELECT * FROM recruit as e WHERE DATE(e.end_time) = :endTime AND e.active = :isActive", nativeQuery = true)
List<RecruitEntity> findAllByEndDateAndActive(@Param("endTime") LocalDate endTime, @Param("isActive") boolean isActive);
@Query(value = "SELECT * FROM recruit as e WHERE DATE(e.end_time) = :endDate AND e.active = :isActive", nativeQuery = true)
List<RecruitEntity> findAllByEndDateAndActive(@Param("endDate") LocalDate endDate, @Param("isActive") boolean isActive);

@Query(value = "SELECT * FROM recruit as e WHERE e.member_id = :memberId AND DATE(e.end_time) = :endDate AND e.active = :isActive", nativeQuery = true)
List<RecruitEntity> findAllByMemberIdAndEndDateAndActive(@Param("memberId") Long memberId, @Param("endDate") LocalDate endDate, @Param("isActive") boolean isActive);

List<RecruitEntity> findAllByEndTimeAfterAndActive(LocalDateTime endTime, boolean active);
List<RecruitEntity> findAllByMemberIdAndEndTimeAfterAndActive(Long memberId, LocalDateTime endTime, boolean active);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,26 @@ public Optional<Recruit> findByIdAndIsActive(long id, boolean active) {
}

@Override
public List<Recruit> findAllByEndDateAndIsActive(LocalDate endTime, boolean active) {
public List<Recruit> findAllByEndDateAndActive(LocalDate endTime, boolean active) {
return recruitJpaRepository.findAllByEndDateAndActive(endTime, active)
.stream().map(RecruitEntity::toModel).toList();
}

@Override
public List<Recruit> findAllByEndTimeAfterAndIsActive(LocalDateTime endTime, boolean active) {
public List<Recruit> findAllByEndTimeAfterAndActive(LocalDateTime endTime, boolean active) {
return recruitJpaRepository.findAllByEndTimeAfterAndActive(endTime,active)
.stream().map(RecruitEntity::toModel).toList();
}

@Override
public List<Recruit> findAllActiveRecruitByMemberIdAndEndDate(Long memberId, LocalDate endDate) {
return recruitJpaRepository.findAllByMemberIdAndEndDateAndActive(memberId, endDate, true)
.stream().map(RecruitEntity::toModel).toList();
}

@Override
public List<Recruit> findAllActiveRecruitByMemberIdAndEndTimeAfter(Long memberId, LocalDateTime endTime) {
return recruitJpaRepository.findAllByMemberIdAndEndTimeAfterAndActive(memberId, endTime, true)
.stream().map(RecruitEntity::toModel).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.common.domian.exception.RecruitOwnerMismatchException;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.recruit.controller.port.RecruitService;
import umc.kkijuk.server.recruit.domain.Recruit;
import umc.kkijuk.server.recruit.domain.RecruitCreate;
Expand All @@ -31,44 +33,56 @@ public Recruit getById(long id) {

@Override
@Transactional
public Recruit create(RecruitCreate recruitCreate) {
Recruit recruit = Recruit.from(recruitCreate);
public Recruit create(Member requestMember, RecruitCreate recruitCreate) {
Recruit recruit = Recruit.from(requestMember.getId(), recruitCreate);
return recruitRepository.save(recruit);
}

@Override
@Transactional
public Recruit update(Long recruitId, RecruitUpdate recruitUpdate) {
public Recruit update(Member requestMember, Long recruitId, RecruitUpdate recruitUpdate) {
Recruit recruit = getById(recruitId);
if (!recruit.getMemberId().equals(requestMember.getId())) {
throw new RecruitOwnerMismatchException();
}

recruit = recruit.update(recruitUpdate);
return recruitRepository.save(recruit);
}

@Override
@Transactional
public Recruit updateStatus(long recruitId, RecruitStatusUpdate recruitStatusUpdate) {
public Recruit updateStatus(Member requestMember, long recruitId, RecruitStatusUpdate recruitStatusUpdate) {
Recruit recruit = getById(recruitId);
if (!recruit.getMemberId().equals(requestMember.getId())) {
throw new RecruitOwnerMismatchException();
}

recruit = recruit.updateStatus(recruitStatusUpdate);
return recruitRepository.save(recruit);
}

@Override
@Transactional
public Recruit disable(long recruitId) {
public Recruit disable(Member requestMember, long recruitId) {
Recruit recruit = getById(recruitId);
if (!recruit.getMemberId().equals(requestMember.getId())) {
throw new RecruitOwnerMismatchException();
}

recruit = recruit.disable();
return recruitRepository.save(recruit);
}

@Override
@Transactional
public List<Recruit> findAllByEndTime(LocalDate date) {
return recruitRepository.findAllByEndDateAndIsActive(date, true);
public List<Recruit> findAllByEndTime(Member requestMember, LocalDate date) {
return recruitRepository.findAllActiveRecruitByMemberIdAndEndDate(requestMember.getId(), date);
}

@Override
@Transactional
public List<Recruit> findAllByEndTimeAfter(LocalDateTime endTime) {
return recruitRepository.findAllByEndTimeAfterAndIsActive(endTime, true);
public List<Recruit> findAllByEndTimeAfter(Member requestMember, LocalDateTime endTime) {
return recruitRepository.findAllActiveRecruitByMemberIdAndEndTimeAfter(requestMember.getId(), endTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public interface RecruitRepository {
Recruit save(Recruit recruit);
Recruit getById(long id);
Optional<Recruit> findByIdAndIsActive(long id, boolean active);

List<Recruit> findAllByEndDateAndIsActive(LocalDate endTime, boolean active);

List<Recruit> findAllByEndTimeAfterAndIsActive(LocalDateTime endTime, boolean b);
List<Recruit> findAllByEndDateAndActive(LocalDate endTime, boolean active);
List<Recruit> findAllByEndTimeAfterAndActive(LocalDateTime endTime, boolean active);
List<Recruit> findAllActiveRecruitByMemberIdAndEndDate(Long memberId, LocalDate endTime);
List<Recruit> findAllActiveRecruitByMemberIdAndEndTimeAfter(Long memberId, LocalDateTime endTime);
}
Loading

0 comments on commit 03d1298

Please sign in to comment.