Skip to content

Commit

Permalink
feat: 지원 공고 목록 (특정 시간 이후) API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tape4 committed Jul 20, 2024
1 parent f9a6660 commit afcd1b9
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.*;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import umc.kkijuk.server.common.LoginUser;
import umc.kkijuk.server.recruit.controller.port.RecruitService;
import umc.kkijuk.server.recruit.controller.response.RecruitInfoResponse;
import umc.kkijuk.server.recruit.controller.response.RecruitListByEndTimeResponse;
import umc.kkijuk.server.recruit.controller.response.RecruitListByEndDateResponse;
import umc.kkijuk.server.recruit.controller.response.RecruitListByEndTimeAfterResponse;
import umc.kkijuk.server.recruit.domain.*;
import umc.kkijuk.server.recruit.controller.response.RecruitIdResponse;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -92,15 +95,29 @@ public ResponseEntity<RecruitInfoResponse> getRecruitInfo(@PathVariable long rec
.body(RecruitInfoResponse.from(recruit, new ArrayList<>()));
}
@Operation(
summary = "지원 공고 목록 (마감 날짜 기준)",
summary = "지원 공고 목록 (특정 날짜 이후)",
description = "주어진 날짜에 마감 종료되는 지원 공고들의 목록을 요청합니다.")
@GetMapping
public ResponseEntity<RecruitListByEndTimeResponse> getRecruitListByEndTime(
@GetMapping("/list/end")
public ResponseEntity<RecruitListByEndDateResponse> findAllRecruitListByEndTime(
@Parameter(name = "date", description = "지원 공고 마감 날짜", example = "2024-07-20")
LocalDate date) {
@RequestParam LocalDate date) {
List<Recruit> recruits = recruitService.findAllByEndTime(date);
return ResponseEntity
.ok()
.body(RecruitListByEndTimeResponse.from(recruits));
.body(RecruitListByEndDateResponse.from(recruits));
}

@Operation(
summary = "지원 공고 목록 (특정 시간 이후)",
description = "주어진 시간 이후 마감 종료되는 지원 공고들의 목록을 요청합니다.")
@GetMapping("/list/after")
public ResponseEntity<RecruitListByEndTimeAfterResponse> findAllRecruitListByEndTimeAfterNow(
@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);
return ResponseEntity
.ok()
.body(RecruitListByEndTimeAfterResponse.from(recruits));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import umc.kkijuk.server.recruit.domain.RecruitUpdate;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

public interface RecruitService {
Expand All @@ -20,4 +21,6 @@ public interface RecruitService {
Recruit disable(long recruitId);

List<Recruit> findAllByEndTime(LocalDate date);

List<Recruit> findAllByEndTimeAfter(LocalDateTime endTime);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package umc.kkijuk.server.recruit.controller.response;

import lombok.Builder;
import lombok.Getter;
import umc.kkijuk.server.recruit.domain.Recruit;

import java.time.LocalDate;
import java.util.*;

@Getter
@Builder
public class RecruitByEndDate {
private final LocalDate endDate;
private int count;
private List<RecruitListResponse> recruits;

public static List<RecruitByEndDate> from(List<Recruit> recruits) {
List<RecruitByEndDate> outputs = new ArrayList<>();
recruits.forEach(recruit -> {
Optional<RecruitByEndDate> o = outputs.stream()
.filter(item -> recruit.getEndTime().toLocalDate().isEqual(item.getEndDate()))
.findFirst();
o.ifPresent(recruitByEndDate -> {
recruitByEndDate.recruits.add(RecruitListResponse.from(recruit));
recruitByEndDate.count++;
});
if (o.isEmpty()) {
outputs.add(RecruitByEndDate.builder()
.endDate(recruit.getEndTime().toLocalDate())
.count(1)
.recruits(new ArrayList<>(Arrays.asList(RecruitListResponse.from(recruit))))
.build());
}
});

outputs.sort(Comparator.comparing(RecruitByEndDate::getEndDate));
return outputs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

@Getter
@Builder
public class RecruitListByEndTimeResponse {
public class RecruitListByEndDateResponse {
private final int count;
private final List<RecruitListResponse> recruits;

public static RecruitListByEndTimeResponse from(List<Recruit> recruits) {
return RecruitListByEndTimeResponse.builder()
public static RecruitListByEndDateResponse from(List<Recruit> recruits) {
return RecruitListByEndDateResponse.builder()
.count(recruits.size())
.recruits(recruits.stream().map(RecruitListResponse::from).toList())
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package umc.kkijuk.server.recruit.controller.response;

import lombok.Builder;
import lombok.Getter;
import umc.kkijuk.server.recruit.domain.Recruit;

import java.util.List;

@Getter
@Builder
public class RecruitListByEndTimeAfterResponse {
private final int totalCount;
private final List<RecruitByEndDate> outputs;

public static RecruitListByEndTimeAfterResponse from(List<Recruit> recruits) {
return RecruitListByEndTimeAfterResponse.builder()
.totalCount(recruits.size())
.outputs(RecruitByEndDate.from(recruits))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand All @@ -13,4 +14,6 @@ public interface RecruitJpaRepository extends JpaRepository<RecruitEntity, Long>

@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);

List<RecruitEntity> findAllByEndTimeAfterAndActive(LocalDateTime endTime, boolean active);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ public List<Recruit> findAllByEndDateAndIsActive(LocalDate endTime, boolean acti
return recruitJpaRepository.findAllByEndDateAndActive(endTime, active)
.stream().map(RecruitEntity::toModel).toList();
}

@Override
public List<Recruit> findAllByEndTimeAfterAndIsActive(LocalDateTime endTime, boolean active) {
return recruitJpaRepository.findAllByEndTimeAfterAndActive(endTime,active)
.stream().map(RecruitEntity::toModel).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import umc.kkijuk.server.recruit.service.port.RecruitRepository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
Expand Down Expand Up @@ -64,4 +65,10 @@ public Recruit disable(long recruitId) {
public List<Recruit> findAllByEndTime(LocalDate date) {
return recruitRepository.findAllByEndDateAndIsActive(date, true);
}

@Override
@Transactional
public List<Recruit> findAllByEndTimeAfter(LocalDateTime endTime) {
return recruitRepository.findAllByEndTimeAfterAndIsActive(endTime, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import umc.kkijuk.server.recruit.domain.Recruit;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand All @@ -13,4 +14,6 @@ public interface RecruitRepository {
Optional<Recruit> findByIdAndIsActive(long id, boolean active);

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

List<Recruit> findAllByEndTimeAfterAndIsActive(LocalDateTime endTime, boolean b);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import umc.kkijuk.server.recruit.service.port.RecruitRepository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -61,8 +62,14 @@ public Optional<Recruit> findByIdAndIsActive(long id, boolean active) {
@Override
public List<Recruit> findAllByEndDateAndIsActive(LocalDate endTime, boolean active) {
return data.stream()
.filter(item ->
endTime.equals(item.getEndTime().toLocalDate()) &&
.filter(item -> endTime.equals(item.getEndTime().toLocalDate()) &&
item.isActive() == active).toList();
}

@Override
public List<Recruit> findAllByEndTimeAfterAndIsActive(LocalDateTime endTime, boolean active) {
return data.stream()
.filter(item -> item.getEndTime().isAfter(endTime) &&
item.isActive() == active).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void init() {
}

@Test
void getByEndTime_마감시간이_date인_모든_active공고를_불러온다() {
void findAllByEndTime_마감시간이_date인_모든_active공고를_불러온다() {
//given
LocalDateTime dateTime = LocalDateTime.of(2024, 7, 30, 2, 30);
int times = 10;
Expand All @@ -231,7 +231,7 @@ void init() {
}

@Test
void getByEndTime_마감시간이_date인_inactive공고는_제외한다() {
void findAllByEndTime_마감시간이_date인_inactive공고는_제외한다() {
//given
LocalDateTime dateTime = LocalDateTime.of(2024, 7, 30, 2, 30);
int times = 10;
Expand All @@ -250,7 +250,7 @@ void init() {
}

@Test
void getByEndTime_active_inactive공고() {
void findAllByEndTime_active_inactive공고() {
//given
LocalDateTime dateTime = LocalDateTime.of(2024, 7, 30, 2, 30);
int times = 10;
Expand All @@ -267,4 +267,26 @@ void init() {
//then
assertThat(result.size()).isEqualTo(times / 2);
}

@Test
void findAllByEndTimeAfter_마감이_안지난_active공고를_불러온다() {
//given
//when
List<Recruit> result = recruitService.findAllByEndTimeAfter(LocalDateTime.of(2024, 1, 1, 1, 1));

//then
assertThat(result.size()).isOne();
}

@Test
void findAllByEndTimeAfter_inactive공고는_제외한다() {
//given
recruitService.disable(1L);

//when
List<Recruit> result = recruitService.findAllByEndTimeAfter(LocalDateTime.of(2024, 1, 1, 1, 1));

//then
assertThat(result.size()).isZero();
}
}

0 comments on commit afcd1b9

Please sign in to comment.