Skip to content

Commit

Permalink
Merge pull request #64 from 9oormthon-univ/hyundong
Browse files Browse the repository at this point in the history
feat :: 복지패스 발급 기능 추가
  • Loading branch information
ReasOon authored Dec 3, 2024
2 parents e313965 + 7264882 commit 8807a38
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 199 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.danpoong.onchung.domain.policy.controller;

import com.danpoong.onchung.domain.policy.dto.PolicyPathRequestDto;
import com.danpoong.onchung.domain.policy.dto.PolicyPathResponseDto;
import com.danpoong.onchung.domain.policy.dto.PolicyResponseDto;
import com.danpoong.onchung.domain.policy.dto.RecommendResponseDto;
import com.danpoong.onchung.domain.policy.service.PolicyService;
Expand All @@ -9,6 +11,7 @@
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -19,12 +22,21 @@
public class PolicyController {
private final PolicyService policyService;

@Operation(summary = "정책 단일 조회", description = "조회 시 최근 본 정책에 자동 추가")
@Operation(summary = "정책 상세 조회", description = "조회 시 최근 본 정책에 자동 추가")
@GetMapping("/{policy_id}")
public ResponseTemplate<PolicyResponseDto> getPolicy(@PathVariable("policy_id") Long policyId, HttpServletRequest request, HttpServletResponse response) {
return new ResponseTemplate<>(HttpStatus.OK, "정책 단일 조회 성공", policyService.getPolicy(policyId, request, response));
}

@Operation(summary = "복지패스 발급", description = "반환 값은 피그마 사전 작업 페이지의 카드 순서와 동일(1~10)")
@PostMapping("/path")
public ResponseTemplate<PolicyPathResponseDto> makePolicyPath(
@AuthenticationPrincipal Long userId,
@RequestBody PolicyPathRequestDto requestDto
) {
return new ResponseTemplate<>(HttpStatus.CREATED, "복지패스 발급 성공", policyService.makePolicyPath(userId, requestDto));
}

@Operation(summary = "정책 추천")
@GetMapping("/recommend")
public ResponseTemplate<List<RecommendResponseDto>> recommend(@RequestParam int cardNum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
@Builder
public class FilteringDetails {
private String ageInfo; // 연령 정보
private String employmentStatus;
// @Enumerated(EnumType.STRING)
// private EmploymentStatus employmentStatus; // 취업상태내용
private String employmentStatus; // 취업상태내용
private String specializationField; // 특화분야내용
// @Enumerated(EnumType.STRING)
// private EducationStatus educationRequirement; // 학력요건내용
private String educationRequirement;
private String educationRequirement; // 학력요건내용
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.danpoong.onchung.domain.policy.domain;

import com.danpoong.onchung.domain.policy.domain.enums.PolicyCategory;
import com.danpoong.onchung.domain.policy.domain.enums.PolicyCategoryConverter;
import com.danpoong.onchung.domain.public_office.domain.PublicOffice;
import jakarta.persistence.*;
import lombok.Builder;
Expand Down Expand Up @@ -46,7 +47,7 @@ public class Policy {
@Embedded
private FilteringDetails filteringDetails;

@Enumerated(EnumType.STRING)
@Convert(converter = PolicyCategoryConverter.class)
private PolicyCategory category;

@ManyToMany
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@

@Getter
public enum PolicyCategory {
Employment("23010"), // 일자리 분야
Housing("23020"), // 주거 분야
Education("23030"), //교육 분야
WelfareCulture("23040"), // 복지, 문화 분야
ParticipationRights("23050"); // 참여, 권리 분야
EMPLOYMENT("23010", "일자리 분야"),
HOUSING("23020", "주거 분야"),
EDUCATION("23030", "교육 분야"),
WELFARE_CULTURE("23040", "복지, 문화 분야"),
PARTICIPATION_RIGHTS("23050", "참여, 권리 분야");

private final String code;
private final String displayName;

PolicyCategory(String code) {
PolicyCategory(String code, String displayName) {
this.code = code;
this.displayName = displayName;
}

@Override
public String toString() {
return displayName;
}

public static PolicyCategory findCategory(String code) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.danpoong.onchung.domain.policy.domain.enums;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

@Converter
public class PolicyCategoryConverter implements AttributeConverter<PolicyCategory, String> {

@Override
public String convertToDatabaseColumn(PolicyCategory attribute) {
return attribute != null ? attribute.getDisplayName() : null;
}

@Override
public PolicyCategory convertToEntityAttribute(String dbData) {
for (PolicyCategory category : PolicyCategory.values()) {
if (category.getDisplayName().equals(dbData)) {
return category;
}
}
throw new IllegalArgumentException("Unknown display name: " + dbData);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.danpoong.onchung.domain.policy.domain.enums;

public enum PolicyPath {
예비창업자(1),
공정한근로(2),
커리어(3),
첫보금자리(4),
자취생활(5),
미래를향한(6),
문화(7),
쉼표(8),
함께(9),
권리(10);

private final int number;

PolicyPath(int number) {
this.number = number;
}

public int getNumber() {
return number;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.danpoong.onchung.domain.policy.dto;

public record PolicyPathRequestDto(
String birthDate,
String educationStatus,
String employmentStatus,
String baseInfo,
String interestTopic
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.danpoong.onchung.domain.policy.dto;

import lombok.Builder;

@Builder
public record PolicyPathResponseDto(
int policyPathNum
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import com.danpoong.onchung.domain.policy.domain.Policy;
import com.danpoong.onchung.domain.policy.domain.enums.PolicyCategory;
import com.danpoong.onchung.domain.policy.domain.enums.PolicyPath;
import com.danpoong.onchung.domain.policy.dto.PolicyPathRequestDto;
import com.danpoong.onchung.domain.policy.dto.PolicyPathResponseDto;
import com.danpoong.onchung.domain.policy.dto.PolicyResponseDto;
import com.danpoong.onchung.domain.policy.dto.RecommendResponseDto;
import com.danpoong.onchung.domain.policy.repository.PolicyRepository;
import com.danpoong.onchung.domain.public_office.domain.PublicOffice;
import com.danpoong.onchung.domain.user.domain.UserInfo;
import com.danpoong.onchung.domain.user.exception.UserNotFoundException;
import com.danpoong.onchung.domain.user.repository.UserInfoRepository;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -19,11 +25,8 @@
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PolicyService {
private static final String RECENT_POLICY_COOKIE_NAME = "recentPolicyList";
private static final int COOKIE_EXPIRE_SECONDS = 60 * 60 * 24 * 365 * 10; // 10년

private final PolicyRepository policyRepository;
// private final UserRepository userRepository;
private final UserInfoRepository userInfoRepository;

public PolicyResponseDto getPolicy(Long policyId, HttpServletRequest request, HttpServletResponse response) {
Policy policy = policyRepository.findById(policyId).orElseThrow(() -> new RuntimeException("해당 ID의 정책이 존재하지 않습니다."));
Expand Down Expand Up @@ -52,100 +55,58 @@ public PolicyResponseDto getPolicy(Long policyId, HttpServletRequest request, Ht
.build();
}

public List<RecommendResponseDto> recommendPolicy(int cardNum) {
return policyRepository.findAll().stream()
.filter(p -> p.getCategory().equals(switchIntToCategory(cardNum)))
.filter(p ->
p.getFilteringDetails().getEmploymentStatus().contains(switchIntToEmpoly(cardNum))
||
p.getFilteringDetails().getEmploymentStatus().contains("제한없음")
||
p.getFilteringDetails().getEmploymentStatus().contains("-")
)
.filter(p ->
p.getFilteringDetails().getEducationRequirement().contains(switchIntToEdu(cardNum))
||
p.getFilteringDetails().getEducationRequirement().contains("제한없음")
||
p.getFilteringDetails().getEducationRequirement().contains("-")
)
.map(this::recommendPolicy)
.limit(5)
.toList();
public PolicyPathResponseDto makePolicyPath(Long userId, PolicyPathRequestDto requestDto) {
UserInfo userInfo = userInfoRepository.findById(userId)
.orElseThrow(() -> new UserNotFoundException("해당 ID의 유저 정보를 찾을 수 없습니다."));
userInfo.updateBirthDate(requestDto.birthDate());

PolicyPath tempPolicyPath = determinePolicyPath(requestDto);

userInfo.addPolicyPath(tempPolicyPath);

return PolicyPathResponseDto.builder()
.policyPathNum(tempPolicyPath.getNumber())
.build();
}

// @Transactional
// public void favoritePolicy(Long userId, Long policyId) {
// UserInfo userInfo = userRepository.findById(userId).orElseThrow(() -> new RuntimeException("사용자가 존재하지 않습니다."));
//
// Policy policy = policyRepository.findById(policyId).orElseThrow(() -> new RuntimeException("해당 ID의 정책이 존재하지 않습니다."));
//
// boolean isPresent = userInfo.getFavoritePolicies().contains(policy);
//
// if (!isPresent) {
// userInfo.addFavoritePolicy(policy);
// } else {
// userInfo.removeFavoritePolicy(policy);
// }
// }
private PolicyPath determinePolicyPath(PolicyPathRequestDto requestDto) {
String interestTopic = requestDto.interestTopic();
String educationStatus = requestDto.educationStatus();

private PolicyCategory switchIntToCategory(int cardNum) {
switch (cardNum) {
case 1, 2:
return PolicyCategory.Employment;
case 3, 4:
return PolicyCategory.Housing;
case 5, 6:
return PolicyCategory.Education;
case 7:
return PolicyCategory.WelfareCulture;
default:
return null;
if (interestTopic.contains("일자리")) {
return educationStatus.equals("고교 졸업") ? PolicyPath.공정한근로 : PolicyPath.커리어;
}
}

private String switchIntToEdu(int cardNum) {
switch (cardNum) {
case 3, 5:
// return EducationStatus.고졸_예정;
return "고졸 예정";
case 1:
// return EducationStatus.고교_졸업;
return "고교 졸업";
case 4, 6:
// return EducationStatus.대학_재학;
return "대학 재학";
case 2, 7:
// return EducationStatus.대학_졸업;
return "대학 졸업";
default:
return "제한없음";
if (interestTopic.contains("주거")) {
return educationStatus.equals("고졸 예정") ? PolicyPath.첫보금자리 : PolicyPath.자취생활;
}
}

private String switchIntToEmpoly(int cardNum) {
switch (cardNum) {
case 1:
// return EmploymentStatus.단기근로자;
return "단기근로자";
case 2, 7:
// return EmploymentStatus.재직자_인턴;
return "재직자";
case 3, 4:
// return EmploymentStatus.사회초년생;
return "사회초년생";
case 5:
// return EmploymentStatus.직업훈련생;
return "직업훈련생";
case 6:
// return EmploymentStatus.예비창업자;
return "예비창업자";
default:
return "제한없음";
if (interestTopic.contains("교육")) {
return educationStatus.equals("고졸 예정") ? PolicyPath.미래를향한 : PolicyPath.예비창업자;
}

if (interestTopic.contains("복지")) {
return educationStatus.equals("대졸 예정") ? PolicyPath.쉼표 : PolicyPath.문화;
}

return educationStatus.equals("대졸 예정") ? PolicyPath.권리 : PolicyPath.함께;
}

private RecommendResponseDto recommendPolicy(Policy policy) {
public List<RecommendResponseDto> recommendPolicy(int cardNum) {
PolicyCategory category = switchIntToCategory(cardNum);
String education = switchIntToEdu(cardNum);
String employment = switchIntToEmpoly(cardNum);

return policyRepository.findAll().stream()
.filter(policy -> policy.getCategory().equals(category))
.filter(policy -> matchesEmploymentStatus(policy, employment))
.filter(policy -> matchesEducationRequirement(policy, education))
.map(this::changeToRecommendResponseDto)
.limit(5)
.toList();
}

private RecommendResponseDto changeToRecommendResponseDto(Policy policy) {
return RecommendResponseDto.builder()
.policyId(policy.getId())
.ageInfo(policy.getFilteringDetails().getAgeInfo())
Expand All @@ -164,4 +125,49 @@ private List<String> getPublicOfficeName(List<PublicOffice> publicOffices) {
return publicOfficeNames;
}

private PolicyCategory switchIntToCategory(int cardNum) {
return switch (cardNum) {
case 2, 3 -> PolicyCategory.EMPLOYMENT;
case 4, 5 -> PolicyCategory.HOUSING;
case 6, 1 -> PolicyCategory.EDUCATION;
case 7, 8 -> PolicyCategory.WELFARE_CULTURE;
case 9, 10 -> PolicyCategory.PARTICIPATION_RIGHTS;
default -> null;
};
}

private String switchIntToEdu(int cardNum) {
return switch (cardNum) {
case 4, 6 -> "고졸 예정";
case 2, 7 -> "고교 졸업";
case 1, 5, 9 -> "대학 재학";
case 3, 8, 10 -> "대학 졸업";
default -> "제한없음";
};
}

private String switchIntToEmpoly(int cardNum) {
return switch (cardNum) {
case 2 -> "단기근로자";
case 3, 8 -> "재직자";
case 4 -> "사회초년생";
case 6 -> "직업훈련생";
case 1 -> "예비창업자";
default -> "제한없음";
};
}

private boolean matchesEmploymentStatus(Policy policy, String employment) {
String employmentStatus = policy.getFilteringDetails().getEmploymentStatus();
return employmentStatus.contains(employment) ||
employmentStatus.contains("제한없음") ||
employmentStatus.contains("-");
}

private boolean matchesEducationRequirement(Policy policy, String education) {
String educationRequirement = policy.getFilteringDetails().getEducationRequirement();
return educationRequirement.contains(education) ||
educationRequirement.contains("제한없음") ||
educationRequirement.contains("-");
}
}
Loading

0 comments on commit 8807a38

Please sign in to comment.