diff --git a/src/main/java/com/umc/commonplant/domain/friend/dto/FriendDto.java b/src/main/java/com/umc/commonplant/domain/friend/dto/FriendDto.java index 7c1f94b..82a0ca2 100644 --- a/src/main/java/com/umc/commonplant/domain/friend/dto/FriendDto.java +++ b/src/main/java/com/umc/commonplant/domain/friend/dto/FriendDto.java @@ -33,4 +33,5 @@ public static class placeCodeAndFriendResponse{ @Schema(description = "친구요청 받는 사람", example = "[\"커먼2\", \"커먼3\"]") private List receiverList; } + } diff --git a/src/main/java/com/umc/commonplant/domain/friend/entity/Friend.java b/src/main/java/com/umc/commonplant/domain/friend/entity/Friend.java index 075055f..b602714 100644 --- a/src/main/java/com/umc/commonplant/domain/friend/entity/Friend.java +++ b/src/main/java/com/umc/commonplant/domain/friend/entity/Friend.java @@ -42,6 +42,9 @@ public Friend(String sender, String receiver, String placeCode, String status){ this.placeCode = placeCode; this.status = status; // WAITING, ACCEPTED, REJECTED } + public void setStatus(String status){ + this.status = status; + } // @ManyToOne(fetch = LAZY) // @JoinColumn(name = "user_idx", nullable = false) diff --git a/src/main/java/com/umc/commonplant/domain/friend/entity/FriendRepository.java b/src/main/java/com/umc/commonplant/domain/friend/entity/FriendRepository.java index 70513f2..a2f831a 100644 --- a/src/main/java/com/umc/commonplant/domain/friend/entity/FriendRepository.java +++ b/src/main/java/com/umc/commonplant/domain/friend/entity/FriendRepository.java @@ -2,6 +2,8 @@ import org.springframework.data.jpa.repository.JpaRepository; -public interface FriendRepository extends JpaRepository { +import java.util.Optional; +public interface FriendRepository extends JpaRepository { + Optional findBySenderAndReceiverAndStatus(String sender, String receiver, String status); } diff --git a/src/main/java/com/umc/commonplant/domain/friend/service/FriendService.java b/src/main/java/com/umc/commonplant/domain/friend/service/FriendService.java index ed886c5..bbc90a5 100644 --- a/src/main/java/com/umc/commonplant/domain/friend/service/FriendService.java +++ b/src/main/java/com/umc/commonplant/domain/friend/service/FriendService.java @@ -1,6 +1,7 @@ package com.umc.commonplant.domain.friend.service; import com.umc.commonplant.domain.belong.entity.BelongRepository; +import com.umc.commonplant.domain.friend.dto.FriendDto; import com.umc.commonplant.domain.friend.entity.Friend; import com.umc.commonplant.domain.friend.entity.FriendRepository; import com.umc.commonplant.domain.user.entity.User; @@ -9,9 +10,11 @@ import com.umc.commonplant.global.exception.ErrorResponseStatus; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.Arrays; import java.util.List; +import java.util.Optional; import static com.umc.commonplant.global.exception.ErrorResponseStatus.*; diff --git a/src/main/java/com/umc/commonplant/domain/place/controller/PlaceController.java b/src/main/java/com/umc/commonplant/domain/place/controller/PlaceController.java index e421e2c..eff8a91 100644 --- a/src/main/java/com/umc/commonplant/domain/place/controller/PlaceController.java +++ b/src/main/java/com/umc/commonplant/domain/place/controller/PlaceController.java @@ -84,16 +84,16 @@ public ResponseEntity getPlaceWeather(@PathVariable("code") String PlaceDto.getWeatherRes res = weatherService.getPlaceWeather(placeGrid); return ResponseEntity.ok(new JsonResponse(true, 200, "getPlaceWeather", res)); } -// //친구 요청 - @PostMapping("/friends") - public ResponseEntity newFriends(@RequestBody PlaceDto.newFriendsReq req){ - log.info("[API] newFriends"); +// //친구 요청 수락 및 장소 초대 + @PostMapping("/friend/response") + public ResponseEntity acceptFriendAndInvitedPlace(@RequestBody PlaceDto.acceptFriendsReq req){ + log.info("[API] accept Friend And Invited place"); String uuid = jwtService.resolveToken(); User user = userService.getUser(uuid); - String placeCode = placeService.newFriend(req.getName(), req.getCode()); + String placeCode = placeService.acceptFriendRequest(req.getSender(), user.getName()); - return ResponseEntity.ok(new JsonResponse(true, 200, "newFriend", placeCode)); + return ResponseEntity.ok(new JsonResponse(true, 200, "acceptFriend and Invited place", placeCode)); } // //친구 검색 diff --git a/src/main/java/com/umc/commonplant/domain/place/controller/PlaceSwagger.java b/src/main/java/com/umc/commonplant/domain/place/controller/PlaceSwagger.java index 9aeabc3..46c2b04 100644 --- a/src/main/java/com/umc/commonplant/domain/place/controller/PlaceSwagger.java +++ b/src/main/java/com/umc/commonplant/domain/place/controller/PlaceSwagger.java @@ -46,8 +46,8 @@ public ResponseEntity getPlaceWeather( @Parameter(description = "place code", example = "XFGEDS") @PathVariable("code") String code ); - @Operation(summary = "newFriends", description = "장소에 유저 추가") - public ResponseEntity newFriends(@RequestBody PlaceDto.newFriendsReq req); + @Operation(summary = "acceptFriendAndInvitedPlace", description = "친구요청 수락 후 장소초대 완료") + public ResponseEntity acceptFriendAndInvitedPlace(@RequestBody PlaceDto.acceptFriendsReq req); // @Operation(summary = "getFriends", description = "친구 검색") // public ResponseEntity getFriends(@RequestBody PlaceDto.getFriendsReq req); diff --git a/src/main/java/com/umc/commonplant/domain/place/dto/PlaceDto.java b/src/main/java/com/umc/commonplant/domain/place/dto/PlaceDto.java index f9ddd41..aa8e7c1 100644 --- a/src/main/java/com/umc/commonplant/domain/place/dto/PlaceDto.java +++ b/src/main/java/com/umc/commonplant/domain/place/dto/PlaceDto.java @@ -91,9 +91,9 @@ public static class getPlaceGridRes { @NoArgsConstructor @AllArgsConstructor @Data - public static class newFriendsReq{ - @Schema(description = "유저 이름" , example = "user1") - private String name; + public static class acceptFriendsReq{ + @Schema(description = "초대 보낸 사람" , example = "common1") + private String sender; @Schema(description = "장소 코드" , example = "vErDfX") private String code; } diff --git a/src/main/java/com/umc/commonplant/domain/place/service/PlaceService.java b/src/main/java/com/umc/commonplant/domain/place/service/PlaceService.java index be8e727..0b95db7 100644 --- a/src/main/java/com/umc/commonplant/domain/place/service/PlaceService.java +++ b/src/main/java/com/umc/commonplant/domain/place/service/PlaceService.java @@ -3,6 +3,8 @@ import com.umc.commonplant.domain.belong.entity.Belong; import com.umc.commonplant.domain.belong.entity.BelongRepository; import com.umc.commonplant.domain.friend.dto.FriendDto; +import com.umc.commonplant.domain.friend.entity.Friend; +import com.umc.commonplant.domain.friend.entity.FriendRepository; import com.umc.commonplant.domain.friend.service.FriendService; import com.umc.commonplant.domain.image.service.ImageService; import com.umc.commonplant.domain.memo.entity.Memo; @@ -43,6 +45,7 @@ public class PlaceService { private final FriendService friendService; private final PlantRepository plantRepository; private final MemoRepository memoRepository; + private final FriendRepository friendRepository; @Transactional @@ -127,7 +130,7 @@ public PlaceDto.getPlaceGridRes getPlaceGrid(User user, String code) { Place place = placeRepository.getPlaceByCode(code).orElseThrow(() -> new BadRequestException(NOT_FOUND_PLACE_CODE)); return new PlaceDto.getPlaceGridRes(place.getGridX(), place.getGridY()); } - public String newFriend(String name, String code){ + public String addFriendInPlace(String name, String code){ User newUser = userRepository.findByname(name).orElseThrow(() -> new BadRequestException(NOT_FOUND_USER)); Place place = getPlaceByCode(code); @@ -332,4 +335,18 @@ public void changeOwnerOfPlace(User user, String code) { } } + // NOTE: 장소 초대 요청 수락(PENDING -> ACCEPTED) && 장소 초대 완료 + @Transactional + public String acceptFriendRequest(String sender, String receiver) { + Friend friendreq = friendRepository + .findBySenderAndReceiverAndStatus(sender, receiver, "PENDING") + .orElseThrow(() -> new BadRequestException(IS_USER_ON_PLACE)); // NOTE: 이미 장소에 초대된 경우 + friendreq.setStatus("ACCEPTED"); + friendRepository.save(friendreq); + + // receiver 장소코드 -> 장소에 추가 + String placeCode = friendreq.getPlaceCode(); + + return addFriendInPlace(receiver, placeCode); + } }