Skip to content

Commit

Permalink
[Update] 인공지능 서버와 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
alswp006 committed Sep 10, 2024
1 parent 3d547bb commit 9b12e8a
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 118 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/example/bami/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

Expand All @@ -22,9 +23,10 @@ public SecurityConfig(JwtTokenProvider tokenProvider) {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf(csrf -> csrf.disable()) // CSRF 비활성화
http.csrf(AbstractHttpConfigurer::disable) // CSRF 비활성화
.authorizeHttpRequests(authz -> authz
.requestMatchers(HttpMethod.GET, "/api/config").permitAll()
.requestMatchers("/ai/**").permitAll()
.requestMatchers("/api/**").permitAll()
.requestMatchers("/").permitAll()
// Swagger 관련 경로들에 대해 접근을 허용합니다.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/bami/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 모든 API 경로에 대해 CORS 설정
.allowedOrigins("http://" + endPoint + ":8080", "http://" + endPoint + ":3000")
.allowedOrigins("http://" + endPoint + ":8080", "http://" + endPoint + ":3000", "http://" + endPoint + ":8000")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.example.bami.short_travel.controller;

import com.example.bami.short_travel.dto.*;
import com.example.bami.short_travel.service.AIRecommendationService;
import com.example.bami.short_travel.service.TravelPlanService;
import com.example.bami.user.domain.BamiUser;
import com.example.bami.user.repository.UserRepository;
import com.example.bami.user.security.JwtTokenProvider;
import com.example.bami.weather.service.ReverseGeocodingService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
Expand All @@ -22,29 +21,14 @@
@Slf4j
public class ShortTravelController {


// public ShortTravelController(AIRecommendationService aiRecommendationService) {
// this.aiRecommendationService = aiRecommendationService;
// }
//
// @PostMapping("/submit")
// public Mono<List<RecommendationDTO>> submitTravelPlan(@RequestBody ShortTravelDTO shortTravelDTO) {
// log.info("Received travel plan: {}", shortTravelDTO);
// return aiRecommendationService.getRecommendations(shortTravelDTO);
// }

// private final AIRecommendationService aiRecommendationService;

private final TravelPlanService travelPlanService;
private final JwtTokenProvider jwtTokenProvider;
private final UserRepository userRepository;
private final AIRecommendationService aiRecommendationService;


@PostMapping("/save")
public ResponseEntity<String> saveTravelPlan(@RequestBody SaveShortTravelDTO saveShortTravelDTO, HttpServletRequest request) {
log.info(saveShortTravelDTO.getStartDate());
log.info(saveShortTravelDTO.getEndDate());

String token = jwtTokenProvider.resolveToken(request);
if (token == null || !jwtTokenProvider.validateToken(token)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("유효하지 않은 토큰입니다.");
Expand All @@ -70,43 +54,11 @@ public ResponseEntity<String> saveTravelPlan(@RequestBody SaveShortTravelDTO sav
}
}

@PostMapping("/submit")
@PostMapping(value = "/submit", consumes = "application/json")
public List<RecommendationDTO> submitTravelPlan(@RequestBody ShortTravelDTO shortTravelDTO) {
log.info("Received travel plan: {}", shortTravelDTO);

List<RecommendationDTO> mockRecommendations = createMockRecommendations();

return mockRecommendations;
return aiRecommendationService.getRecommendations(shortTravelDTO);
}

private List<RecommendationDTO> createMockRecommendations() {
List<RecommendationDTO> recommendations = new ArrayList<>();

RecommendationDTO day1 = new RecommendationDTO();
day1.setDay("1일차");
day1.setPlaces(List.of(
new PlaceDTO("해운대 해수욕장", "부산광역시 해운대구", "부산광역시 해운대구 중동 1015", 35.158698, 129.160384),
new PlaceDTO("부산 타워", "부산광역시 중구", "부산광역시 중구 광복동2가 1-2", 35.101975, 129.032213),
new PlaceDTO("해운대 해수욕장", "부산광역시 해운대구", "부산광역시 해운대구 중동 1015", 35.158698, 129.160384),
new PlaceDTO("부산 타워", "부산광역시 중구", "부산광역시 중구 광복동2가 1-2", 35.101975, 129.032213),
new PlaceDTO("해운대 해수욕장", "부산광역시 해운대구", "부산광역시 해운대구 중동 1015", 35.158698, 129.160384),
new PlaceDTO("부산 타워", "부산광역시 중구", "부산광역시 중구 광복동2가 1-2", 35.101975, 129.032213)
));

RecommendationDTO day2 = new RecommendationDTO();
day2.setDay("2일차");
day2.setPlaces(List.of(
new PlaceDTO("감천문화마을", "부산광역시 사하구", "부산광역시 사하구 감천동 10-13", 35.097317, 128.987374),
new PlaceDTO("광안리 해수욕장", "부산광역시 수영구", "부산광역시 수영구 광안동 192-20", 35.153087, 129.118556),
new PlaceDTO("해운대 해수욕장", "부산광역시 해운대구", "부산광역시 해운대구 중동 1015", 35.158698, 129.160384),
new PlaceDTO("부산 타워", "부산광역시 중구", "부산광역시 중구 광복동2가 1-2", 35.101975, 129.032213),
new PlaceDTO("해운대 해수욕장", "부산광역시 해운대구", "부산광역시 해운대구 중동 1015", 35.158698, 129.160384),
new PlaceDTO("부산 타워", "부산광역시 중구", "부산광역시 중구 광복동2가 1-2", 35.101975, 129.032213)
));

recommendations.add(day1);
recommendations.add(day2);

return recommendations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@

@Data
public class AiShortTravelDTO {
private double LATITUDE; // 위도
private double LONGITUDE; // 경도
private int DAY;
private String MVMN_NM; // "자가용", "대중교통 등"
private String GENDER; //"남성", "여성"
private int AGE_GRP; // 20대면 20, 30대면 30 등등
private int TRAVEL_STYL_1;
private int TRAVEL_STYL_2;
private int TRAVEL_STYL_3;
private int TRAVEL_STYL_5;
private int TRAVEL_STYL_6;
private int TRAVEL_STYL_7;
private int TRAVEL_STYL_8;
private int TRAVEL_MOTIVE_1; // 여행 동기
private String REL_CD_Categorized; // 동행인 정보
private float latitude; // 위도
private float longitude; // 경도
private String mvmn_nm; // "자가용", "대중교통 등"
private String gender; //"남성", "여성"
private int age_grp; // 20대면 20, 30대면 30 등등
private int day;
private int travel_styl_1;
private int travel_styl_2;
private int travel_styl_3;
private int travel_styl_5;
private int travel_styl_6;
private int travel_styl_7;
private int travel_styl_8;
private int travel_motive_1; // 여행 동기
private String rel_cd_categorized; // 동행인 정보


public static AiShortTravelDTO toAiShortTravelDTO(ShortTravelDTO shortTravelDTO){
AiShortTravelDTO aiShortTravelDTO = new AiShortTravelDTO();
Expand All @@ -37,32 +38,31 @@ public static AiShortTravelDTO toAiShortTravelDTO(ShortTravelDTO shortTravelDTO)
"기타"
);

aiShortTravelDTO.LATITUDE = shortTravelDTO.getLocation().getLatitude();
aiShortTravelDTO.LONGITUDE = shortTravelDTO.getLocation().getLongitude();
aiShortTravelDTO.DAY = shortTravelDTO.getDay_duration();
aiShortTravelDTO.latitude = shortTravelDTO.getLocation().getLatitude();
aiShortTravelDTO.longitude = shortTravelDTO.getLocation().getLongitude();
aiShortTravelDTO.day = shortTravelDTO.getDay_duration();
if (shortTravelDTO.getTransport().equals("차량 이용")){
aiShortTravelDTO.MVMN_NM = "자가용";
aiShortTravelDTO.mvmn_nm = "자가용";
} else{
aiShortTravelDTO.MVMN_NM = "대중교통 등";
aiShortTravelDTO.mvmn_nm = "대중교통 등";
}
aiShortTravelDTO.GENDER = shortTravelDTO.getGender().replace("성", "");
aiShortTravelDTO.AGE_GRP = Integer.parseInt(shortTravelDTO.getAgeGroup().replace("대", "").replace(" 이상", "").replace(" 미만", ""));
aiShortTravelDTO.TRAVEL_STYL_1 = Integer.parseInt(shortTravelDTO.getPreferences().getNature());
aiShortTravelDTO.TRAVEL_STYL_2 = Integer.parseInt(shortTravelDTO.getPreferences().getDuration());
aiShortTravelDTO.TRAVEL_STYL_3 = Integer.parseInt(shortTravelDTO.getPreferences().getNewPlaces());
aiShortTravelDTO.TRAVEL_STYL_5 = Integer.parseInt(shortTravelDTO.getPreferences().getRelaxation());
aiShortTravelDTO.TRAVEL_STYL_6 = Integer.parseInt(shortTravelDTO.getPreferences().getExploration());
aiShortTravelDTO.TRAVEL_STYL_7 = Integer.parseInt(shortTravelDTO.getPreferences().getPlanning());
aiShortTravelDTO.TRAVEL_STYL_8 = Integer.parseInt(shortTravelDTO.getPreferences().getPhotography());
aiShortTravelDTO.gender = shortTravelDTO.getGender();
aiShortTravelDTO.age_grp = Integer.parseInt(shortTravelDTO.getAgeGroup().replace("대", "").replace(" 이상", "").replace(" 미만", ""));
aiShortTravelDTO.travel_styl_1 = Integer.parseInt(shortTravelDTO.getPreferences().getNature());
aiShortTravelDTO.travel_styl_2 = Integer.parseInt(shortTravelDTO.getPreferences().getDuration());
aiShortTravelDTO.travel_styl_3 = Integer.parseInt(shortTravelDTO.getPreferences().getNewPlaces());
aiShortTravelDTO.travel_styl_5 = Integer.parseInt(shortTravelDTO.getPreferences().getRelaxation());
aiShortTravelDTO.travel_styl_6 = Integer.parseInt(shortTravelDTO.getPreferences().getExploration());
aiShortTravelDTO.travel_styl_7 = Integer.parseInt(shortTravelDTO.getPreferences().getPlanning());
aiShortTravelDTO.travel_styl_8 = Integer.parseInt(shortTravelDTO.getPreferences().getPhotography());
for (int i = 0; i < travelMotivations.size(); i++){
if (travelMotivations.get(i).equals(shortTravelDTO.getTravelPurpose())){
aiShortTravelDTO.TRAVEL_MOTIVE_1 = i + 1;
aiShortTravelDTO.travel_motive_1 = i + 1;
break;
}
}
aiShortTravelDTO.REL_CD_Categorized = shortTravelDTO.getCompanion().replace("와", "").replace("과", "");
aiShortTravelDTO.rel_cd_categorized = shortTravelDTO.getCompanion().replace("와", "").replace("과", "");

System.out.println(aiShortTravelDTO);
return aiShortTravelDTO;
}
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/example/bami/short_travel/dto/PlaceDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
@AllArgsConstructor
public class PlaceDTO {
private String name; // 장소 이름
private String roadAddress; // 도로명 주소
private String lotnoAddress; // 지번 주소
private double latitude; // 위도
private double longitude; // 경도
private String city; // 도로명 주소
private String address; // 지번 주소
private float longitude; // 경도
private float latitude; // 위도
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package com.example.bami.short_travel.dto;

import lombok.Data;
import java.util.List;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor

public class RecommendationDTO {
// "1일차", "2일차"
private String day;
// 해당 날짜의 추천 장소 리스트
private List<PlaceDTO> places;

public RecommendationDTO(String day){
this.day = day;
this.places = new ArrayList<>();
}

public void addPlaces(PlaceDTO placeDTO){
places.add(placeDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class SaveShortTravelDTO {
private List<RecommendationDTO> recommendations;
private String startDate;
private String endDate;
private Double latitude;
private Double longitude;
private float latitude;
private float longitude;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static class Preferences {

@Data
public static class Location {
private double latitude;
private double longitude;
private float latitude;
private float longitude;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class PlaceEntity {

private String roadAddress;
private String lotnoAddress;
private Double latitude;
private Double longitude;
private float latitude;
private float longitude;

@ManyToOne(fetch = FetchType.LAZY)
private RecommendationEntity recommendation;

public PlaceEntity(String name, String roadAddress, String lotnoAddress, double latitude, double longitude) {
public PlaceEntity(String name, String roadAddress, String lotnoAddress, float latitude, float longitude) {
this.name = name;
this.roadAddress = roadAddress;
this.lotnoAddress = lotnoAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public class TravelPlanEntity {
private String endDate; // 여행 종료 날짜

@Column(nullable = false)
private Double latitude;
private float latitude;

@Column(nullable = false)
private Double longitude;
private float longitude;

private String address;

Expand All @@ -50,7 +50,7 @@ public void setDate(String startDate, String endDate){
this.endDate = endDate;
}

public void setLocation(Double latitude, Double longitude, String address) {
public void setLocation(float latitude, float longitude, String address) {
this.latitude = latitude;
this.longitude = longitude;
this.address = address;
Expand Down
Loading

0 comments on commit 9b12e8a

Please sign in to comment.