-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from Review-zip/dev
[Feat] ์นด์นด์ค ์์ ๋ก๊ทธ์ธ
- Loading branch information
Showing
22 changed files
with
382 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule config
updated
from 80f8e4 to d8e009
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/example/ReviewZIP/domain/follow/FollowsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.example.ReviewZIP.domain.follow; | ||
|
||
import com.example.ReviewZIP.global.response.ApiResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.Parameters; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/v1/follows") | ||
public class FollowsController { | ||
private final FollowsService followsService; | ||
|
||
@PostMapping("/users/{userId}") | ||
@Operation(summary = "์ ์ ํ๋ก์ฐ ํ๊ธฐ API",description = "์ ์ ์ id๋ฅผ ๋ฐ์ ํด๋น ์ ์ ํ๋ก์ฐ") | ||
@ApiResponses({ | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, ์ฑ๊ณต"), | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER404", description = "ํ๋ก์ฐ ํ ์ ์ ๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค",content = @Content(schema = @Schema(implementation = ApiResponse.class))), | ||
}) | ||
@Parameters({ | ||
@Parameter(name = "userId", description = "ํ๋ก์ฐํ ์ ์ ์ ์์ด๋"), | ||
}) | ||
public ApiResponse<Void> follow(@PathVariable(name="userId") Long userId) { | ||
Follows follows = followsService.createFollowing(userId); | ||
|
||
return ApiResponse.onSuccess(null); | ||
} | ||
|
||
@DeleteMapping("/users/{userId}") | ||
@Operation(summary = " ์ ์ ์ธํ๋ก์ฐ ํ๊ธฐ API",description = "์ ์ ์ id๋ฅผ ๋ฐ์ ํด๋น ์ ์ ์ธํ๋ก์ฐ") | ||
@ApiResponses({ | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200",description = "OK, ์ฑ๊ณต"), | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER404", description = "์ธํ๋ก์ฐ ํ ์ ์ ๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค",content = @Content(schema = @Schema(implementation = ApiResponse.class))), | ||
}) | ||
@Parameters({ | ||
@Parameter(name = "userId", description = "ํ๋ก์ฐ ์ทจ์ํ ์ ์ ์ ์์ด๋"), | ||
}) | ||
public ApiResponse<Void> unfollowUser(@PathVariable(name="userId")Long userId){ | ||
followsService.unfollowUser(userId); | ||
|
||
return ApiResponse.onSuccess(null); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/ReviewZIP/domain/follow/FollowsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.ReviewZIP.domain.follow; | ||
|
||
import com.example.ReviewZIP.domain.user.Users; | ||
|
||
public class FollowsConverter { | ||
public static Follows toFollows(Users sender, Users receiver){ | ||
return Follows.builder() | ||
.sender(sender) | ||
.receiver(receiver) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/example/ReviewZIP/domain/follow/FollowsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.example.ReviewZIP.domain.follow; | ||
|
||
|
||
import com.example.ReviewZIP.domain.user.Users; | ||
import com.example.ReviewZIP.domain.user.UsersRepository; | ||
import com.example.ReviewZIP.global.response.code.resultCode.ErrorStatus; | ||
import com.example.ReviewZIP.global.response.exception.handler.UsersHandler; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class FollowsService { | ||
private final FollowsRepository followsRepository; | ||
private final UsersRepository usersRepository; | ||
|
||
|
||
@Transactional | ||
public Follows createFollowing(Long userId) { | ||
Users sender = usersRepository.getById(1L); | ||
Users receiver = usersRepository.findById(userId).orElseThrow(() -> new UsersHandler(ErrorStatus.USER_NOT_FOUND)); | ||
|
||
Follows newFollow = FollowsConverter.toFollows(sender, receiver); | ||
return followsRepository.save(newFollow); | ||
} | ||
|
||
@Transactional | ||
public void unfollowUser(Long userId){ | ||
Users sender = usersRepository.findById(1L).orElseThrow(()->new UsersHandler(ErrorStatus.USER_NOT_FOUND)); | ||
Users receiver = usersRepository.findById(userId).orElseThrow(()->new UsersHandler(ErrorStatus.USER_NOT_FOUND)); | ||
|
||
Follows unfollow = followsRepository.getBySenderAndReceiver(sender, receiver); | ||
|
||
followsRepository.delete(unfollow); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/example/ReviewZIP/domain/jwt/JwtProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.example.ReviewZIP.domain.jwt; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Getter | ||
@Setter | ||
@Component | ||
@ConfigurationProperties("jwt") | ||
public class JwtProperties { | ||
@Value("${jwt.issuer}") | ||
private String issuer; | ||
|
||
@Value("${jwt.secretKey}") | ||
private String secretKey; | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/example/ReviewZIP/domain/jwt/TokenProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.example.ReviewZIP.domain.jwt; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Header; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Date; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class TokenProvider { | ||
private final JwtProperties jwtProperties; | ||
|
||
public String makeToken(Long userId){ | ||
Date now = new Date(); | ||
Date exp = new Date(now.getTime() + 3600000); //1์๊ฐ ๋ง๋ฃ ๊ธฐ์ค | ||
Claims claims = Jwts.claims(); | ||
claims.put("userid", userId); | ||
|
||
return Jwts.builder() | ||
.setHeaderParam(Header.TYPE, Header.JWT_TYPE) | ||
.setIssuer(jwtProperties.getIssuer()) | ||
.setIssuedAt(now) | ||
.setExpiration(exp) | ||
.setClaims(claims) | ||
//.setSubject(userId.toString()) | ||
.signWith(SignatureAlgorithm.HS256, jwtProperties.getSecretKey()) | ||
.compact(); | ||
} | ||
|
||
public Long getUserId(String token){ | ||
Claims claims = Jwts.parser() | ||
.setSigningKey(jwtProperties.getSecretKey()) | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
return claims.get("userId", Long.class); | ||
} | ||
|
||
public boolean validToken(String token){ | ||
try{ | ||
Jwts.parser() | ||
.setSigningKey(jwtProperties.getSecretKey()) | ||
.parseClaimsJws(token); | ||
return true; | ||
} catch(Exception e){ | ||
return false; | ||
} | ||
} | ||
|
||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/example/ReviewZIP/domain/oauth/KakaoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.example.ReviewZIP.domain.oauth; | ||
|
||
import com.example.ReviewZIP.domain.jwt.TokenProvider; | ||
import com.example.ReviewZIP.domain.oauth.dto.request.OauthRequestDto; | ||
import com.example.ReviewZIP.global.response.ApiResponse; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/v1/oauth") | ||
public class KakaoController { | ||
|
||
private final OauthService oauthService; | ||
|
||
// ์ ์ ๊ด๋ จ token ๋ฐ์ ํ ํด๋น ์ ๋ณด๋ก accessToken ๋ฐ๊ธ | ||
@PostMapping("/kakao") | ||
public ApiResponse<Map<String, Object>> sendAccessToken(@RequestBody OauthRequestDto.kakaoTokenRequest request) throws JsonProcessingException { | ||
|
||
List<String> kakaoUserInfoList = oauthService.getKakaoUserInfo(request); | ||
|
||
return ApiResponse.onSuccess(oauthService.generateAccessToken(kakaoUserInfoList.get(0),kakaoUserInfoList.get(1) , kakaoUserInfoList.get(2))); | ||
|
||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
src/main/java/com/example/ReviewZIP/domain/oauth/OauthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.example.ReviewZIP.domain.oauth; | ||
|
||
import com.example.ReviewZIP.domain.jwt.TokenProvider; | ||
import com.example.ReviewZIP.domain.oauth.dto.request.OauthRequestDto; | ||
import com.example.ReviewZIP.domain.user.Users; | ||
import com.example.ReviewZIP.domain.user.UsersRepository; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.*; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class OauthService { | ||
private final UsersRepository usersRepository; | ||
private final TokenProvider tokenProvider; | ||
|
||
@Transactional | ||
public Long createUser(String id, String nickname, String email){ | ||
Users newUser = Users.builder() | ||
.social(id) | ||
.nickname(nickname) | ||
.name(nickname) | ||
.email(email) | ||
.build(); | ||
usersRepository.save(newUser); | ||
|
||
return newUser.getId(); | ||
} | ||
|
||
@Transactional | ||
public Map<String, Object> generateAccessToken(String id, String nickname, String email){ | ||
boolean exist = usersRepository.existsBySocial(id); | ||
Long userId; | ||
|
||
if(!exist) { | ||
// ์ ์ ๊ฐ ์กด์ฌํ์ง ์์๋ -> ์ ์ ์์ฑ | ||
userId = createUser(id, nickname, email); | ||
} else { | ||
// ์กด์ฌํ ๊ฒฝ์ฐ ํด๋น ์ ์ ์ userId ๋ฐํ | ||
userId = usersRepository.getBySocial(id).getId(); | ||
} | ||
|
||
String accessToken = tokenProvider.makeToken(userId); | ||
|
||
Map<String, Object> map = new LinkedHashMap<>(); | ||
map.put("accessToken", accessToken); | ||
|
||
return map; | ||
} | ||
|
||
public List<String> getKakaoUserInfo(OauthRequestDto.kakaoTokenRequest request) throws JsonProcessingException { | ||
String token = request.getToken(); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Content-type", "application/x-www-form-urlencoded;charset=utf-8"); | ||
headers.set("Authorization", "Bearer " + token); | ||
|
||
HttpEntity<MultiValueMap<String, String>> kakaoProfileRequest = new HttpEntity<>(null, headers); | ||
|
||
RestTemplate restTemplate = new RestTemplate(); | ||
|
||
ResponseEntity<String> response = restTemplate.exchange( | ||
"https://kapi.kakao.com/v2/user/me", | ||
HttpMethod.POST, | ||
kakaoProfileRequest, | ||
String.class | ||
); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
String responseBody = response.getBody(); | ||
JsonNode jsonNode = objectMapper.readTree(responseBody); | ||
String id = jsonNode.get("id").asText(); | ||
String nickname = jsonNode.get("properties").get("nickname").asText(); | ||
String email = jsonNode.get("kakao_account").get("email").asText(); | ||
|
||
List<String> kakaoUserInfoList = Arrays.asList(id, nickname, email); | ||
|
||
return kakaoUserInfoList; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/ReviewZIP/domain/oauth/dto/Response/OauthResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.ReviewZIP.domain.oauth.dto.Response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.*; | ||
|
||
|
||
public class OauthResponseDto { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/ReviewZIP/domain/oauth/dto/request/OauthRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.ReviewZIP.domain.oauth.dto.request; | ||
|
||
import lombok.Getter; | ||
|
||
|
||
public class OauthRequestDto { | ||
|
||
@Getter | ||
public static class kakaoTokenRequest{ | ||
String token; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.