-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from JimTheCat/CU-86977vg76_EPIC---API-v4---Us…
…er-relations_Kinga-Traczyk feature: User relations API
- Loading branch information
Showing
28 changed files
with
872 additions
and
29 deletions.
There are no files selected for viewing
7 changes: 0 additions & 7 deletions
7
backend/src/main/java/meowhub/backend/repositories/RelationTypeRepository.java
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
backend/src/main/java/meowhub/backend/repositories/UserRelationRepository.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/meowhub/backend/shared/exceptions/RelationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package meowhub.backend.shared.exceptions; | ||
|
||
public class RelationException extends RuntimeException { | ||
|
||
public RelationException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
backend/src/main/java/meowhub/backend/user_relations/controllers/UserRelationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package meowhub.backend.user_relations.controllers; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import meowhub.backend.user_relations.services.UserRelationQueryService; | ||
import meowhub.backend.user_relations.services.UserRelationService; | ||
import meowhub.backend.users.dtos.BasicUserInfoDto; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("api/relations") | ||
@RequiredArgsConstructor | ||
public class UserRelationController { | ||
private final UserRelationService service; | ||
private final UserRelationQueryService queryService; | ||
|
||
@GetMapping("friends") | ||
public ResponseEntity<Page<BasicUserInfoDto>> getFriends(@AuthenticationPrincipal UserDetails userDetails, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { | ||
Page<BasicUserInfoDto> friends = queryService.getFriends(userDetails.getUsername(), page, size); | ||
return ResponseEntity.ok(friends); | ||
} | ||
|
||
@GetMapping("{login}/friends") | ||
public ResponseEntity<Page<BasicUserInfoDto>> getFriendsForUser(@PathVariable String login, @AuthenticationPrincipal UserDetails userDetails, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { | ||
Page<BasicUserInfoDto> friends = queryService.getFriendsForUser(login, userDetails.getUsername(), page, size); | ||
return ResponseEntity.ok(friends); | ||
} | ||
|
||
@GetMapping("pending") | ||
public ResponseEntity<Page<BasicUserInfoDto>> getPendingSentRequests(@AuthenticationPrincipal UserDetails userDetails, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { | ||
Page<BasicUserInfoDto> pending = queryService.getPendingSentRequests(userDetails.getUsername(), page, size); | ||
return ResponseEntity.ok(pending); | ||
} | ||
|
||
@GetMapping("received") | ||
public ResponseEntity<Page<BasicUserInfoDto>> getReceivedRequests(@AuthenticationPrincipal UserDetails userDetails, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { | ||
Page<BasicUserInfoDto> received = queryService.getReceivedRequests(userDetails.getUsername(), page, size); | ||
return ResponseEntity.ok(received); | ||
} | ||
|
||
@GetMapping("rejected") | ||
public ResponseEntity<Page<BasicUserInfoDto>> getRejectedRequests(@AuthenticationPrincipal UserDetails userDetails, @RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) { | ||
Page<BasicUserInfoDto> rejected = queryService.getRejectedRequests(userDetails.getUsername(), page, size); | ||
return ResponseEntity.ok(rejected); | ||
} | ||
|
||
@PostMapping("{login}/send") | ||
public ResponseEntity<Void> sendFriendRequest(@PathVariable String login, @AuthenticationPrincipal UserDetails userDetails) { | ||
service.sendFriendRequestTo(login, userDetails.getUsername()); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PostMapping("{login}/reject") | ||
public ResponseEntity<Void> rejectFriendRequest(@PathVariable String login, @AuthenticationPrincipal UserDetails userDetails) { | ||
service.rejectFriendRequestFrom(login, userDetails.getUsername()); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PostMapping("{acceptFrom}/accept") | ||
public ResponseEntity<Void> acceptFriendRequest(@PathVariable String acceptFrom, @AuthenticationPrincipal UserDetails userDetails) { | ||
service.acceptFriendRequestFrom(acceptFrom, userDetails.getUsername()); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@PostMapping("{login}/delete-friend") | ||
public ResponseEntity<Void> deleteFriend(@PathVariable String login, @AuthenticationPrincipal UserDetails userDetails) { | ||
service.deleteFriend(login, userDetails.getUsername()); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...owhub/backend/jpa_buddy/RelationType.java → ...d/user_relations/models/RelationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...owhub/backend/jpa_buddy/UserRelation.java → ...d/user_relations/models/UserRelation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...end/src/main/java/meowhub/backend/user_relations/repositories/RelationTypeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package meowhub.backend.user_relations.repositories; | ||
|
||
import meowhub.backend.user_relations.models.RelationType; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface RelationTypeRepository extends JpaRepository<RelationType, String> { | ||
Optional<RelationType> findByCode(String code); | ||
} |
130 changes: 130 additions & 0 deletions
130
...end/src/main/java/meowhub/backend/user_relations/repositories/UserRelationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package meowhub.backend.user_relations.repositories; | ||
|
||
import meowhub.backend.user_relations.models.UserRelation; | ||
import meowhub.backend.users.dtos.BasicUserInfoDto; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
|
||
public interface UserRelationRepository extends JpaRepository<UserRelation, String> { | ||
@Query(value = """ | ||
SELECT new meowhub.backend.users.dtos.BasicUserInfoDto ( | ||
receiver.id, | ||
receiver.name, | ||
receiver.surname, | ||
receiver.login, | ||
null | ||
) | ||
FROM UserRelation relation | ||
JOIN User sender ON relation.sender.id = sender.id | ||
JOIN RelationType relationType ON relation.relationType.id = relationType.id | ||
JOIN User receiver ON relation.receiver.id = receiver.id | ||
WHERE sender.login = :login | ||
AND relationType.code = :relationTypeCode | ||
""") | ||
Page<BasicUserInfoDto> findRelationsFor(@Param("login") String login, @Param("relationTypeCode") String relationTypeCode, Pageable pageable); | ||
|
||
@Query(value = """ | ||
SELECT new meowhub.backend.users.dtos.BasicUserInfoDto ( | ||
sender.id, | ||
sender.name, | ||
sender.surname, | ||
sender.login, | ||
null | ||
) | ||
FROM UserRelation relation | ||
JOIN User sender ON relation.sender.id = sender.id | ||
JOIN RelationType relationType ON relation.relationType.id = relationType.id | ||
JOIN User receiver ON relation.receiver.id = receiver.id | ||
WHERE receiver.login = :login | ||
AND relationType.code = :relationTypeCode | ||
""") | ||
Page<BasicUserInfoDto> findRelationsWhereReceiverIs(@Param("login") String receiverLogin, @Param("relationTypeCode") String relationTypeCode, Pageable pageable); | ||
|
||
@Query(value = """ | ||
SELECT new meowhub.backend.users.dtos.BasicUserInfoDto ( | ||
sender.id, | ||
sender.name, | ||
sender.surname, | ||
sender.login, | ||
null | ||
) | ||
FROM UserRelation relation | ||
JOIN User sender ON relation.sender.id = sender.id | ||
JOIN User receiver ON relation.receiver.id = receiver.id | ||
JOIN RelationType relationType ON relation.relationType.id = relationType.id | ||
WHERE receiver.login = :login | ||
AND relationType.code = 'FRIENDS' | ||
UNION | ||
SELECT new meowhub.backend.users.dtos.BasicUserInfoDto ( | ||
receiver.id, | ||
receiver.name, | ||
receiver.surname, | ||
receiver.login, | ||
null | ||
) | ||
FROM UserRelation relation | ||
JOIN User sender ON relation.sender.id = sender.id | ||
JOIN User receiver ON relation.receiver.id = receiver.id | ||
JOIN RelationType relationType ON relation.relationType.id = relationType.id | ||
WHERE sender.login = :login | ||
AND relationType.code = 'FRIENDS' | ||
""") | ||
Page<BasicUserInfoDto> findFriendsFor (@Param("login") String login, Pageable pageable); | ||
|
||
|
||
@Modifying | ||
@Query(""" | ||
UPDATE UserRelation relation | ||
SET relation.relationType = (SELECT r FROM RelationType r WHERE r.code = :relationTypeCode), | ||
relation.answerDate = CURRENT_TIMESTAMP | ||
WHERE relation.sender.login = :updateRelationWithLogin AND relation.receiver.login = :currentlyLoggedUserLogin | ||
AND relation.relationType.code = 'SENT_INVITATION' | ||
""") | ||
void updateRelationType(@Param("relationTypeCode") String relationTypeCode, @Param("updateRelationWithLogin") String updateRelationWithLogin, @Param("currentlyLoggedUserLogin") String currentlyLoggedUserLogin); | ||
|
||
|
||
boolean existsUserRelationBySenderLoginAndReceiverLoginAndRelationTypeCode(String senderLogin, String receiverLogin, String relationTypeCode); | ||
|
||
/*** | ||
* Deletes friend relation between two users. The relation is symmetric, so it doesn't matter who is the sender and who is the receiver. | ||
* @param login | ||
* @param requesterLogin | ||
*/ | ||
@Modifying | ||
@Query(""" | ||
DELETE | ||
FROM UserRelation relation | ||
WHERE (relation.sender.login = :requesterLogin | ||
AND relation.receiver.login = :login) | ||
OR (relation.receiver.login = :requesterLogin | ||
AND relation.sender.login = :login) | ||
""") | ||
void deleteFriend(@Param("login") String login, @Param("requesterLogin") String requesterLogin); | ||
|
||
@Query(""" | ||
SELECT | ||
CASE WHEN EXISTS ( | ||
SELECT 1 | ||
FROM User requestedUser | ||
WHERE requestedUser.login = :requestedLogin | ||
AND (requestedUser.friendsPrivacy.code = 'PUBLIC' | ||
OR (requestedUser.friendsPrivacy.code = 'FRIENDS' | ||
AND EXISTS ( | ||
SELECT 1 | ||
FROM UserRelation relation | ||
WHERE relation.relationType.code = 'FRIENDS' | ||
AND (relation.sender.login = :requestedLogin AND relation.receiver.login = :requestedBy | ||
OR | ||
relation.sender.login = :requestedBy AND relation.receiver.login = :requestedLogin) | ||
) | ||
) | ||
) | ||
) THEN true ELSE false END | ||
""") | ||
boolean canViewUserPosts(@Param("requestedLogin") String requestedLogin, @Param("requestedBy") String requestedBy); | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/meowhub/backend/user_relations/services/UserRelationQueryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package meowhub.backend.user_relations.services; | ||
|
||
import meowhub.backend.users.dtos.BasicUserInfoDto; | ||
import org.springframework.data.domain.Page; | ||
|
||
public interface UserRelationQueryService { | ||
Page<BasicUserInfoDto> getFriends(String login, int page, int size); | ||
|
||
Page<BasicUserInfoDto> getFriendsForUser(String login, String requestedBy, int page, int size); | ||
|
||
Page<BasicUserInfoDto> getPendingSentRequests(String login, int page, int size); | ||
|
||
Page<BasicUserInfoDto> getReceivedRequests(String login, int page, int size); | ||
|
||
Page<BasicUserInfoDto> getRejectedRequests(String login, int page, int size); | ||
} |
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/meowhub/backend/user_relations/services/UserRelationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package meowhub.backend.user_relations.services; | ||
|
||
public interface UserRelationService { | ||
void acceptFriendRequestFrom(String login, String accepterLogin); | ||
|
||
void sendFriendRequestTo (String login, String senderLogin); | ||
|
||
void rejectFriendRequestFrom(String login, String rejecterLogin); | ||
|
||
void deleteFriend(String login, String requesterLogin); | ||
} |
Oops, something went wrong.