Skip to content

Commit

Permalink
Merge pull request #15 from 9oormthon-univ/feature/#14
Browse files Browse the repository at this point in the history
[FEAT] 방문 기업 조회 API 구현
  • Loading branch information
haeun1107 authored Nov 19, 2024
2 parents b2a865d + 1eceec0 commit 936052e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package danpoong.soenter.domain.enterprise.controller;

import danpoong.soenter.base.ApiResponse;
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;
Expand All @@ -27,4 +29,13 @@ public class EnterpriseController {
public ApiResponse<List<Enterprise>> getEnterprisesByRegion(@PathVariable("region") Region region) {
return enterpriseService.getEnterprisesByRegion(region);
}

@GetMapping("/users/visit")
@Operation(summary = "사용자가 방문한 기업 조회 API", description = "사용자가 방문한 모든 기업을 조회합니다.")
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공")
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON400", description = "잘못된 요청입니다.")
public ApiResponse<List<GetVisitedEnterpriseResponse>> getVisitedEnterprises(Authentication authentication) {
List<GetVisitedEnterpriseResponse>response = enterpriseService.getVisitedEnterprises(authentication.getName());
return ApiResponse.onSuccess(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package danpoong.soenter.domain.enterprise.dto;

import danpoong.soenter.domain.enterprise.entity.Enterprise;
import danpoong.soenter.domain.enterprise.dto.VisitDTO.VisitResponse.GetVisitedEnterpriseResponse;
public class VisitConverter {
public static GetVisitedEnterpriseResponse toVisitedEnterpriseResponse(Enterprise enterprise) {
return GetVisitedEnterpriseResponse.builder()
.enterpriseId(enterprise.getEnterpriseId())
.enterpriseName(enterprise.getName())
.city(enterprise.getCity())
.district(enterprise.getDistrict())
.website(enterprise.getWebsite())
.build();
}
}
18 changes: 18 additions & 0 deletions src/main/java/danpoong/soenter/domain/enterprise/dto/VisitDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package danpoong.soenter.domain.enterprise.dto;

import lombok.Builder;
import lombok.Getter;

public class VisitDTO {
public static class VisitResponse {
@Getter
@Builder
public static class GetVisitedEnterpriseResponse {
private Long enterpriseId;
private String enterpriseName;
private String city;
private String district;
private String website;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package danpoong.soenter.domain.enterprise.repository;

import danpoong.soenter.domain.enterprise.entity.Visit;
import danpoong.soenter.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface VisitRepository extends JpaRepository<Visit, Long> {
List<Visit> findByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package danpoong.soenter.domain.enterprise.service;

import danpoong.soenter.base.ApiResponse;
import danpoong.soenter.domain.enterprise.dto.VisitConverter;
import danpoong.soenter.domain.enterprise.dto.VisitDTO.VisitResponse.GetVisitedEnterpriseResponse;
import danpoong.soenter.domain.enterprise.entity.Enterprise;
import danpoong.soenter.domain.enterprise.entity.Region;
import danpoong.soenter.domain.enterprise.entity.Visit;
import danpoong.soenter.domain.enterprise.repository.EnterpriseRepository;
import danpoong.soenter.domain.enterprise.repository.VisitRepository;
import danpoong.soenter.domain.review.converter.ReviewConverter;
import danpoong.soenter.domain.user.entity.User;
import danpoong.soenter.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@RequiredArgsConstructor
@Transactional
public class EnterpriseService {

private final EnterpriseRepository enterpriseRepository;
private final UserRepository userRepository;
private final VisitRepository visitRepository;

public ApiResponse<List<Enterprise>> getEnterprisesByRegion(Region region) {
try {
Expand All @@ -25,4 +35,15 @@ public ApiResponse<List<Enterprise>> getEnterprisesByRegion(Region region) {
return ApiResponse.onFailure("500", "지역별 기업 정보를 가져오는 데 실패했습니다.", null);
}
}
@Transactional(readOnly = true)
public List<GetVisitedEnterpriseResponse> getVisitedEnterprises(String userId) {
User user = userRepository.findById(Long.valueOf(userId))
.orElseThrow(() -> new RuntimeException("존재하지 않는 사용자입니다."));

return visitRepository.findByUser(user).stream()
.map(Visit::getEnterprise) // Visit에서 Enterprise 추출
.distinct() // 동일한 Enterprise는 한 번만 포함
.map(VisitConverter::toVisitedEnterpriseResponse)
.collect(Collectors.toList());
}
}

0 comments on commit 936052e

Please sign in to comment.