diff --git a/module-api/src/main/java/kernel/jdon/jobcategory/controller/JobCategoryController.java b/module-api/src/main/java/kernel/jdon/jobcategory/controller/JobCategoryController.java new file mode 100644 index 000000000..ff34f8a1c --- /dev/null +++ b/module-api/src/main/java/kernel/jdon/jobcategory/controller/JobCategoryController.java @@ -0,0 +1,24 @@ +package kernel.jdon.jobcategory.controller; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import kernel.jdon.dto.response.CommonResponse; +import kernel.jdon.jobcategory.dto.response.FindListJobGroupResponse; +import kernel.jdon.jobcategory.service.JobCategoryService; +import lombok.RequiredArgsConstructor; + +@RestController +@RequiredArgsConstructor +public class JobCategoryController { + private final JobCategoryService jobCategoryService; + + @GetMapping("/api/v1/job-categories") + public ResponseEntity getJobGroupList() { + FindListJobGroupResponse jobGroupList = jobCategoryService.findJobGroupList(); + + return ResponseEntity.ok(CommonResponse.of(jobGroupList)); + } + +} diff --git a/module-api/src/main/java/kernel/jdon/jobcategory/dto/response/FindListJobGroupResponse.java b/module-api/src/main/java/kernel/jdon/jobcategory/dto/response/FindListJobGroupResponse.java new file mode 100644 index 000000000..30eb7f8bc --- /dev/null +++ b/module-api/src/main/java/kernel/jdon/jobcategory/dto/response/FindListJobGroupResponse.java @@ -0,0 +1,57 @@ +package kernel.jdon.jobcategory.dto.response; + +import java.util.List; +import java.util.Map; + +import kernel.jdon.jobcategory.domain.JobCategory; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class FindListJobGroupResponse { + private List jobGroupList; + + public FindListJobGroupResponse(List jobGroupList, Map> jobCategoryMap) { + this.jobGroupList = jobGroupList.stream() + .map(jobGroup -> FindJobGroupResponse.of(jobGroup, jobCategoryMap.get(jobGroup.getId()))) + .toList(); + } + + @Getter + @Builder + @AllArgsConstructor + public static class FindJobGroupResponse { + private Long id; + private String name; + private List jobCategoryList; + + public static FindJobGroupResponse of(JobCategory jobCategory, List jobCategoryList) { + List list = jobCategoryList.stream() + .map(FindJobCategoryResponse::of) + .toList(); + + return FindJobGroupResponse.builder() + .id(jobCategory.getId()) + .name(jobCategory.getName()) + .jobCategoryList(list) + .build(); + } + } + + @Getter + @Builder + @AllArgsConstructor + public static class FindJobCategoryResponse { + private Long id; + private String name; + + public static FindJobCategoryResponse of(JobCategory jobCategory) { + return FindJobCategoryResponse.builder() + .id(jobCategory.getId()) + .name(jobCategory.getName()) + .build(); + } + } +} diff --git a/module-api/src/main/java/kernel/jdon/jobcategory/repository/JobCategoryRepository.java b/module-api/src/main/java/kernel/jdon/jobcategory/repository/JobCategoryRepository.java index e6319ce9f..07f877d30 100644 --- a/module-api/src/main/java/kernel/jdon/jobcategory/repository/JobCategoryRepository.java +++ b/module-api/src/main/java/kernel/jdon/jobcategory/repository/JobCategoryRepository.java @@ -1,4 +1,10 @@ package kernel.jdon.jobcategory.repository; +import java.util.List; + +import kernel.jdon.jobcategory.domain.JobCategory; + public interface JobCategoryRepository extends JobCategoryDomainRepository { + List findByParentIdIsNull(); + List findByParentId(Long pareantId); } diff --git a/module-api/src/main/java/kernel/jdon/jobcategory/service/JobCategoryService.java b/module-api/src/main/java/kernel/jdon/jobcategory/service/JobCategoryService.java new file mode 100644 index 000000000..5b334ffe4 --- /dev/null +++ b/module-api/src/main/java/kernel/jdon/jobcategory/service/JobCategoryService.java @@ -0,0 +1,27 @@ +package kernel.jdon.jobcategory.service; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.springframework.stereotype.Service; + +import kernel.jdon.jobcategory.domain.JobCategory; +import kernel.jdon.jobcategory.dto.response.FindListJobGroupResponse; +import kernel.jdon.jobcategory.repository.JobCategoryRepository; +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +public class JobCategoryService { + private final JobCategoryRepository jobCategoryRepository; + + public FindListJobGroupResponse findJobGroupList() { + List findJobGroupList = jobCategoryRepository.findByParentIdIsNull(); + Map> groupedCategoryList = findJobGroupList.stream() + .collect(Collectors.toMap(JobCategory::getId, jobGroup -> + jobCategoryRepository.findByParentId(jobGroup.getId()))); + + return new FindListJobGroupResponse(findJobGroupList, groupedCategoryList); + } +}