Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-Server-Refactoring into feat/#45_Memo
  • Loading branch information
Bjimin committed Jan 28, 2024
2 parents 855f4d4 + 9adc785 commit 973c3b3
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ public interface BelongRepository extends JpaRepository<Belong, Long> {
Integer countUserOnPlace(String uuid, String code);

String countUserByPlace(Place place);

@Query("select b from Belong b where b.user.uuid=?1")
List<Belong> getPlaceBelongUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.umc.commonplant.domain.oauth;

public enum OAuthProvider {
KAKAO, APPLE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.umc.commonplant.domain.oauth.controller;

import com.umc.commonplant.domain.oauth.service.OAuthService;
import com.umc.commonplant.global.dto.JsonResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequiredArgsConstructor
@RestController
public class OAuthController {
private final OAuthService oAuthService;

@GetMapping("/login/{provider}")
public ResponseEntity<JsonResponse> login(@RequestParam("accessToken") String accessToken, @PathVariable String provider){
log.info("accessToken : " + accessToken);
String token = oAuthService.oAuthLogin(accessToken, provider);
String email = oAuthService.kakaoLogin(accessToken);
if(token != null){
return ResponseEntity.ok(new JsonResponse(true, 200, "login", token));
}else{
return ResponseEntity.ok(new JsonResponse(true, 2001, "no User Info", email));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.umc.commonplant.domain.user.dto;
package com.umc.commonplant.domain.oauth.dto;

import lombok.Data;

Expand All @@ -11,7 +11,7 @@ public class KakaoProfile {

@Data
public static class Properties{
private String name;
private String nickname;
private String profile_image;
private String thumbnail_image;
}
Expand All @@ -29,7 +29,7 @@ public static class KakaoAccount{

@Data
public static class Profile{
private String name;
private String nickname;
private String profile_image_url;
private boolean is_default_image;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.umc.commonplant.domain.user.service;
package com.umc.commonplant.domain.oauth.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.umc.commonplant.domain.Jwt.JwtService;
import com.umc.commonplant.domain.user.dto.KakaoProfile;
import com.umc.commonplant.domain.oauth.dto.KakaoProfile;
import com.umc.commonplant.domain.user.entity.User;
import com.umc.commonplant.domain.user.repository.UserRepository;
import com.umc.commonplant.global.exception.BadRequestException;
import com.umc.commonplant.global.exception.ErrorResponseStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
Expand All @@ -17,6 +19,9 @@
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;


import static com.umc.commonplant.global.exception.ErrorResponseStatus.*;

@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -29,12 +34,13 @@ public String oAuthLogin(String accessToken, String provider){
String email = "";
email = kakaoLogin(accessToken);

if(userRepository.countUserByEmail(email, provider) > 0){
if(userRepository.countUserByEmail(email, provider) > 0){ // 로그인 성공
User user = userRepository.findByEmail(email, provider);
String token = jwtService.createToken(user.getUuid());
return token;
}else{
throw new IllegalArgumentException(email);
}
else{
return null;
}
}
public String kakaoLogin(String accessToken){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,25 @@ public ResponseEntity<JsonResponse> getMyGarden(){

return ResponseEntity.ok(new JsonResponse(true, 200, "getMyGarden", mainPage));
}

// 사용자가 속한 장소 리스트
@GetMapping("/user")
public ResponseEntity<JsonResponse> getPlaceBelongUser(){
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

List<PlaceDto.getPlaceBelongUser> placeList = placeService.getPlaceBelongUser(user);
return ResponseEntity.ok(new JsonResponse(true, 200, "getPlaceBelongUser",placeList));
}

// 친구 리스트 조회
@GetMapping("/{code}/friends")
public ResponseEntity<JsonResponse> getPlaceFriends(@PathVariable String code){
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

placeService.userOnPlace(user, code);
List<PlaceDto.getPlaceFriends> userList = placeService.getPlaceFriends(code);
return ResponseEntity.ok(new JsonResponse(true, 200, "getPlaceFriends", userList));
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/umc/commonplant/domain/place/dto/PlaceDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

public class PlaceDto {
Expand Down Expand Up @@ -99,4 +100,28 @@ public static class getMainPage{
private List<getPlaceListRes> placeList;
private List<PlantDto.getPlantListRes> plantList;
}

@NoArgsConstructor
@AllArgsConstructor
public static class getPlaceBelongUser {
private String imgUrl;
private String name;
private LocalDateTime createdAt;
}

public static class getPlaceFriends {
private boolean isLeader = false;
private String image;
private String name;

public getPlaceFriends(String image, String name) {
this.image = image;
this.name = name;
}

public void setLeader(boolean leader) {
isLeader = leader;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.umc.commonplant.domain.place.entity;

import com.umc.commonplant.domain.BaseTime;
import com.umc.commonplant.domain.user.entity.User;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -13,7 +14,7 @@
@Table(name = "place")
@NoArgsConstructor
@Entity
public class Place {
public class Place extends BaseTime {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "place_idx")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,33 @@ public Optional<User> getFriends(String inputName) {
return users;
}

public List<PlaceDto.getPlaceBelongUser> getPlaceBelongUser(User user) {
List<Belong> belongs = belongRepository.getPlaceBelongUser(user);
List<PlaceDto.getPlaceBelongUser> belongList = new ArrayList<>();
for(Belong b : belongs){
PlaceDto.getPlaceBelongUser belongUser = new PlaceDto.getPlaceBelongUser(
b.getPlace().getImgUrl(),
b.getPlace().getName(),
b.getPlace().getCreatedAt());
belongList.add(belongUser);
}
return belongList;
}

public List<PlaceDto.getPlaceFriends> getPlaceFriends(String code) {
Place place = placeRepository.getPlaceByCode(code).orElseThrow(() -> new BadRequestException(NOT_FOUND_PLACE_CODE));
List<User> userList = belongRepository.getUserListByPlaceCode(code).orElseThrow(() -> new BadRequestException(NOT_FOUND_PLACE_CODE));

List<PlaceDto.getPlaceFriends> friends = new ArrayList<>();
for(User u : userList){
PlaceDto.getPlaceFriends user = new PlaceDto.getPlaceFriends(u.getImgUrl(), u.getName());
if(place.getOwner().getUuid().equals(u.getUuid()))
user.setLeader(true);
friends.add(user);
}
return friends;
}

// ----- API 외 메서드 -----

public void belongUserOnPlace(User user, String code)
Expand All @@ -143,10 +170,6 @@ public void belongUserOnPlace(User user, String code)
throw new BadRequestException(NOT_FOUND_USER_ON_PLACE);
}

public void IsUserOnPlace(User user, String code){

}

public Place getPlaceByCode(String code){
return placeRepository.getPlaceByCode(code).orElseThrow(() -> new BadRequestException(NOT_FOUND_PLACE_CODE));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public class PlantController {
@PostMapping("/plant/add")
public ResponseEntity<JsonResponse> createPlant(@RequestPart("plant") PlantDto.createPlantReq createPlantReq,
@RequestPart("image") MultipartFile file) {

// log.info("=============CREATE PLANT===============");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -50,9 +47,6 @@ public ResponseEntity<JsonResponse> createPlant(@RequestPart("plant") PlantDto.c
*/
@GetMapping("/plant/{plantIdx}")
public ResponseEntity<JsonResponse> getPlantCard(@PathVariable Long plantIdx) {

// log.info("=============GET PLANT===============");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -68,9 +62,6 @@ public ResponseEntity<JsonResponse> getPlantCard(@PathVariable Long plantIdx) {
*/
@GetMapping("/user/plantList")
public ResponseEntity<JsonResponse> getPlantList() {

// log.info("=============GET PLANT LIST===============");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -86,9 +77,6 @@ public ResponseEntity<JsonResponse> getPlantList() {
// */
// @GetMapping("/place/plantList/{placeCode}")
// public ResponseEntity<JsonResponse> getMyGardenPlantList(@PathVariable String placeCode) {
//
// // log.info("=============GET PLANT LIST===============");
//
// String uuid = jwtService.resolveToken();
// User user = userService.getUser(uuid);
//
Expand All @@ -107,24 +95,20 @@ public ResponseEntity<JsonResponse> getPlantList() {
*/
@PutMapping("/plant/update/wateredDate/{plantIdx}")
public ResponseEntity<JsonResponse> updateWateredDate(@PathVariable Long plantIdx){

// System.out.println("=============UPDATE PLANT WATERED DATE===============");

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

String nickname = plantService.updateWateredDate(plantIdx, user);
String nickname = plantService.updateWateredDate(user, plantIdx);

return ResponseEntity.ok(new JsonResponse(true, 200, "updateWateredDate", nickname));
}

/**
* [GET] /plant/update
* @return 수정할 식물의 애칭
* @return 수정하기 전 식물의 애칭
*/
@GetMapping("/plant/update/{plantIdx}")
public ResponseEntity<JsonResponse> getUpdatedPlant(@PathVariable Long plantIdx){

String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand All @@ -141,15 +125,27 @@ public ResponseEntity<JsonResponse> getUpdatedPlant(@PathVariable Long plantIdx)
public ResponseEntity<JsonResponse> updatePlant(@PathVariable Long plantIdx,
@RequestPart("nickname") String nickname,
@RequestPart("image") MultipartFile file){
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

// log.info("=============UPDATE PLANT===============");
String updatedPlant = plantService.updatePlant(user, plantIdx, nickname, file);

return ResponseEntity.ok(new JsonResponse(true, 200, "updatePlant", updatedPlant));
}

/**
* [DELETE] /plant/delete
* @param plantIdx
* @return 삭제한 식물의 닉네임
*/
@DeleteMapping("/plant/delete/{plantIdx}")
public ResponseEntity<JsonResponse> deletePlant(@PathVariable Long plantIdx){
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

String updatedPlant = plantService.updatePlant(plantIdx, nickname, file);
String deletedPlant = plantService.deletePlant(user, plantIdx);

return ResponseEntity.ok(new JsonResponse(true, 200, "updatePlant", updatedPlant));
return ResponseEntity.ok(new JsonResponse(true, 200, "deletePlant", deletedPlant));
}

}
Loading

0 comments on commit 973c3b3

Please sign in to comment.