package com.alterdekim.game.service; import com.alterdekim.game.entity.Relationship; import com.alterdekim.game.entity.User; import com.alterdekim.game.repository.RelationshipRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service public class RelationshipService { @Autowired private RelationshipRepository repository; public void save(Relationship relationship) { this.repository.save(relationship); } public Boolean isFriends(Long userId, Long userId1) { return this.repository.findRelation(userId, userId1, Relationship.FriendshipStatus.Friendship).isPresent(); } public Optional isRequestSent(Long userId, Long userId1) { return this.repository.findRelation(userId, userId1, Relationship.FriendshipStatus.Request); } public void deleteRequest(Long id) { this.repository.deleteById(id); } public void acceptRequest(Long id) { this.repository.acceptRequestById(id); } public void sendRequest(Long sender, Long receiver) { if( this.isFriends(sender, receiver) ) return; if( this.isRequestSent(sender, receiver).isPresent() ) { Relationship r = this.isRequestSent(sender, receiver).get(); if( r.getRecepientId().longValue() != sender.longValue() ) return; this.acceptRequest(r.getId()); return; } this.repository.save(new Relationship(null, sender, receiver, Relationship.FriendshipStatus.Request)); } public void deleteFriendshipByUserIds(Long userId1, Long userId2) { this.repository.deleteFriendshipByUserIds(userId1, userId2); } public List getFriendsOfUser(Long userId) { return this.repository.findFriendsOfUserById(userId); } public List getRequestsOfUser(Long userId) { return this.repository.findRequestsOfUserById(userId); } public void acceptFriendshipWithUsers(Long userId, List users) { this.repository.acceptFriendshipWithUsers(userId, users); } public void denyFriendshipsWithUsers(Long userId, List users) { this.repository.denyFriendshipsWithUsers(userId, users); } }