Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
Daily Thought App
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
COM3014-Coursework
Daily Thought App
Commits
a989166a
Commit
a989166a
authored
1 year ago
by
Matt Kirby
Browse files
Options
Downloads
Patches
Plain Diff
Added friend datastore
parent
338147f9
No related branches found
No related tags found
1 merge request
!15
Add Friend service
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend-services/friend-service/src/Datastores/FriendDataStore.ts
+61
-0
61 additions, 0 deletions
...services/friend-service/src/Datastores/FriendDataStore.ts
with
61 additions
and
0 deletions
backend-services/friend-service/src/Datastores/FriendDataStore.ts
0 → 100644
+
61
−
0
View file @
a989166a
import
FriendSchema
from
'
../Database/Schemas/FriendSchema
'
;
import
RequestSchema
from
'
../Database/Schemas/RequestSchema
'
;
import
{
Friend
}
from
'
../Types/Friend
'
;
import
{
DataStore
}
from
'
./DataStore
'
;
/**
* Contains actions pertaining to storing and accessing Friends
*/
class
FriendDataStore
extends
DataStore
<
any
>
{
/**
* Create a new friend relation between two users.
* @param u1
* @param u2
* @returns
*/
public
newFriend
=
async
(
u1
:
string
,
u2
:
string
):
Promise
<
Friend
>
=>
{
if
(
await
this
.
Model
.
findOne
({
User1
:
u1
,
User2
:
u2
})
!==
null
||
await
this
.
Model
.
findOne
({
User1
:
u2
,
User2
:
u1
})
!==
null
){
throw
new
Error
(
`
${
u1
}
and
${
u2
}
are already friends!`
);
}
return
await
this
.
Model
.
create
({
User1
:
u1
,
User2
:
u2
});
};
/**
* Store action for getting a users friends
* @param itemCount
* @returns
*/
public
getFriends
=
async
(
userId
:
string
):
Promise
<
{
friends1
:
Friend
[],
friends2
:
Friend
[]}
>
=>
{
const
friends1
=
await
this
.
Model
.
find
({
User1
:
userId
})
const
friends2
=
await
this
.
Model
.
find
({
User2
:
userId
})
return
{
friends1
,
friends2
}
}
/**
* Method to removes a friend relation
* @param user1
* @param user2
*/
public
RemoveFriend
=
async
(
user1
:
string
,
user2
:
string
):
Promise
<
void
>
=>
{
await
this
.
Model
.
findOneAndDelete
({
User1
:
user1
,
User2
:
user2
})
await
this
.
Model
.
findOneAndDelete
({
User1
:
user2
,
User2
:
user1
})
}
/**
* Get the friend item by its id.
* @param id
* @returns
*/
public
GetFriendByUsers
=
async
(
user1
:
string
,
user2
:
string
):
Promise
<
Friend
|
null
>
=>
{
const
result
=
await
this
.
Model
.
findOne
({
User1
:
user1
,
User2
:
user2
})
||
await
this
.
Model
.
findOne
({
User1
:
user2
,
User2
:
user1
});
return
result
;
}
}
export
default
new
FriendDataStore
(
'
Friend
'
,
FriendSchema
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment