From 01057780505b232a32ae0aea970f400dbeec5fd8 Mon Sep 17 00:00:00 2001 From: haeun1107 Date: Sun, 1 Dec 2024 20:55:07 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EA=B8=B0=EC=97=85=20=EC=9D=B8=EC=A6=9D?= =?UTF-8?q?=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/EnterpriseController.java | 21 ++++++++++++++----- .../domain/enterprise/dto/EnterpriseDTO.java | 11 ++++++++++ .../domain/enterprise/entity/Enterprise.java | 6 ++++++ .../repository/EnterpriseRepository.java | 6 ++++++ .../enterprise/service/EnterpriseService.java | 18 ++++++++++++++++ .../soenter/domain/user/entity/User.java | 9 ++++++++ 6 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/main/java/danpoong/soenter/domain/enterprise/controller/EnterpriseController.java b/src/main/java/danpoong/soenter/domain/enterprise/controller/EnterpriseController.java index c430e4b..f36a804 100644 --- a/src/main/java/danpoong/soenter/domain/enterprise/controller/EnterpriseController.java +++ b/src/main/java/danpoong/soenter/domain/enterprise/controller/EnterpriseController.java @@ -1,19 +1,16 @@ package danpoong.soenter.domain.enterprise.controller; import danpoong.soenter.base.ApiResponse; +import danpoong.soenter.domain.enterprise.dto.EnterpriseDTO.EnterpriseRequest.EnterpriseVerificationRequest; import danpoong.soenter.domain.enterprise.dto.EnterpriseDTO.EnterpriseResponse.GetEnterpriseResponse; import danpoong.soenter.domain.enterprise.dto.VisitDTO.VisitResponse.GetVisitedEnterpriseResponse; import danpoong.soenter.domain.enterprise.service.EnterpriseService; -import danpoong.soenter.domain.enterprise.entity.Enterprise; import danpoong.soenter.domain.enterprise.entity.Region; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.security.core.Authentication; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -55,4 +52,18 @@ public ApiResponse> getEnterprisesByRegionAndCity( } } + @Operation(summary = "기업 인증", description = "사용자가 입력한 정보로 기업을 인증하고 사용자와 매핑합니다.") + @PostMapping("/verify") + public ApiResponse verifyEnterprise( + @RequestBody EnterpriseVerificationRequest request, + Authentication authentication) { + try { + enterpriseService.verifyEnterprise(authentication.getName(), request); + return ApiResponse.onSuccess("기업 인증이 성공적으로 완료되었습니다."); + } catch (IllegalArgumentException e) { + return ApiResponse.onFailure("400", e.getMessage(), null); + } catch (Exception e) { + return ApiResponse.onFailure("500", "기업 인증 중 문제가 발생했습니다.", null); + } + } } \ No newline at end of file diff --git a/src/main/java/danpoong/soenter/domain/enterprise/dto/EnterpriseDTO.java b/src/main/java/danpoong/soenter/domain/enterprise/dto/EnterpriseDTO.java index 81189a3..5bfdd31 100644 --- a/src/main/java/danpoong/soenter/domain/enterprise/dto/EnterpriseDTO.java +++ b/src/main/java/danpoong/soenter/domain/enterprise/dto/EnterpriseDTO.java @@ -4,8 +4,19 @@ import danpoong.soenter.domain.enterprise.entity.Region; import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; public class EnterpriseDTO { + public static class EnterpriseRequest { + @Getter + @NoArgsConstructor + public static class EnterpriseVerificationRequest { + private String businessRegistrationNumber; + private String representativeName; + private String name; + } + } + public static class EnterpriseResponse { @Getter @Builder diff --git a/src/main/java/danpoong/soenter/domain/enterprise/entity/Enterprise.java b/src/main/java/danpoong/soenter/domain/enterprise/entity/Enterprise.java index 9e71556..f607258 100644 --- a/src/main/java/danpoong/soenter/domain/enterprise/entity/Enterprise.java +++ b/src/main/java/danpoong/soenter/domain/enterprise/entity/Enterprise.java @@ -1,8 +1,11 @@ package danpoong.soenter.domain.enterprise.entity; +import danpoong.soenter.domain.user.entity.User; import jakarta.persistence.*; import lombok.*; +import java.util.List; + @Entity @Getter @Builder @@ -60,5 +63,8 @@ public class Enterprise { @Column private Integer certificationYear; + + @OneToMany(mappedBy = "enterprise") + private List users; } diff --git a/src/main/java/danpoong/soenter/domain/enterprise/repository/EnterpriseRepository.java b/src/main/java/danpoong/soenter/domain/enterprise/repository/EnterpriseRepository.java index 3e95259..ee4c209 100644 --- a/src/main/java/danpoong/soenter/domain/enterprise/repository/EnterpriseRepository.java +++ b/src/main/java/danpoong/soenter/domain/enterprise/repository/EnterpriseRepository.java @@ -5,8 +5,14 @@ import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; +import java.util.Optional; public interface EnterpriseRepository extends JpaRepository { List findAllByRegion(Region region); List findAllByRegionAndCity(Region region, String city); + Optional findByBusinessRegistrationNumberAndRepresentativeNameAndName( + String businessRegistrationNumber, + String representativeName, + String name + ); } diff --git a/src/main/java/danpoong/soenter/domain/enterprise/service/EnterpriseService.java b/src/main/java/danpoong/soenter/domain/enterprise/service/EnterpriseService.java index f10cc81..efacff3 100644 --- a/src/main/java/danpoong/soenter/domain/enterprise/service/EnterpriseService.java +++ b/src/main/java/danpoong/soenter/domain/enterprise/service/EnterpriseService.java @@ -3,6 +3,7 @@ import danpoong.soenter.base.ApiResponse; import danpoong.soenter.domain.enterprise.converter.EnterpriseConverter; import danpoong.soenter.domain.enterprise.converter.VisitConverter; +import danpoong.soenter.domain.enterprise.dto.EnterpriseDTO.EnterpriseRequest.EnterpriseVerificationRequest; import danpoong.soenter.domain.enterprise.dto.EnterpriseDTO.EnterpriseResponse.GetEnterpriseResponse; import danpoong.soenter.domain.enterprise.dto.VisitDTO.VisitResponse.GetVisitedEnterpriseResponse; import danpoong.soenter.domain.enterprise.entity.Enterprise; @@ -79,4 +80,21 @@ public ApiResponse> getEnterprisesByRegionAndCity(Re return ApiResponse.onFailure("500", "지역 및 도시별 기업 정보를 가져오는 데 실패했습니다.", null); } } + + public void verifyEnterprise(String userId, EnterpriseVerificationRequest request) { + // 기업 검색 + Enterprise enterprise = enterpriseRepository.findByBusinessRegistrationNumberAndRepresentativeNameAndName( + request.getBusinessRegistrationNumber(), + request.getRepresentativeName(), + request.getName() + ).orElseThrow(() -> new IllegalArgumentException("일치하는 기업을 찾을 수 없습니다.")); + + // 사용자 조회 + User user = userRepository.findById(Long.valueOf(userId)) + .orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다.")); + + // 사용자와 기업 매핑 + user.updateEnterprise(enterprise.getEnterpriseId()); + userRepository.save(user); // 변경사항 저장 + } } \ No newline at end of file diff --git a/src/main/java/danpoong/soenter/domain/user/entity/User.java b/src/main/java/danpoong/soenter/domain/user/entity/User.java index a407a02..043202a 100644 --- a/src/main/java/danpoong/soenter/domain/user/entity/User.java +++ b/src/main/java/danpoong/soenter/domain/user/entity/User.java @@ -1,5 +1,6 @@ package danpoong.soenter.domain.user.entity; +import danpoong.soenter.domain.enterprise.entity.Enterprise; import jakarta.persistence.*; import lombok.*; @@ -40,6 +41,10 @@ public class User { @Column private String kakaoAccess; + @ManyToOne + @JoinColumn(name = "enterprise_id") + private Enterprise enterprise; + public void updateBirthDate(LocalDate birth) { this.birth = birth; } @@ -51,4 +56,8 @@ public void updateKakaoAccess(String kakaoAccess) { public void updateCityDate(String city) { this.city = city; } + + public void updateEnterprise(Long enterpriseId) { + this.enterprise = Enterprise.builder().enterpriseId(enterpriseId).build(); + } }