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

[Feat] : accept friend request And invited place #156

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -33,4 +33,5 @@ public static class placeCodeAndFriendResponse{
@Schema(description = "μΉœκ΅¬μš”μ²­ λ°›λŠ” μ‚¬λžŒ", example = "[\"컀먼2\", \"컀먼3\"]")
private List<String> receiverList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.springframework.data.jpa.repository.JpaRepository;

public interface FriendRepository extends JpaRepository<Friend, Long> {
import java.util.Optional;

public interface FriendRepository extends JpaRepository<Friend, Long> {
Optional<Friend> findBySenderAndReceiverAndStatus(String sender, String receiver, String status);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ public ResponseEntity<JsonResponse> getPlaceWeather(@PathVariable("code") String
PlaceDto.getWeatherRes res = weatherService.getPlaceWeather(placeGrid);
return ResponseEntity.ok(new JsonResponse(true, 200, "getPlaceWeather", res));
}
// //친ꡬ μš”μ²­
@PostMapping("/friends")
public ResponseEntity<JsonResponse> newFriends(@RequestBody PlaceDto.newFriendsReq req){
log.info("[API] newFriends");
// //친ꡬ μš”μ²­ 수락 및 μž₯μ†Œ μ΄ˆλŒ€
@PostMapping("/friend/response")
public ResponseEntity<JsonResponse> 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));
}

// //친ꡬ 검색
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public ResponseEntity<JsonResponse> getPlaceWeather(
@Parameter(description = "place code", example = "XFGEDS") @PathVariable("code") String code
);

@Operation(summary = "newFriends", description = "μž₯μ†Œμ— μœ μ € μΆ”κ°€")
public ResponseEntity<JsonResponse> newFriends(@RequestBody PlaceDto.newFriendsReq req);
@Operation(summary = "acceptFriendAndInvitedPlace", description = "μΉœκ΅¬μš”μ²­ 수락 ν›„ μž₯μ†Œμ΄ˆλŒ€ μ™„λ£Œ")
public ResponseEntity<JsonResponse> acceptFriendAndInvitedPlace(@RequestBody PlaceDto.acceptFriendsReq req);

// @Operation(summary = "getFriends", description = "친ꡬ 검색")
// public ResponseEntity<JsonResponse> getFriends(@RequestBody PlaceDto.getFriendsReq req);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +45,7 @@ public class PlaceService {
private final FriendService friendService;
private final PlantRepository plantRepository;
private final MemoRepository memoRepository;
private final FriendRepository friendRepository;


@Transactional
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
}