From 446555615d5dc04cd78356228d8f15340543976f Mon Sep 17 00:00:00 2001 From: ShinHyeong Date: Sun, 19 Feb 2023 10:44:09 +0900 Subject: [PATCH] =?UTF-8?q?#5=20fix:=20=EB=A9=94=EC=9D=B8=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=ED=8F=AC=EC=8A=A4=ED=8A=B8=20=EC=A1=B0?= =?UTF-8?q?=ED=9E=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/GraphController.java | 6 +-- .../java/sg/graphServer/entity/Account.java | 9 ++-- .../entity/SocialRelationship.java | 50 +++++++++---------- .../repository/GraphRepository.java | 2 +- .../sg/graphServer/service/GraphService.java | 43 ++++++++-------- 5 files changed, 57 insertions(+), 53 deletions(-) diff --git a/src/main/java/sg/graphServer/controller/GraphController.java b/src/main/java/sg/graphServer/controller/GraphController.java index 8b303ba..a0cca07 100644 --- a/src/main/java/sg/graphServer/controller/GraphController.java +++ b/src/main/java/sg/graphServer/controller/GraphController.java @@ -60,10 +60,10 @@ public ResponseEntity findFollowing(@PathVariable String accountId, @PathVari } //4. 해당 relation 을 맺고 있는지 확인 : isFollowing, isBlocking - @GetMapping("{accountId1}/is{request}ing/{accountId2}") - public ResponseEntity isRequesting(@PathVariable String accountId1, @PathVariable String request, @PathVariable String accountId2){ + @GetMapping("/{senderId}/is{request}ing/{recipientId}") + public ResponseEntity isRequesting(@PathVariable String senderId, @PathVariable String request, @PathVariable String recipientId){ try { - boolean result = graphService.isRequesting(request, accountId1, accountId2); + boolean result = graphService.isRequesting(request, senderId, recipientId); return ResponseEntity.ok().body(result); }catch(Exception e){ ResponseDTO response = ResponseDTO.builder().error(e.getMessage()).build(); diff --git a/src/main/java/sg/graphServer/entity/Account.java b/src/main/java/sg/graphServer/entity/Account.java index 5784f54..84110cd 100644 --- a/src/main/java/sg/graphServer/entity/Account.java +++ b/src/main/java/sg/graphServer/entity/Account.java @@ -4,6 +4,8 @@ import lombok.Data; import org.springframework.data.neo4j.core.schema.*; +import java.util.ArrayList; +import java.util.List; import java.util.Set; @@ -18,8 +20,7 @@ public class Account { private String accountName; private String profileImage; - @Relationship(type = "IS_FOLLOWING", direction = Relationship.Direction.OUTGOING) - private Set FollowingList; - @Relationship(type = "IS_BLOCKING", direction = Relationship.Direction.OUTGOING) - private Set BlockingList; + private int followingCount; + private int followerCount; + } \ No newline at end of file diff --git a/src/main/java/sg/graphServer/entity/SocialRelationship.java b/src/main/java/sg/graphServer/entity/SocialRelationship.java index 035954f..647edc1 100644 --- a/src/main/java/sg/graphServer/entity/SocialRelationship.java +++ b/src/main/java/sg/graphServer/entity/SocialRelationship.java @@ -1,25 +1,25 @@ -package sg.graphServer.entity; - -import lombok.Builder; -import lombok.NonNull; -import org.springframework.data.annotation.CreatedDate; -import org.springframework.data.neo4j.core.schema.GeneratedValue; -import org.springframework.data.neo4j.core.schema.Id; -import org.springframework.data.neo4j.core.schema.RelationshipProperties; -import org.springframework.data.neo4j.core.schema.TargetNode; - -import java.time.LocalDateTime; - -@Builder -@RelationshipProperties -public class SocialRelationship { - @Id @GeneratedValue - private Long id; - - @TargetNode @NonNull - private Account target; - - @CreatedDate - private LocalDateTime createDT; - -} +//package sg.graphServer.entity; +// +//import lombok.Builder; +//import lombok.NonNull; +//import org.springframework.data.annotation.CreatedDate; +//import org.springframework.data.neo4j.core.schema.GeneratedValue; +//import org.springframework.data.neo4j.core.schema.Id; +//import org.springframework.data.neo4j.core.schema.RelationshipProperties; +//import org.springframework.data.neo4j.core.schema.TargetNode; +// +//import java.time.LocalDateTime; +// +//@Builder +//@RelationshipProperties +//public class SocialRelationship { +// @Id @GeneratedValue +// private Long id; +// +// @TargetNode @NonNull +// private Account target; +// +// @CreatedDate +// private LocalDateTime createDT; +// +//} diff --git a/src/main/java/sg/graphServer/repository/GraphRepository.java b/src/main/java/sg/graphServer/repository/GraphRepository.java index 97cfba3..036c802 100644 --- a/src/main/java/sg/graphServer/repository/GraphRepository.java +++ b/src/main/java/sg/graphServer/repository/GraphRepository.java @@ -13,7 +13,7 @@ public interface GraphRepository extends Neo4jRepository { @Query("MATCH (n:Account{ accountId:$id }) RETURN n") Account findByAccountId(String id); - //팔로우 + //팔로우 관계 추가 @Query("MATCH (n1:Account{accountId:$id1})" + "MATCH (n2:Account{accountId:$id2}) " + "CREATE (n1)-[:IS_FOLLOWING]->(n2) ") diff --git a/src/main/java/sg/graphServer/service/GraphService.java b/src/main/java/sg/graphServer/service/GraphService.java index 53d00ea..9b551c6 100644 --- a/src/main/java/sg/graphServer/service/GraphService.java +++ b/src/main/java/sg/graphServer/service/GraphService.java @@ -5,10 +5,11 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import sg.graphServer.entity.Account; -import sg.graphServer.entity.SocialRelationship; +//import sg.graphServer.entity.SocialRelationship; import sg.graphServer.repository.GraphRepository; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -35,31 +36,27 @@ public void socialRequest(String request, String senderId, String recipientId){ && !graphRepository.isBlocking(savedRecipientId, savedSenderId)){ //(2) 이미 팔로우한 상태인지 확인 if(!graphRepository.isFollowing(savedSenderId, savedRecipientId)){ - graphRepository.follow(savedSenderId, savedRecipientId); - if(savedSender.getFollowingList() == null){ - savedSender.setFollowingList(new HashSet<>()); - } - savedSender.getFollowingList() - .add(SocialRelationship.builder() - .target(savedRecipient) - .createDT(LocalDateTime.now()) - .build()); - } else {throw new RuntimeException("already follow");} + //관련 속성 변경 + savedSender.setFollowingCount(savedSender.getFollowingCount()+1); + savedRecipient.setFollowerCount(savedRecipient.getFollowerCount()+1); + //변경된 속성 db에 반영 + graphRepository.save(savedRecipient); graphRepository.save(savedSender); + //팔로우 + graphRepository.follow(savedSenderId, savedRecipientId); + } else {throw new RuntimeException("already follow");} } else {throw new RuntimeException("already block");} break; case "block": //(1) 이미 block한 상태인지 확인 if(!graphRepository.isBlocking(savedSenderId, savedRecipientId) && !graphRepository.isBlocking(savedRecipientId, savedSenderId)){ + //관련 속성 변경 + savedSender.setFollowingCount(savedSender.getFollowingCount()-1); + savedRecipient.setFollowerCount(savedRecipient.getFollowerCount()-1); + //변경된 속성 db에 반영 + graphRepository.save(savedRecipient); graphRepository.save(savedSender); + //차단 graphRepository.block(savedSenderId, savedRecipientId); - if(savedSender.getBlockingList() == null){ - savedSender.setBlockingList(new HashSet<>()); - } - savedSender.getBlockingList() - .add(SocialRelationship.builder() - .target(savedRecipient) - .createDT(LocalDateTime.now()) - .build()); } else {throw new RuntimeException("already block");} break; default: @@ -70,7 +67,8 @@ public void socialRequest(String request, String senderId, String recipientId){ //2. 소셜 관계 신청 취소 : 팔로우 취소, 차단 취소 @Transactional public void cancelSocialRequest(String request, String senderId, String recipientId){ - + Account savedSender = graphRepository.findByAccountId(senderId); + Account savedRecipient = graphRepository.findByAccountId(recipientId); String savedSenderId = graphRepository.findByAccountId(senderId).getAccountId(); String savedRecipientId = graphRepository.findByAccountId(recipientId).getAccountId(); recipientValidate(savedSenderId, savedRecipientId); @@ -79,6 +77,11 @@ public void cancelSocialRequest(String request, String senderId, String recipien case "follow": //애초에 follow한 상태인지 확인 if(graphRepository.isFollowing(savedSenderId, savedRecipientId)) { + //관련 속성 변경 + savedSender.setFollowingCount(savedSender.getFollowingCount()-1); + savedRecipient.setFollowerCount(savedRecipient.getFollowerCount()-1); + //변경된 속성 db에 반영 + graphRepository.save(savedRecipient); graphRepository.save(savedSender); graphRepository.unfollow(savedSenderId, savedRecipientId); } else {String msg = savedSenderId+" didn't follow "+savedRecipientId; throw new RuntimeException(msg);} break;