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] : create Place And Send Friend Request #153

Closed
wants to merge 1 commit into from
Closed
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
@@ -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 {
}
36 changes: 36 additions & 0 deletions src/main/java/com/umc/commonplant/domain/friend/dto/FriendDto.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> receiverList;
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/umc/commonplant/domain/friend/entity/Friend.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.umc.commonplant.domain.friend.entity;

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

public interface FriendRepository extends JpaRepository<Friend, Long> {

}
Original file line number Diff line number Diff line change
@@ -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<String> sendFriendRequest(String senderName, List<String>receiverList, 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -49,6 +50,17 @@ public ResponseEntity<JsonResponse> 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<JsonResponse> 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<JsonResponse> getPlace(@PathVariable("code") String code)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,6 +21,14 @@ public ResponseEntity<JsonResponse> createPlace(
@Parameter(description = "์žฅ์†Œ ๋Œ€ํ‘œ ์ด๋ฏธ์ง€") @RequestPart(value = "image", required = false) MultipartFile image
);

// TODO: ์Šค์›จ๊ฑฐ ์ด๋ฏธ์ง€ํƒญ ์ƒ์„ฑ ์•ˆ๋จ
@Operation(summary = "registerPlaceAndSendFriendReq", description = "์žฅ์†Œ ๋“ฑ๋ก ๋ฐ ์นœ๊ตฌ ์š”์ฒญ๋ณด๋‚ด๊ธฐ")
public ResponseEntity<JsonResponse> 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<JsonResponse> updatePlace(
@Parameter(description = "place code", example = "XFGEDS") @PathVariable("code") String code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,6 +36,7 @@ public class PlaceService {

private final OpenApiService openApiService;
private final ImageService imageService;
private final FriendService friendService;
private final PlantRepository plantRepository;


Expand All @@ -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<String> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonResponse> join(@RequestPart("user") UserDto.join req, @RequestParam(value ="image", required = false)MultipartFile image){
public ResponseEntity<JsonResponse> join(@RequestPart("user") UserDto.join req, @RequestPart(value ="image", required = false)MultipartFile image){
log.info("[API] Join User");
String token = userService.joinUser(req, image);

Expand All @@ -43,7 +43,7 @@ public ResponseEntity<JsonResponse> checkNameDuplicate(@PathVariable String name

@GetMapping("/user/profileImg")
public ResponseEntity<JsonResponse> getUserProfileImg(){
log.info("[API] Get User Profile Img");
log.info("[API] Get User Profile Imgw");
String uuid = jwtService.resolveToken();
User user = userService.getUser(uuid);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ResponseEntity<JsonResponse> 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)
Expand Down