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

API #13

Open
wants to merge 44 commits into
base: develop/API
Choose a base branch
from
Open

API #13

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2092f83
FEAT: Connect to FE for Login
knh4437 Oct 22, 2022
224c139
Merge pull request #5 from Travel-planning-site/develop/googleLogin
knh4437 Oct 22, 2022
3474d82
feat: login function
knh4437 Nov 9, 2022
efbca65
Merge pull request #6 from Travel-planning-site/develop/googleLogin
knh4437 Nov 9, 2022
80d1cf6
feat: connect to Mysql
knh4437 Nov 11, 2022
7ea34fc
Merge pull request #7 from Travel-planning-site/develop/googleLogin
knh4437 Nov 11, 2022
7879a1c
fix: Add gitignore with SQL.properties
knh4437 Nov 11, 2022
62c5ffa
refactor: Apply .gitignore
knh4437 Nov 11, 2022
266c12d
test
knh4437 Nov 11, 2022
93a480b
test1
knh4437 Nov 11, 2022
ff43bb4
test11
knh4437 Nov 11, 2022
7db55e2
test2
knh4437 Nov 11, 2022
c91050d
test123
knh4437 Nov 11, 2022
a40883e
Merge branch 'main' into Feature/gitignoretest
knh4437 Nov 11, 2022
463e5e7
Merge pull request #8 from Travel-planning-site/Feature/gitignoretest
knh4437 Nov 11, 2022
757a9e1
fix: gitignore
knh4437 Nov 11, 2022
342160e
fix: gitignore files
knh4437 Nov 11, 2022
5eb3a08
fix: insert given id in table
knh4437 Nov 13, 2022
cfc8ce5
refactor: delect unused files
knh4437 Nov 13, 2022
4ad0065
feat: Connect to Frontend and return UserInfo
knh4437 Nov 13, 2022
c1b99b6
Merge branch 'main' of https://github.com/Travel-planning-site/Backen…
knh4437 Nov 13, 2022
c754632
Merge pull request #9 from Travel-planning-site/develop/jwtToFront
knh4437 Nov 13, 2022
3e5f678
FEAT - KakaoNavi API 완성
dhgkdud12 Nov 14, 2022
0c7fd4e
Merge branch 'main' of https://github.com/Travel-planning-site/Backen…
knh4437 Nov 14, 2022
e35cfe3
fix: login error (maybe)
knh4437 Nov 14, 2022
e3184d1
Merge pull request #10 from Travel-planning-site/develop/jwtToFront
knh4437 Nov 14, 2022
85ed677
refactor: change user table
knh4437 Nov 14, 2022
2081ab9
FEAT - GoogleMap API 추가
dhgkdud12 Nov 15, 2022
5e23ab8
Merge remote-tracking branch 'origin/main'
dhgkdud12 Nov 15, 2022
19cc003
FEAT - BasicInfo DB 저장 API 추가
dhgkdud12 Nov 15, 2022
25e6e7c
FEAT - TravleInfo DB 저장 API 추가
dhgkdud12 Nov 16, 2022
7e85ff9
fix(LoginController): get userInfo
knh4437 Nov 16, 2022
b1fd42b
Merge branch 'main' of https://github.com/Travel-planning-site/Backend
knh4437 Nov 16, 2022
75676ac
FEAT - MyPage 데이터 조회 API 추가
dhgkdud12 Nov 16, 2022
2d5612f
Merge remote-tracking branch 'origin/main'
dhgkdud12 Nov 16, 2022
898da44
FEAT - 계획 삭제 기능 API 추가
dhgkdud12 Nov 16, 2022
6c431ae
refactor: travle typo solved
knh4437 Nov 16, 2022
fa2b24b
refactor: travle to travel
knh4437 Nov 16, 2022
618ef0b
Update SaveController.java
knh4437 Nov 16, 2022
53590dc
Merge pull request #11 from Travel-planning-site/develop/googleLogin
knh4437 Nov 16, 2022
054abfa
fix: add Value Annotation
knh4437 Nov 17, 2022
1a5a3d4
Merge pull request #12 from Travel-planning-site/develop/FixLogin
knh4437 Nov 17, 2022
2478792
feat: make multiple plans
knh4437 Nov 29, 2022
8aca193
Merge pull request #14 from Travel-planning-site/develop/makeMultiplans
knh4437 Nov 29, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# ignore properties file
application.properties
application-API-KEY.properties
application-SQL.properties

# User-specific stuff
.idea/**/workspace.xml
Expand Down
10 changes: 6 additions & 4 deletions .idea/Backend.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.CapStone.Backend.Component;

import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Component
public class AuthorizationExtractor {
public static final String AUTHORIZATION = "Authorization";
public static final String ACCESS_TOKEN_TYPE = AuthorizationExtractor.class.getSimpleName();

public String extract(HttpServletRequest request, String type) {
Enumeration<String> headers = request.getHeaders(AUTHORIZATION);
while (headers.hasMoreElements()) {
String value = headers.nextElement();
if (value.toLowerCase().startsWith(type.toLowerCase())) {
return value.substring(type.length()).trim();
}
}
return Strings.EMPTY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.CapStone.Backend.Component;

import com.CapStone.Backend.Service.LoginBoard.JwtManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class BearerAuthInterceptor implements HandlerInterceptor {

private static final Logger logger = LoggerFactory.getLogger(BearerAuthInterceptor.class);

private AuthorizationExtractor authorizationExtractor;
private JwtManager JwtManager;

public BearerAuthInterceptor(AuthorizationExtractor authorizationExtractor, JwtManager jwtTokenProvider) {
this.authorizationExtractor = authorizationExtractor;
this.JwtManager = jwtTokenProvider;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
logger.info(">>> interceptor.preHandle 호출");
String token = authorizationExtractor.extract(request, "Bearer");
System.out.println("inter token : " + token);
if (token.isEmpty()) {
return true;
}

if (!JwtManager.validateToken(token)) {
throw new IllegalArgumentException("유효하지 않은 토큰");
}

Long id = JwtManager.getSubject(token);
request.setAttribute("userId", id);
return true;
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/CapStone/Backend/Config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.CapStone.Backend.Config;

import com.CapStone.Backend.Component.BearerAuthInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class AppConfig implements WebMvcConfigurer {

private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);

private final BearerAuthInterceptor bearerAuthInterceptor;

public AppConfig(BearerAuthInterceptor bearerAuthInterceptor) { // 인터셉터 구현체를 만들어 등록함
this.bearerAuthInterceptor = bearerAuthInterceptor;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
logger.info(">>> 인터셉터 등록");
registry.addInterceptor(bearerAuthInterceptor).addPathPatterns("/user/userInfo");
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/CapStone/Backend/Config/RestTemplateConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.CapStone.Backend.Config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

@Configuration
public class RestTemplateConfig {

@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder
.requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))
.additionalMessageConverters(new StringHttpMessageConverter(Charset.forName("UTF-8")))
.build();
}
}
12 changes: 9 additions & 3 deletions src/main/java/com/CapStone/Backend/Controller/APIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.CapStone.Backend.Dto.CoordinateRequest;
import com.CapStone.Backend.Dto.NavigationResponse;
import com.CapStone.Backend.Entity.Coordinate;
import com.CapStone.Backend.Service.APIService;
import lombok.RequiredArgsConstructor;
import org.json.simple.parser.ParseException;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -13,6 +13,7 @@
@CrossOrigin
public class APIController {
private final APIService apiService;

// 네이버 검색 Image API
@GetMapping("/searchImg")
public String searchImgApi(String query) {
Expand All @@ -21,10 +22,15 @@ public String searchImgApi(String query) {
}

// 카카오 Navi API
@GetMapping("/KaokaoNavi")
@PostMapping("/KakaoNavi")
public NavigationResponse kakaoNaviApi(@RequestBody CoordinateRequest coordinateRequest) {
return null;
return apiService.getRoutes(coordinateRequest);
}

// 구글 맵 API
@PostMapping("/GoogleMap")
public Object GoogleMapApi(@RequestBody CoordinateRequest coordinateRequest) {
System.out.println(coordinateRequest.toString());
return apiService.getDuration(coordinateRequest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.CapStone.Backend.Controller;

import com.CapStone.Backend.Dto.TokenResponseDto;
import com.CapStone.Backend.Dto.UserResponseDto;
import com.CapStone.Backend.Entity.User;
import com.CapStone.Backend.Service.LoginBoard.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequiredArgsConstructor
@RequestMapping("/user")
@CrossOrigin
public class LoginBoardController {

private String ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth";

@Value("${Google-Client-ID}")
private String CLIENT_ID = "";
@Value("${Google-RedirectToken-URL}")
private String REDIRECT_URI = "";
private String RESPONSE_TYPE = "code";
private String SCOPE = "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
private final UserService userService;

private String token;

@GetMapping("/login")
public String login() {
System.out.println("동작");
return "redirect:" + ENDPOINT + "?client_id=" + CLIENT_ID + "&redirect_uri=" + REDIRECT_URI
+ "&response_type=" + RESPONSE_TYPE + "&scope=" + SCOPE;
}

@GetMapping("/oauth/google/callback")
public String oauthLogin(@RequestParam("code")String code) {
System.out.println("콜백 동작");
String token = userService.oauthLogin(code);
this.token = token;
return "redirect:" + "http://localhost:8080/#/Login";
}

@GetMapping("/token")
public ResponseEntity<TokenResponseDto> getToken() {
System.out.println("getToken() 동작");
return new ResponseEntity<>(new TokenResponseDto(token, "bearer"), HttpStatus.OK);
}

@GetMapping("/userInfo")
public ResponseEntity<UserResponseDto> getUserFromToken(HttpServletRequest request) {
System.out.println("getUserFromToken() 동작");
Long id = (Long) request.getAttribute("userId");
User user = userService.findById((Long) request.getAttribute("userId"));
return new ResponseEntity<>(new UserResponseDto(user.getUserId(), user.getUserName(), user.getUserEmail()), HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.CapStone.Backend.Controller;

import com.CapStone.Backend.Dto.BasicInfoResponse;
import com.CapStone.Backend.Service.BasicInfoService;
import com.CapStone.Backend.Service.TravelInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/myPage")
@CrossOrigin
@RequiredArgsConstructor
public class MyPageController {

private final BasicInfoService basicInfoService;
private final TravelInfoService travelInfoService;

@GetMapping("/plan")
public List<BasicInfoResponse> saveInfo() { // 기본정보와 여행계획 같이 담아서 반환
return basicInfoService.selectPlan();
}

@DeleteMapping("/plan/{idx}")
public String deleteInfo(@PathVariable("idx") int idx) { // 기본정보와 여행계획 같이 담아서 반환
return basicInfoService.deletePlan(idx);
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/CapStone/Backend/Controller/SaveController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.CapStone.Backend.Controller;

import com.CapStone.Backend.Dto.BasicInfoRequest;
import com.CapStone.Backend.Dto.TravelInfoRequest;
import com.CapStone.Backend.Service.BasicInfoService;
import com.CapStone.Backend.Service.TravelInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/save")
@CrossOrigin
@RequiredArgsConstructor
public class SaveController {

private final BasicInfoService basicInfoService;
private final TravelInfoService travelInfoService;

@PostMapping("/basic")
public int saveInfo(@RequestBody BasicInfoRequest request) {
System.out.println(request.toString());
return basicInfoService.saveInfo(request);
}

@PostMapping("/travel")
public void travelPlan(@RequestBody TravelInfoRequest request) {
System.out.println(request.toString());
travelInfoService.saveTravleInfo(request);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/CapStone/Backend/Dto/BasicInfoRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.CapStone.Backend.Dto;

import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public class BasicInfoRequest {
private String title;
private String place;
private String people;
private String period;
private Long userId;
private String memo;
}
17 changes: 17 additions & 0 deletions src/main/java/com/CapStone/Backend/Dto/BasicInfoResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.CapStone.Backend.Dto;

import com.CapStone.Backend.Entity.Info;
import com.CapStone.Backend.Entity.Travel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;

import java.util.List;

@Data
@ToString
@AllArgsConstructor
public class BasicInfoResponse {
Info info;
List<Travel> travels;
}
13 changes: 9 additions & 4 deletions src/main/java/com/CapStone/Backend/Dto/CoordinateRequest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.CapStone.Backend.Dto;

import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
public class CoordinateRequest {
private Integer originX;
private Integer originY;
private Integer destinationX;
private Integer destinationY;
private double originX;
private double originY;
private double destinationX;
private double destinationY;
}
Loading