Skip to content

Commit

Permalink
Merge pull request #49 from Team-B1ND/Feature/#47
Browse files Browse the repository at this point in the history
Feature/#47
  • Loading branch information
leeggmin authored Mar 26, 2024
2 parents 72c0dc5 + 08fc73a commit b9e4197
Show file tree
Hide file tree
Showing 17 changed files with 457 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package b1nd.dodamapi.recruit;

import b1nd.dodamapi.common.response.Response;
import b1nd.dodamapi.common.response.ResponseData;
import b1nd.dodamcore.recruit.application.RecruitService;
import b1nd.dodamcore.recruit.application.dto.req.RecruitReq;
import b1nd.dodamcore.recruit.application.dto.res.RecruitPageRes;
import b1nd.dodamcore.recruit.application.dto.res.RecruitRes;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/recruit")
@RequiredArgsConstructor
public class RecruitController {

private final RecruitService recruitService;

@GetMapping
public ResponseData<RecruitPageRes> getRecruits(@RequestParam(name = "page") int page) {
RecruitPageRes recruitList = recruitService.getRecruitsByPaging(page);
return ResponseData.ok("채용 의뢰서 조회 성공", recruitList);
}

@GetMapping("/{id}")
public ResponseData<RecruitRes> getRecruitById(@PathVariable int id) {
RecruitRes recruit = recruitService.getRecruitById(id);
return ResponseData.ok("채용 의뢰서 단일 조회 성공", recruit);
}

@PostMapping
public Response createRecruit(@RequestBody @Valid RecruitReq createRecruitReq) {
recruitService.createRecruit(createRecruitReq);
return Response.created("채용 의뢰서 작성 성공");
}

@PatchMapping("/{id}")
public Response modifyRecruit(
@PathVariable int id,
@RequestBody RecruitReq modifyRecruitReq
) {
recruitService.modifyRecruit(id, modifyRecruitReq);
return Response.ok("채용 의뢰서 수정 성공");
}

@DeleteMapping("/{id}")
public Response deleteRecruit(@PathVariable int id) {
recruitService.deleteRecruit(id);
return Response.ok("채용 의뢰서 삭제 성공");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public Response modifySchedule(
}

@DeleteMapping("/{id}")
public Response deleteSchedule(
@PathVariable int id
) {
public Response deleteSchedule(@PathVariable int id) {
scheduleService.deleteSchedule(id);
return Response.ok("일정 삭제 성공");
}
Expand All @@ -61,9 +59,7 @@ public ResponseData<List<ScheduleRes>> getScheduleByDate(
}

@GetMapping("/search/keyword")
public ResponseData<List<ScheduleRes>> getScheduleByKeyword(
@RequestParam("keyword") String keyword
) {
public ResponseData<List<ScheduleRes>> getScheduleByKeyword(@RequestParam("keyword") String keyword) {
List<ScheduleRes> scheduleList = scheduleService.getScheduleByKeyword(keyword);
return ResponseData.ok("해당 키워드의 일정 조회 성공", scheduleList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import b1nd.dodamcore.banner.repository.BannerRepository;
import b1nd.dodamcore.common.util.ModifyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -42,11 +41,11 @@ public void modifyBanner(int id, BannerReq modifyBannerReq) {

@Transactional(rollbackFor = Exception.class)
public void deleteBanner(int id) {
try {
bannerRepository.deleteById(id);
} catch (EmptyResultDataAccessException e) {
throw new BannerNotFoundException();
}

Banner banner = bannerRepository.findById(id)
.orElseThrow(BannerNotFoundException::new);

bannerRepository.delete(banner);
}

@Transactional(rollbackFor = Exception.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import b1nd.dodamcore.member.domain.entity.Student;
import b1nd.dodamcore.member.repository.StudentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -104,11 +103,11 @@ public void modifyBus(int id, BusReq modifyBusReq) {

@Transactional(rollbackFor = Exception.class)
public void deleteBus(int id) {
try {
busRepository.deleteById(id);
} catch (EmptyResultDataAccessException e) {
throw new BusNotFoundException();
}

Bus bus = busRepository.findById(id)
.orElseThrow(BusNotFoundException::new);

busRepository.delete(bus);
}

@Transactional(rollbackFor = Exception.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package b1nd.dodamcore.recruit.application;

import b1nd.dodamcore.common.util.ModifyUtil;
import b1nd.dodamcore.member.application.MemberSessionHolder;
import b1nd.dodamcore.member.domain.entity.Member;
import b1nd.dodamcore.recruit.application.dto.req.RecruitReq;
import b1nd.dodamcore.recruit.application.dto.res.RecruitPageRes;
import b1nd.dodamcore.recruit.application.dto.res.RecruitRes;
import b1nd.dodamcore.recruit.domain.entity.Recruit;
import b1nd.dodamcore.recruit.domain.exception.RecruitNotFoundException;
import b1nd.dodamcore.recruit.repository.RecruitFileRepository;
import b1nd.dodamcore.recruit.repository.RecruitRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

private final RecruitRepository recruitRepository;
private final RecruitFileRepository recruitFileRepository;
private final MemberSessionHolder memberSessionHolder;

public RecruitPageRes getRecruitsByPaging(int page) {

Pageable pageRequest = PageRequest.of(page - 1, 10);
Page<Recruit> recruitList = recruitRepository.findAllByOrderByIdDesc(pageRequest);

int pageNum = recruitList.getTotalPages() - recruitList.getNumber();
Integer nextPage = pageNum <= 1 ? null : pageNum;

return RecruitPageRes.of(recruitList, nextPage);
}

public RecruitRes getRecruitById(int id) {

Recruit recruit = recruitRepository.findByIdWithJoin(id)
.orElseThrow(RecruitNotFoundException::new);

return RecruitRes.of(recruit);
}

@Transactional(rollbackFor = Exception.class)
public void createRecruit(RecruitReq createRecruitReq) {

Member member = memberSessionHolder.current();
Recruit recruit = createRecruitReq.mapToRecruit(member);

recruitRepository.save(recruit);
}

@Transactional(rollbackFor = Exception.class)
public void modifyRecruit(int id, RecruitReq dto) {

Recruit recruit = recruitRepository.findById(id)
.orElseThrow(RecruitNotFoundException::new);

recruitFileRepository.deleteByRecruit_Id(id);

recruit.updateRecruit(
ModifyUtil.modifyIfNotNull(dto.name(), recruit.getName()),
ModifyUtil.modifyIfNotNull(dto.location(), recruit.getLocation()),
ModifyUtil.modifyIfNotNull(dto.duty(), recruit.getDuty()),
ModifyUtil.modifyIfNotNull(dto.etc(), recruit.getEtc()),
ModifyUtil.modifyIfNotZero(dto.personnel(), recruit.getPersonnel()),
ModifyUtil.modifyIfNotNull(dto.image(), recruit.getImage()),
dto.updateRecruitFile(recruit)
);
}

@Transactional(rollbackFor = Exception.class)
public void deleteRecruit(int id) {

Recruit recruit = recruitRepository.findById(id)
.orElseThrow(RecruitNotFoundException::new);

recruitRepository.delete(recruit);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package b1nd.dodamcore.recruit.application.dto;

import b1nd.dodamcore.recruit.domain.entity.RecruitFile;
import jakarta.validation.constraints.NotEmpty;

import java.util.List;
import java.util.stream.Collectors;

public record Pdf(@NotEmpty String url, @NotEmpty String name) {

public static List<Pdf> of(List<RecruitFile> recruitFiles) {
return recruitFiles.stream().map(recruitFile ->
new Pdf(recruitFile.getPdfUrl(), recruitFile.getPdfName())
).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package b1nd.dodamcore.recruit.application.dto.req;

import b1nd.dodamcore.member.domain.entity.Member;
import b1nd.dodamcore.recruit.application.dto.Pdf;
import b1nd.dodamcore.recruit.domain.entity.Recruit;
import b1nd.dodamcore.recruit.domain.entity.RecruitFile;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;

import java.util.List;
import java.util.stream.Collectors;

public record RecruitReq(@NotEmpty String name, @NotEmpty String location, @NotEmpty String duty,
String etc, @Positive @NotNull int personnel, @NotEmpty String image, List<Pdf> pdfs) {

public Recruit mapToRecruit(Member member) {
Recruit recruit = Recruit.builder()
.writer(member)
.name(name)
.location(location)
.duty(duty)
.etc(etc)
.personnel(personnel)
.image(image)
.build();

pdfs.forEach(pdf -> recruit.addRecruitFile(
RecruitFile.builder()
.pdfName(pdf.name())
.pdfUrl(pdf.url())
.build()
));

return recruit;
}

public List<RecruitFile> updateRecruitFile(Recruit recruit) {
return pdfs.stream().map(pdf ->
RecruitFile.builder()
.recruit(recruit)
.pdfName(pdf.name())
.pdfUrl(pdf.url())
.build()
).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package b1nd.dodamcore.recruit.application.dto.res;

import b1nd.dodamcore.recruit.domain.entity.Recruit;
import org.springframework.data.domain.Page;

import java.util.List;
import java.util.stream.Collectors;

public record RecruitPageRes(List<RecruitListRes> recruitList, Integer nextPage) {

public static RecruitPageRes of(Page<Recruit> recruitPage, Integer nextPage) {
return new RecruitPageRes(recruitPage.getContent().stream().map(recruit ->
new RecruitListRes(
recruit.getId(),
recruit.getWriter().getName(),
recruit.getName(),
recruit.getLocation(),
recruit.getDuty(),
recruit.getEtc(),
recruit.getPersonnel(),
recruit.getImage()
)).collect(Collectors.toList()),
nextPage);
}

public record RecruitListRes(int id, String writer, String name, String location, String duty,
String etc, int personnel, String image) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package b1nd.dodamcore.recruit.application.dto.res;

import b1nd.dodamcore.recruit.application.dto.Pdf;
import b1nd.dodamcore.recruit.domain.entity.Recruit;

import java.util.List;

public record RecruitRes(String writer, String name, String location, String duty,
String etc, int personnel, String image, List<Pdf> pdfs) {

public static RecruitRes of(Recruit recruit) {
return new RecruitRes(
recruit.getWriter().getName(),
recruit.getName(),
recruit.getLocation(),
recruit.getDuty(),
recruit.getEtc(),
recruit.getPersonnel(),
recruit.getImage(),
Pdf.of(recruit.getRecruitFiles())
);
}
}
Loading

0 comments on commit b9e4197

Please sign in to comment.