Skip to content

Commit

Permalink
feat: 지원 공고 삭제(비활성화) API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tape4 committed Jul 19, 2024
1 parent e63cf30 commit 2fff34d
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.common.domian.response.ErrorResponse;

@RestControllerAdvice
@RequiredArgsConstructor
public class ExceptionControllerAdvice {

@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(ResourceNotFoundException.class)
public String resourceNotFoundException(ResourceNotFoundException exception) {
return exception.getMessage();
public ErrorResponse resourceNotFoundException(ResourceNotFoundException exception) {
return new ErrorResponse(exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package umc.kkijuk.server.common.domian.response;

import lombok.Getter;

@Getter
public class ErrorResponse {
private final String message;

public ErrorResponse(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ public ResponseEntity<Long> updateState(@RequestBody @Valid RecruitStatusUpdate
.ok()
.body(recruitService.updateStatus(recruitId, recruitStatusUpdate).getId());
}

@DeleteMapping("/{recruitId}")
public ResponseEntity<Long> delete(@PathVariable long recruitId) {
LoginUser loginUser = LoginUser.get();
return ResponseEntity
.ok()
.body(recruitService.disable(recruitId).getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface RecruitService {
Recruit getById(long id);

Recruit updateStatus(long recruitId, RecruitStatusUpdate recruitStatusUpdate);

Recruit disable(long recruitId);
}
22 changes: 21 additions & 1 deletion src/main/java/umc/kkijuk/server/recruit/domain/Recruit.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class Recruit {
private final LocalDate applyDate;
private final List<String> tags;
private final String link;
private final Boolean isActive;
private final LocalDateTime disabledTime;

public static Recruit from(RecruitCreateDto recruitCreateDto) {
return Recruit.builder()
Expand All @@ -28,19 +30,21 @@ public static Recruit from(RecruitCreateDto recruitCreateDto) {
.applyDate(recruitCreateDto.getApplyDate())
.tags(recruitCreateDto.getTags())
.link(recruitCreateDto.getLink())
.isActive(true)
.build();
}

public Recruit update(RecruitUpdate recruitUpdate) {
return Recruit.builder()
.id(id)
.id(this.id)
.title(recruitUpdate.getTitle())
.status(recruitUpdate.getStatus())
.startTime(recruitUpdate.getStartTime())
.endTime(recruitUpdate.getEndTime())
.applyDate(recruitUpdate.getApplyDate())
.tags(recruitUpdate.getTags())
.link(recruitUpdate.getLink())
.isActive(this.isActive)
.build();
}

Expand All @@ -54,6 +58,22 @@ public Recruit updateStatus(RecruitStatusUpdate recruitStatusUpdate) {
.applyDate(this.getApplyDate())
.tags(this.getTags())
.link(this.getLink())
.isActive(this.isActive)
.build();
}

public Recruit disable() {
return Recruit.builder()
.id(this.id)
.title(this.getTitle())
.status(this.getStatus())
.startTime(this.getStartTime())
.endTime(this.getEndTime())
.applyDate(this.getApplyDate())
.tags(this.getTags())
.link(this.getLink())
.isActive(false)
.disabledTime(LocalDateTime.now())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.stream.Stream;

public enum RecruitStatus {
INVALID, UNAPPLIED, PLANNED, APPLYING, EJECTED, ACCEPTED;
INVALID, UNAPPLIED, PLANNED, APPLYING, REJECTED, ACCEPTED;

@JsonCreator
public static RecruitStatus parsing(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class RecruitEntity {
private List<String> tags;

private String link;
private Boolean isActive;
private LocalDateTime disabledTime;

public static RecruitEntity from(Recruit recruit) {
RecruitEntity recruitEntity = new RecruitEntity();
Expand All @@ -47,6 +49,8 @@ public static RecruitEntity from(Recruit recruit) {
recruitEntity.applyDate = recruit.getApplyDate();
recruitEntity.tags = recruit.getTags();
recruitEntity.link= recruit.getLink();
recruitEntity.isActive = recruit.getIsActive();
recruitEntity.disabledTime = recruit.getDisabledTime();
return recruitEntity;
}

Expand All @@ -60,6 +64,8 @@ public Recruit toModel() {
.applyDate(applyDate)
.tags(tags)
.link(link)
.isActive(isActive)
.disabledTime(disabledTime)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface RecruitJpaRepository extends JpaRepository<RecruitEntity, Long> {
Optional<RecruitEntity> findByIdAndIsActive(long id, Boolean isActive);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.recruit.domain.Recruit;
import umc.kkijuk.server.recruit.service.port.RecruitRepository;

Expand All @@ -21,4 +22,15 @@ public Optional<Recruit> findById(Long id) {
public Recruit save(Recruit recruit) {
return recruitJpaRepository.save(RecruitEntity.from(recruit)).toModel();
}

@Override
public Recruit getById(long id) {
return findByIdAndIsActive(id, true)
.orElseThrow(() -> new ResourceNotFoundException("recruit", id));
}

@Override
public Optional<Recruit> findByIdAndIsActive(long id, Boolean isActive) {
return recruitJpaRepository.findByIdAndIsActive(id, isActive).map(RecruitEntity::toModel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class RecruitServiceImpl implements RecruitService {

@Override
public Recruit getById(long id) {
return recruitRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Recruit", id));
return recruitRepository.findByIdAndIsActive(id, true)
.orElseThrow(() -> new ResourceNotFoundException("Recruit", id));
}

@Override
Expand All @@ -48,4 +49,12 @@ public Recruit updateStatus(long recruitId, RecruitStatusUpdate recruitStatusUpd
recruit = recruit.updateStatus(recruitStatusUpdate);
return recruitRepository.save(recruit);
}

@Override
@Transactional
public Recruit disable(long recruitId) {
Recruit recruit = getById(recruitId);
recruit = recruit.disable();
return recruitRepository.save(recruit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface RecruitRepository {
Optional<Recruit> findById(Long id);
Recruit save(Recruit recruit);
Recruit getById(long id);
Optional<Recruit> findByIdAndIsActive(long id, Boolean isActive);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package umc.kkijuk.server.recruit.mock;

import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.recruit.domain.Recruit;
import umc.kkijuk.server.recruit.service.port.RecruitRepository;

Expand Down Expand Up @@ -30,6 +31,7 @@ public Recruit save(Recruit recruit) {
.applyDate(recruit.getApplyDate())
.tags(recruit.getTags())
.link(recruit.getLink())
.isActive(recruit.getIsActive())
.build();
data.add(newRecruit);
return newRecruit;
Expand All @@ -39,4 +41,20 @@ public Recruit save(Recruit recruit) {
return recruit;
}
}

@Override
public Recruit getById(long id) {
return findByIdAndIsActive(id, true)
.orElseThrow(() -> new ResourceNotFoundException("recruit", id)
);
}

@Override
public Optional<Recruit> findByIdAndIsActive(long id, Boolean isActive) {
return data.stream()
.filter(item ->
item.getId().equals(id) &&
item.getIsActive() != null &&
item.getIsActive()).findAny();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package umc.kkijuk.server.recruit.service;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
Expand Down Expand Up @@ -35,6 +34,7 @@ void init() {
.applyDate(LocalDate.of(2024, 7, 19))
.tags(new ArrayList<>(Arrays.asList("코딩 테스트", "인턴", "대외 활동")))
.link("test-link")
.isActive(true)
.build();

recruitRepository.save(recruit);
Expand Down Expand Up @@ -162,6 +162,7 @@ void init() {

//when
Recruit result = recruitService.updateStatus(1L, recruitStatusUpdate);

assertAll(
() -> assertThat(result.getId()).isEqualTo(1L),
() -> assertThat(result.getTitle()).isEqualTo("test-title"),
Expand All @@ -174,4 +175,38 @@ void init() {
() -> assertThat(result.getLink()).isEqualTo("test-link")
);
}

@Test
void 기존_recruit_비활성화() {
//given
//when
Recruit result = recruitService.disable(1L);

//then
assertAll(
() -> assertThat(result.getIsActive()).isFalse(),
() -> assertThat(result.getDisabledTime()).isNotNull(),
() -> assertThat(result.getDisabledTime()).isBefore(LocalDateTime.now())
);
}

@Test
void 없는_유저는_비활성화_할수_없다() {
//given
//when
//then
assertThatThrownBy(
() -> recruitService.disable(1234L)).isInstanceOf(ResourceNotFoundException.class);
}

@Test
void 비활성화된_유저는_getById로_찾을수_없다() {
//given
recruitService.disable(1L);

//when
//then
assertThatThrownBy(
() -> recruitService.getById(1L)).isInstanceOf(ResourceNotFoundException.class);
}
}

0 comments on commit 2fff34d

Please sign in to comment.