Skip to content

Commit 758e2a0

Browse files
authored
Merge pull request #32 from CSC207-2022F-UofT/Implement-Friends-System
Implement friends system
2 parents 057e70c + 0fd10d2 commit 758e2a0

21 files changed

+4488
-13
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ fabric.properties
8181
**/build/
8282
!src/**/build/
8383

84-
## Mongo-Related
85-
src/main/resources/application.properties
8684

8785
# Ignore Gradle GUI config
8886
gradle-app.setting
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.backend.controller;
2+
3+
4+
import com.backend.usecases.facades.FriendSystemFacade;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
@RestController
10+
public class FriendController {
11+
12+
private final FriendSystemFacade friendSystemFacade;
13+
14+
@Autowired
15+
public FriendController(FriendSystemFacade friendSystemFacade) {
16+
this.friendSystemFacade = friendSystemFacade;
17+
}
18+
19+
@GetMapping("/friends/getFriends")
20+
public ResponseEntity<Object> getFriends(@RequestParam String sessionID) {
21+
return this.friendSystemFacade.getUserFriends(sessionID);
22+
}
23+
24+
@DeleteMapping("/friends/deleteFriend")
25+
public ResponseEntity<Object> deleteFriend(@RequestParam String friendUserName, @RequestParam String sessionID) {
26+
return this.friendSystemFacade.deleteFriend(friendUserName, sessionID);
27+
}
28+
29+
@DeleteMapping("/friends/deleteAllCorrelatedFriends")
30+
public ResponseEntity<Object> deleteAllCorrelatedFriends(@RequestParam String sessionID) {
31+
return this.friendSystemFacade.deleteAllCorrelatedFriends(sessionID);
32+
}
33+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.backend.controller;
2+
3+
import com.backend.usecases.facades.FriendSystemFacade;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.*;
7+
8+
@RestController
9+
public class InvitationController {
10+
11+
private final FriendSystemFacade friendSystemFacade;
12+
13+
@Autowired
14+
public InvitationController(FriendSystemFacade friendSystemFacade) {
15+
this.friendSystemFacade = friendSystemFacade;
16+
}
17+
18+
// create a get request to get invitations for a specific user
19+
@GetMapping("/friends/getInviteAsReceiver")
20+
public ResponseEntity<Object> getInvitationsAsReceiver(@RequestParam String sessionID) {
21+
// get sessionID from request
22+
// call getInvitations method from InvitationsManager
23+
// return response
24+
return this.friendSystemFacade.getInvitations(sessionID, true);
25+
}
26+
27+
@GetMapping("/friends/getInviteAsSender")
28+
public ResponseEntity<Object> getInvitationsAsSender(@RequestParam String sessionID) {
29+
// get sessionID from request
30+
// call getInvitations method from InvitationsManager
31+
// return response
32+
return this.friendSystemFacade.getInvitations(sessionID, false);
33+
}
34+
35+
// create a post request to send an invitation
36+
@PostMapping("/friends/sendInvite")
37+
public ResponseEntity<Object> sendInvitation(@RequestParam String receiverUsername, String sessionID) {
38+
// create invitation
39+
// save invitation to the database
40+
return this.friendSystemFacade.sendInvitation(receiverUsername, sessionID);
41+
}
42+
43+
@DeleteMapping("/friends/withdrawInvite")
44+
public ResponseEntity<Object> withdrawInvitation(@RequestParam String receiverUsername, String sessionID) {
45+
return this.friendSystemFacade.withdrawInvitation(receiverUsername, sessionID);
46+
}
47+
48+
// create a post request to accept an invitation
49+
@PostMapping("/friends/acceptInvite")
50+
public ResponseEntity<Object> acceptInvitation(@RequestParam String receiverUsername, String sessionID) {
51+
return this.friendSystemFacade.handleInvitation(receiverUsername, sessionID, true);
52+
}
53+
54+
55+
// create a post request to decline an invitation
56+
@DeleteMapping("/friends/declineInvite")
57+
public ResponseEntity<Object> declineInvitation(@RequestParam String receiverUsername, @RequestParam String sessionID) {
58+
// check if invitation exists
59+
// delete invitation from the database
60+
return this.friendSystemFacade.handleInvitation(receiverUsername, sessionID, false);
61+
}
62+
63+
@DeleteMapping("/friends/deleteAllCorrelatedInvitations")
64+
public ResponseEntity<Object> deleteAllCorrelatedInvitations(@RequestParam String sessionID) {
65+
66+
return this.friendSystemFacade.deleteAllCorrelatedInvitations(sessionID);
67+
}
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.backend.entities;
2+
3+
import com.backend.entities.IDs.AccountID;
4+
import org.springframework.data.annotation.Id;
5+
import org.springframework.data.annotation.PersistenceCreator;
6+
import org.springframework.data.annotation.Transient;
7+
import org.springframework.data.mongodb.core.mapping.Document;
8+
9+
import java.util.ArrayList;
10+
11+
/**
12+
* Represents a Friend Entity (accountID, friendsList)
13+
*/
14+
@Document(collection = "FriendsCollection")
15+
public class Friend {
16+
@Transient
17+
private final AccountID accountIDObject;
18+
@Id
19+
private String accountID;
20+
21+
private final ArrayList<String> friends;
22+
23+
// fields, getters, setters
24+
public Friend(AccountID accountIDObject, ArrayList<String> friends) {
25+
this.accountIDObject = accountIDObject;
26+
this.accountID = accountIDObject.getID();
27+
this.friends = friends;
28+
}
29+
30+
@PersistenceCreator
31+
public Friend(String accountID, ArrayList<String> friends) {
32+
this.accountIDObject = new AccountID(accountID);
33+
this.accountID = accountID;
34+
this.friends = friends;
35+
}
36+
37+
/**
38+
* Retrieve the AccountID as String for this given instance
39+
* @return the AccountID as String for this given instance
40+
*/
41+
@SuppressWarnings("unused")
42+
public AccountID getAccountIDObject() {
43+
return this.accountIDObject;
44+
}
45+
46+
/**
47+
* Retrieve the AccountID as String for this given instance
48+
* @return the AccountID as String for this given instance
49+
*/
50+
public String getAccountID() { return this.accountID; }
51+
52+
/**
53+
* Retrieves an array of the user's friends
54+
* @return an array of the user's friends
55+
*/
56+
public ArrayList<String> getFriends() {
57+
return this.friends;
58+
}
59+
60+
}
Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,96 @@
11
package com.backend.entities;
22

33
import com.backend.entities.IDs.AccountID;
4+
import com.fasterxml.jackson.annotation.JsonIgnore;
5+
import org.springframework.data.annotation.Id;
6+
import org.springframework.data.annotation.PersistenceCreator;
7+
import org.springframework.data.annotation.Transient;
8+
import org.springframework.data.mongodb.core.mapping.Document;
49

5-
import java.sql.Timestamp;
10+
import java.util.Date;
611

12+
/**
13+
* Represents an Invitation entity
14+
*/
15+
@Document(collection = "InvitationsCollection")
716
public class Invitation {
817
// The value is generated by Account.java
9-
private final AccountID senderID;
18+
@Transient
19+
@JsonIgnore
20+
private final AccountID senderIDObject;
1021

11-
// The value is generated by Account.java
12-
private final AccountID receiverID;
22+
@Transient
23+
@JsonIgnore
24+
private final AccountID receiverIDObject;
25+
26+
@Id
27+
private final String ID;
1328

14-
private final Timestamp timestamp;
29+
private final String senderID;
30+
private final String receiverID;
31+
private final Date timestamp;
1532

1633
// fields, getters
17-
public Invitation(AccountID senderID, AccountID receiverID, Timestamp timestamp) {
34+
public Invitation(AccountID senderIDObject, AccountID receiverIDObject, Date timestamp) {
35+
this.ID = senderIDObject.getID() + receiverIDObject.getID();
36+
37+
this.senderIDObject = senderIDObject;
38+
this.senderID = senderIDObject.getID();
39+
40+
this.receiverIDObject = receiverIDObject;
41+
this.receiverID = receiverIDObject.getID();
42+
43+
this.timestamp = timestamp;
44+
}
45+
@PersistenceCreator
46+
public Invitation(String ID, String senderID, String receiverID, Date timestamp) {
47+
this.ID = ID;
48+
this.senderIDObject = new AccountID(senderID);
49+
this.receiverIDObject = new AccountID(receiverID);
1850
this.senderID = senderID;
1951
this.receiverID = receiverID;
2052
this.timestamp = timestamp;
2153
}
2254

23-
public AccountID getSenderID() {
55+
// Getters
56+
57+
/**
58+
* Retrieve the AccountID String as the sender
59+
* @return the AccountID String as the sender
60+
*/
61+
public String getSenderID() {
2462
return this.senderID;
2563
}
2664

27-
public AccountID getReceiverID() {
65+
/**
66+
* Retrieve the AccountID String as the receiver
67+
* @return the AccountID String as the receiver
68+
*/
69+
public String getReceiverID() {
2870
return this.receiverID;
2971
}
3072

31-
public Timestamp getTimestamp() {
73+
/**
74+
* Retrieve the AccountID Object as the sender
75+
* @return the AccountID Object as the sender
76+
*/
77+
public AccountID getSenderIDObject() {
78+
return this.senderIDObject;
79+
}
80+
81+
/**
82+
* Retrieve the Account Object as the retriever
83+
* @return the Account Object as the retriever
84+
*/
85+
public AccountID getReceiverIDObject() {
86+
return this.receiverIDObject;
87+
}
88+
89+
/**
90+
* Retrieve the TimeStamp as java.util.Date
91+
* @return the TimeStamp as java.util.Date
92+
*/
93+
public Date getTimestamp() {
3294
return this.timestamp;
3395
}
3496
}

src/main/java/com/backend/error/handlers/LogHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public void logError(Exception e) {
9696
public ResponseEntity<Object> logError(Exception exception, HttpStatus status) {
9797
this.logError(exception);
9898

99-
10099
JSONObject jsonObject = new JSONObject();
101100

102101
jsonObject.put("Error", exception.getClass().getName());
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.backend.repositories;
2+
3+
import com.backend.entities.Friend;
4+
import org.springframework.data.mongodb.repository.MongoRepository;
5+
import org.springframework.data.mongodb.repository.Query;
6+
import java.util.List;
7+
8+
/**
9+
* Mongo Repository for custom queries on the Friends Collection
10+
*/
11+
public interface FriendsRepo extends MongoRepository<Friend, String> {
12+
/**
13+
* Find all associated Friends instance containing userID in friends list
14+
* @param userID of type String, accountID to reference the desired Account
15+
* @return the list of accounts that have the desired accountID in their friends list
16+
*/
17+
@Query("{'friends': {'$in': [?0]}}")
18+
List<Friend> findAllContainingUserID(String userID);
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.backend.repositories;
2+
import com.backend.entities.Invitation;
3+
import org.springframework.data.mongodb.repository.MongoRepository;
4+
import org.springframework.data.mongodb.repository.Query;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Mongo Repository for custom queries on the Invitations Collection
10+
*/
11+
public interface InvitationsRepo extends MongoRepository<Invitation, String> {
12+
/**
13+
* Find the specific Invitation with the given parameter (by senderID and receiverID)
14+
* @param senderID of type String, accountID to reference the user as sender
15+
* @param receiverID of type String, accountID to reference the user as receiver
16+
* @return the retrieved Invitation object given valid parameters and exists (otherwise null)
17+
*/
18+
@Query("{ 'senderID' : ?0, 'receiverID' : ?1 }")
19+
Invitation findBySenderIDAndReceiverID(String senderID, String receiverID);
20+
21+
/**
22+
* Find a list of associated Invitation with the given parameter(by receiverID)
23+
* @param receiverID of type String, accountID to reference the user as the receiver
24+
* @return the retrieved list of Invitation Objects given valid parameters and exists (otherwise null)
25+
*/
26+
@Query("{ 'receiverID' : ?0 }")
27+
List<Invitation> findAllByReceiverID(String receiverID);
28+
29+
/**
30+
* Find a list of associated Invitation with the given parameter(by senderID)
31+
* @param senderID of type String, accountID to reference the user as the senderID
32+
* @return the retrieved list of Invitation Objects given valid parameters and exists (otherwise null)
33+
*/
34+
@Query("{ 'senderID' : ?0 }")
35+
List<Invitation> findAllBySenderID(String senderID);
36+
}

0 commit comments

Comments
 (0)