Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 기업 인증 API 구현 #39

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -55,4 +52,18 @@ public ApiResponse<List<GetEnterpriseResponse>> getEnterprisesByRegionAndCity(
}
}

@Operation(summary = "기업 인증", description = "사용자가 입력한 정보로 기업을 인증하고 사용자와 매핑합니다.")
@PostMapping("/verify")
public ApiResponse<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -60,5 +63,8 @@ public class Enterprise {

@Column
private Integer certificationYear;

@OneToMany(mappedBy = "enterprise")
private List<User> users;
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface EnterpriseRepository extends JpaRepository<Enterprise, Long> {
List<Enterprise> findAllByRegion(Region region);
List<Enterprise> findAllByRegionAndCity(Region region, String city);
Optional<Enterprise> findByBusinessRegistrationNumberAndRepresentativeNameAndName(
String businessRegistrationNumber,
String representativeName,
String name
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,4 +80,21 @@ public ApiResponse<List<GetEnterpriseResponse>> 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); // 변경사항 저장
}
}
9 changes: 9 additions & 0 deletions src/main/java/danpoong/soenter/domain/user/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package danpoong.soenter.domain.user.entity;

import danpoong.soenter.domain.enterprise.entity.Enterprise;
import jakarta.persistence.*;
import lombok.*;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}
}