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

fix: 카카오 OpenAPI 수정 및 Class 구조 변경 #3

Merged
merged 1 commit into from
Nov 17, 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
@@ -0,0 +1,57 @@
package com.example.server.controller;

import java.util.Objects;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.server.error.CException;
import com.example.server.model.User;
import com.example.server.model.UserPet;
import com.example.server.service.HomeService;
import com.example.server.service.InvalidTokenService;

import lombok.RequiredArgsConstructor;

@RestController // REST API 컨트롤러 선언, @Controller + @ResponseBody 결합
@RequiredArgsConstructor // final 필드의 생성자 자동 생성
@RequestMapping("/api/home")
public class HomeController {
private final InvalidTokenService invalidTokenService;
private final HomeService homeService;

// 홈페이지
// 펫이름 가져오기
// Input : 파라미터 AccessToken
// Output(200) : 홈페이지[이미 가입한 회원]
// Output(401) : 유효하지 않은 토큰
// Output(402) : 리프레시토큰으로 토큰 재발급 필요.
// Output(500) : 서버에러
@GetMapping("")
public ResponseEntity<?> home(@RequestHeader("AccessToken") String AccessToken) {
User user = null;
try {
invalidTokenService.isTokenInvalid(AccessToken);
System.out.println(AccessToken);
user = homeService.getPetInformation(AccessToken);
} catch (CException e) {
return ResponseEntity.status(e.getErrorCode().getStatus()).body(e.getErrorCode().getMessage());
} catch (Exception e) {
return ResponseEntity.status(500).body(e.toString());
}

if(Objects.isNull(user))
return ResponseEntity.status(500).body("유저 정보를 가져오지 못 했습니다.");

System.out.print("User : ");
System.out.println(user.toString());

UserPet userPet = new UserPet(user.getPetName(), user.getPetWeight());

// access 토큰 확인
return ResponseEntity.status(200).body(userPet);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.server.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.server.error.CException;
import com.example.server.service.LoginService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/login")
public class LoginController {
private final LoginService loginService;

// 로그인시 재가입 여부를 판단하는 API
// Input : AccessToken, RefreshToken, ExpiresIn, RefreshTokenExpiresIn
// Output(403) : 이미 가입한 사용자 -> 데이터 Update -> 홈페이지
// Output(401) : 유효하지 않은 토큰
// Output(200) : 재가입 No -> 회원가입 페이지
@GetMapping("")
public ResponseEntity<?> login(
@RequestHeader("AccessToken") String AccessToken,
@RequestHeader("ExpiresIn") Integer ExpiresIn,
@RequestHeader("RefreshToken") String RefreshToken,
@RequestHeader("RefreshTokenExpiresIn") Integer RefreshTokenExpiresIn
){
try {
loginService.isUserRejoin(AccessToken, ExpiresIn, RefreshToken, RefreshTokenExpiresIn);
} catch (CException e) {
return ResponseEntity.status(e.getErrorCode().getStatus()).body(e.getErrorCode().getMessage());
} catch (Exception e) {
return ResponseEntity.status(500).body(e.toString());
}

return ResponseEntity.status(200).body(".");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.server.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.server.error.CException;
import com.example.server.service.RegisterService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/register")
public class RegisterController {
private final RegisterService registerService;

// 회원가입이 끝나면 저장하기 위한 API
// Input : AccessToken, ExpiresI, RefreshToken, RefreshTokenExpiresIn, PetName, PetWeight
// Output : status(200, 401, 500)
@PostMapping("")
public ResponseEntity<?> register(
@RequestHeader("AccessToken") String AccessToken,
@RequestHeader("ExpiresIn") Integer ExpiresIn,
@RequestHeader("RefreshToken") String RefreshToken,
@RequestHeader("RefreshTokenExpiresIn") Integer RefreshTokenExpiresIn,
@RequestHeader("PetName") String PetName,
@RequestHeader("PetWeight") Integer PetWeight
) {
Integer status = 200;
try {
registerService.isUserRejoin(AccessToken, ExpiresIn, RefreshToken, RefreshTokenExpiresIn, PetName, PetWeight);
} catch (CException e) {
return ResponseEntity.status(e.getErrorCode().getStatus()).body(e.getErrorCode().getMessage());
} catch (Exception e) {
return ResponseEntity.status(500).body(e.toString());
}

return ResponseEntity.status(status).body(".");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.server.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.server.error.CException;
import com.example.server.service.InvalidTokenService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/tartar")
public class TartarController {
private final InvalidTokenService invalidTokenService;

// Default :
// 1. Token이 유효한지 확인.

// 사용자가 애완견 이빨을 찍으면
@GetMapping("")
public ResponseEntity<?> tartar(@RequestHeader("AccessToken") String AccessToken) {
try {
invalidTokenService.isTokenInvalid(AccessToken);
} catch (CException e) {
ResponseEntity.status(e.getErrorCode().getStatus()).body(e.getErrorCode().getMessage());
} catch (Exception e) {
ResponseEntity.status(500).body(e.toString());
}

return ResponseEntity.status(200).body("null");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.server.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.server.error.CException;
import com.example.server.service.InvalidTokenService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/tooth")
public class ToothController {
// 양치질이 끝나면 또는 주기적으로 Front에서 데이터를 보내면
// TimeStream에 저장해주는 역할 API.

private final InvalidTokenService invalidTokenService;

// 사용자가 애완견 이빨을 찍으면
@GetMapping("")
public ResponseEntity<?> tooth(@RequestHeader("AccessToken") String AccessToken) {
try {
invalidTokenService.isTokenInvalid(AccessToken);
} catch (CException e) {
ResponseEntity.status(e.getErrorCode().getStatus()).body(e.getErrorCode().getMessage());
} catch (Exception e) {
ResponseEntity.status(500).body(e.toString());
}

return ResponseEntity.status(200).body("null");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.server.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.server.error.CException;
import com.example.server.service.UpdateAccessService;

import lombok.RequiredArgsConstructor;

@RestController // REST API 컨트롤러 선언, @Controller + @ResponseBody 결합
@RequiredArgsConstructor // final 필드의 생성자 자동 생성
@RequestMapping("/api/update")
public class UpdateAccessController {
private UpdateAccessService updateAccessService;

// AccessToken을 새로 발급 받았을때 사용하는 API.
// 서버에 새로운 AccessToken을 제공.
@GetMapping("/accesstoken")
public ResponseEntity<?> updateAccessToken(@RequestHeader("AccessToken") String AccessToken,
@RequestHeader("ExpiresIn") Integer ExpiresIn) {
try {
updateAccessService.updateAccessTokenById(AccessToken, ExpiresIn);
} catch(CException e) {
ResponseEntity.status(e.getErrorCode().getStatus()).body(e.getErrorCode().getMessage());
} catch(Exception e) {
ResponseEntity.status(500).body(e.toString());
}
return ResponseEntity.status(null).body(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.example.server.controller;

import java.util.Objects;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.server.error.CException;
import com.example.server.model.User;
import com.example.server.model.UserPet;
import com.example.server.service.InvalidTokenService;
import com.example.server.service.UserService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/user")
public class UserController {
private InvalidTokenService invalidTokenService;
private UserService userService;

@GetMapping("test")
public ResponseEntity<?> test() {
return ResponseEntity.ok().body(".");
}

// 마이페이지
// 마이페이지를 보여주는 API
// Input : AccessToken
// Output : User
@GetMapping("/getinfo")
public ResponseEntity<?> mypageGetinfo(@RequestHeader("AccessToken") String AccessToken) {
User user = null;
try {
invalidTokenService.isTokenInvalid(AccessToken);
user = userService.getPetInformation(AccessToken);
} catch (CException e) {
ResponseEntity.status(e.getErrorCode().getStatus()).body(e.getErrorCode().getMessage());
} catch (Exception e) {
ResponseEntity.status(500).body(e.toString());
}

if(Objects.isNull(user))
ResponseEntity.status(500).body("유저 정보를 가져오지 못 했습니다.");

UserPet userPet = new UserPet(user.getPetName(), user.getPetWeight());

return ResponseEntity.status(200).body(userPet);
}

// 마이페이지 수정
// 마이페이지를 수정하는 API
// Input : AccessToken, PetName, PetWeight
// Output : status(200, 401, 500)
@PutMapping("/setinfo")
public ResponseEntity<?> mypageGetinfo(
@RequestHeader("AccessToken") String AccessToken,
@RequestHeader("PetName") String PetName,
@RequestHeader("PetWeight") Integer PetWeight
) {
try {
invalidTokenService.isTokenInvalid(AccessToken);
userService.setPetInformation(AccessToken, PetName, PetWeight);
} catch (CException e) {
ResponseEntity.status(e.getErrorCode().getStatus()).body(e.getErrorCode().getMessage());
} catch (Exception e) {
ResponseEntity.status(500).body(e.toString());
}

return ResponseEntity.ok().body(".");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.server.error;

import lombok.Getter;

@Getter
public class CException extends RuntimeException {
private final ErrorCode errorCode;

public CException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}

public CException(ErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
}
Loading
Loading