From 24cdfab31f0f6cb540567f2aa5462a87abcf7aad Mon Sep 17 00:00:00 2001 From: uhyunglee Date: Tue, 5 Nov 2024 01:46:00 +0900 Subject: [PATCH] [Feat] : create Place And Send Friend Request --- .../friend/controller/FriendController.java | 15 ++++++ .../domain/friend/dto/FriendDto.java | 36 +++++++++++++ .../domain/friend/entity/Friend.java | 22 ++++++++ .../friend/entity/FriendRepository.java | 7 +++ .../domain/friend/service/FriendService.java | 54 +++++++++++++++++++ .../place/controller/PlaceContoller.java | 12 +++++ .../domain/place/controller/PlaceSwagger.java | 9 ++++ .../domain/place/service/PlaceService.java | 16 +++++- .../user/controller/UserController.java | 4 +- .../domain/user/controller/UserSwagger.java | 2 +- 10 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/umc/commonplant/domain/friend/controller/FriendController.java create mode 100644 src/main/java/com/umc/commonplant/domain/friend/dto/FriendDto.java create mode 100644 src/main/java/com/umc/commonplant/domain/friend/entity/FriendRepository.java create mode 100644 src/main/java/com/umc/commonplant/domain/friend/service/FriendService.java diff --git a/src/main/java/com/umc/commonplant/domain/friend/controller/FriendController.java b/src/main/java/com/umc/commonplant/domain/friend/controller/FriendController.java new file mode 100644 index 0000000..6f2ccd0 --- /dev/null +++ b/src/main/java/com/umc/commonplant/domain/friend/controller/FriendController.java @@ -0,0 +1,15 @@ +package com.umc.commonplant.domain.friend.controller; + +import com.umc.commonplant.domain.friend.dto.FriendDto; +import com.umc.commonplant.domain.friend.service.FriendService; +import com.umc.commonplant.global.dto.JsonResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +public class FriendController { +} 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 new file mode 100644 index 0000000..7c1f94b --- /dev/null +++ b/src/main/java/com/umc/commonplant/domain/friend/dto/FriendDto.java @@ -0,0 +1,36 @@ +package com.umc.commonplant.domain.friend.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +public class FriendDto { + @AllArgsConstructor + @NoArgsConstructor + @Data + @Schema(description = "친구 요청 관련 Request") + public static class sendFriendReq{ + @Schema(description = "친구 요청 보내는 사람", example = "커먼1") + private String senderName; + @Schema(description = "친구 요청 받는 사람", example = "[\"커먼2\", \"커먼3\"]") + private List receiverName = new ArrayList<>(); + @Schema(description = "친구 요청 상태", example = "PENDING") + private String status = "PENDING"; // 요청 상태: PENDING, ACCEPTED, REJECTED + } + + @AllArgsConstructor + @NoArgsConstructor + @Getter + @Schema(description = "장소등록 및 친구 요청 관련 Request") + public static class placeCodeAndFriendResponse{ + @Schema(description = "장소 코드", example = "aBcDeF") + private String placeCode; + @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 f7a0b76..075055f 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 @@ -2,8 +2,10 @@ import com.umc.commonplant.domain.BaseTime; import com.umc.commonplant.domain.user.entity.User; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import org.checkerframework.checker.units.qual.C; import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; @@ -21,6 +23,26 @@ public class Friend extends BaseTime { @Column(name = "friend_idx") private Long friendIdx; + @Column(name = "sender") + private String sender; + + @Column(name = "receiver") + private String receiver; + + @Column(name = "placeCode") + private String placeCode; + + @Column(name = "status") + private String status; // WAITING ACCEPTED REJECTED + + @Builder + public Friend(String sender, String receiver, String placeCode, String status){ + this.sender = sender; + this.receiver = receiver; + this.placeCode = placeCode; + this.status = status; // WAITING, ACCEPTED, REJECTED + } + // @ManyToOne(fetch = LAZY) // @JoinColumn(name = "user_idx", nullable = false) // @OnDelete(action = OnDeleteAction.CASCADE) 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 new file mode 100644 index 0000000..70513f2 --- /dev/null +++ b/src/main/java/com/umc/commonplant/domain/friend/entity/FriendRepository.java @@ -0,0 +1,7 @@ +package com.umc.commonplant.domain.friend.entity; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface FriendRepository extends JpaRepository { + +} 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 new file mode 100644 index 0000000..ed886c5 --- /dev/null +++ b/src/main/java/com/umc/commonplant/domain/friend/service/FriendService.java @@ -0,0 +1,54 @@ +package com.umc.commonplant.domain.friend.service; + +import com.umc.commonplant.domain.belong.entity.BelongRepository; +import com.umc.commonplant.domain.friend.entity.Friend; +import com.umc.commonplant.domain.friend.entity.FriendRepository; +import com.umc.commonplant.domain.user.entity.User; +import com.umc.commonplant.domain.user.service.UserService; +import com.umc.commonplant.global.exception.BadRequestException; +import com.umc.commonplant.global.exception.ErrorResponseStatus; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Arrays; +import java.util.List; + +import static com.umc.commonplant.global.exception.ErrorResponseStatus.*; + + + +@RequiredArgsConstructor +@Service +public class FriendService { + private final UserService userService; + private final FriendRepository friendRepository; + private final BelongRepository belongRepository; + + public List sendFriendRequest(String senderName, ListreceiverList, String placeCode){ + User sender = userService.getUserByName(senderName); + + // Exception + // 친구 요청이 수락되었는지 확인 + // TODO: 에러코드로 요청을 이미 보낸 경우 예외처리하기 + /*if(!"PENDING".equals(FriendDto.sendFriendReq.status())){ + }*/ + for(String name : receiverList){ + User receiver = userService.getUserByName(name); // 서비스 사용자인지 확인 + if(belongRepository.countUserOnPlace(receiver.getUuid(), placeCode) > 0) // 장소에 이미 초대 됐는지 확인 + throw new BadRequestException(IS_USER_ON_PLACE); + } + + // 요청 보내기(여러명) + for(String name : receiverList){ + Friend friend = Friend.builder() + .sender(sender.getName()) + .receiver(name) + .placeCode(placeCode) + .status("PENDING") + .build(); + friendRepository.save(friend); + } + // TODO : 요청 보낸 리스트 정렬 + return receiverList; + } +} diff --git a/src/main/java/com/umc/commonplant/domain/place/controller/PlaceContoller.java b/src/main/java/com/umc/commonplant/domain/place/controller/PlaceContoller.java index a8dd4f2..8a4f0a5 100644 --- a/src/main/java/com/umc/commonplant/domain/place/controller/PlaceContoller.java +++ b/src/main/java/com/umc/commonplant/domain/place/controller/PlaceContoller.java @@ -1,6 +1,7 @@ package com.umc.commonplant.domain.place.controller; import com.umc.commonplant.domain.Jwt.JwtService; +import com.umc.commonplant.domain.friend.dto.FriendDto; import com.umc.commonplant.domain.place.dto.PlaceDto; import com.umc.commonplant.domain.place.service.PlaceService; import com.umc.commonplant.domain.plant.dto.PlantDto; @@ -49,6 +50,17 @@ public ResponseEntity createPlace(@RequestPart("place") PlaceDto.c String placeCode = placeService.create(user, req, image); return ResponseEntity.ok(new JsonResponse(true, 200, "createPlace", placeCode)); } + @PostMapping(value = "/friend/request", + consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity createPlaceAndSendFriendRequest(@RequestPart("place") PlaceDto.createPlaceReq placeReq, @RequestPart(value= "image", required = false) MultipartFile image, @RequestPart("friendList")FriendDto.sendFriendReq friendReq){ + //장소 코드 필요 + log.info("[API] registerPlace and Send FriendReq"); + String uuid = jwtService.resolveToken(); + User user = userService.getUser(uuid); + FriendDto.placeCodeAndFriendResponse res = placeService.registerPlace(user, placeReq, image, friendReq); // 중첩 API 호출 + + return ResponseEntity.ok(new JsonResponse(true, 200, "creratePlaceAndSendFriendRequest", res)); + } @GetMapping("/{code}") public ResponseEntity getPlace(@PathVariable("code") String code) 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 9dbdb90..9aeabc3 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 @@ -1,5 +1,6 @@ package com.umc.commonplant.domain.place.controller; +import com.umc.commonplant.domain.friend.dto.FriendDto; import com.umc.commonplant.domain.place.dto.PlaceDto; import com.umc.commonplant.global.dto.JsonResponse; import io.swagger.v3.oas.annotations.Operation; @@ -20,6 +21,14 @@ public ResponseEntity createPlace( @Parameter(description = "장소 대표 이미지") @RequestPart(value = "image", required = false) MultipartFile image ); + // TODO: 스웨거 이미지탭 생성 안됨 + @Operation(summary = "registerPlaceAndSendFriendReq", description = "장소 등록 및 친구 요청보내기") + public ResponseEntity createPlaceAndSendFriendRequest( + @Parameter(description = "장소 생성 요청 정보", required = true) @RequestPart("place") PlaceDto.createPlaceReq placeReq, + @Parameter(description = "장소 대표 이미지") @RequestPart(value = "image", required = false) MultipartFile image, + @Parameter(description = "친구 요청 리스트") @RequestPart("friendList") FriendDto.sendFriendReq friendReq + ); + @Operation(summary = "updatePlace", description = "장소 수정") public ResponseEntity updatePlace( @Parameter(description = "place code", example = "XFGEDS") @PathVariable("code") 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 7f98a8d..d9c1641 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 @@ -2,6 +2,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.service.FriendService; import com.umc.commonplant.domain.image.service.ImageService; import com.umc.commonplant.domain.place.dto.PlaceDto; import com.umc.commonplant.domain.place.entity.Place; @@ -34,6 +36,7 @@ public class PlaceService { private final OpenApiService openApiService; private final ImageService imageService; + private final FriendService friendService; private final PlantRepository plantRepository; @@ -59,7 +62,18 @@ public String create(User user, PlaceDto.createPlaceReq req, MultipartFile image belongRepository.save(belong); return newCode; } - + // NOTE: 중첩 API 호출 + public FriendDto.placeCodeAndFriendResponse registerPlace(User user, PlaceDto.createPlaceReq placeReq, MultipartFile image, FriendDto.sendFriendReq friendReq){ + // 1. 장소 생성 + String placeCode = create(user, placeReq, image); + List receiverList = new ArrayList<>(); + // 2. 장소코드가 존재하면, 친구요청처리 + if(placeCode != null){ + receiverList = friendService.sendFriendRequest(user.getName(), friendReq.getReceiverName(), placeCode); + } + FriendDto.placeCodeAndFriendResponse res = new FriendDto.placeCodeAndFriendResponse(placeCode, receiverList); + return res; + } public void userOnPlace(User user, String code) { if(belongRepository.countUserOnPlace(user.getUuid(), code) < 1) diff --git a/src/main/java/com/umc/commonplant/domain/user/controller/UserController.java b/src/main/java/com/umc/commonplant/domain/user/controller/UserController.java index 36031e2..35da4c9 100644 --- a/src/main/java/com/umc/commonplant/domain/user/controller/UserController.java +++ b/src/main/java/com/umc/commonplant/domain/user/controller/UserController.java @@ -20,7 +20,7 @@ public class UserController implements UserSwagger{ private final JwtService jwtService; @PostMapping(value = "/user", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) //join - public ResponseEntity join(@RequestPart("user") UserDto.join req, @RequestParam(value ="image", required = false)MultipartFile image){ + public ResponseEntity join(@RequestPart("user") UserDto.join req, @RequestPart(value ="image", required = false)MultipartFile image){ log.info("[API] Join User"); String token = userService.joinUser(req, image); @@ -43,7 +43,7 @@ public ResponseEntity checkNameDuplicate(@PathVariable String name @GetMapping("/user/profileImg") public ResponseEntity getUserProfileImg(){ - log.info("[API] Get User Profile Img"); + log.info("[API] Get User Profile Imgw"); String uuid = jwtService.resolveToken(); User user = userService.getUser(uuid); diff --git a/src/main/java/com/umc/commonplant/domain/user/controller/UserSwagger.java b/src/main/java/com/umc/commonplant/domain/user/controller/UserSwagger.java index 8690e3b..ece2a18 100644 --- a/src/main/java/com/umc/commonplant/domain/user/controller/UserSwagger.java +++ b/src/main/java/com/umc/commonplant/domain/user/controller/UserSwagger.java @@ -23,7 +23,7 @@ public ResponseEntity join( @Parameter( name = "image", description = "프로필 이미지" - )@RequestParam(value = "image", required = false)MultipartFile image + )@RequestPart(value = "image", required = false)MultipartFile image ); @Operation(summary = "유저 조회", description = "유저조회 API 호출") @Parameter(name = "name", description="조회할 유저 이름",example = "김커먼", required = true)