Skip to content

Commit

Permalink
#5 fix: 메인페이지 포스트 조히
Browse files Browse the repository at this point in the history
  • Loading branch information
ShinHyeong committed Feb 19, 2023
1 parent 0a1d8f4 commit 4465556
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 53 deletions.
6 changes: 3 additions & 3 deletions src/main/java/sg/graphServer/controller/GraphController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/sg/graphServer/entity/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -18,8 +20,7 @@ public class Account {
private String accountName;
private String profileImage;

@Relationship(type = "IS_FOLLOWING", direction = Relationship.Direction.OUTGOING)
private Set<SocialRelationship> FollowingList;
@Relationship(type = "IS_BLOCKING", direction = Relationship.Direction.OUTGOING)
private Set<SocialRelationship> BlockingList;
private int followingCount;
private int followerCount;

}
50 changes: 25 additions & 25 deletions src/main/java/sg/graphServer/entity/SocialRelationship.java
Original file line number Diff line number Diff line change
@@ -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;
//
//}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface GraphRepository extends Neo4jRepository<Account, Long> {
@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) ")
Expand Down
43 changes: 23 additions & 20 deletions src/main/java/sg/graphServer/service/GraphService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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:
Expand All @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 4465556

Please sign in to comment.