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
4
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
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
0f81aa97
Commit
0f81aa97
authored
1 year ago
by
Felipe D'Abrantes
Browse files
Options
Downloads
Patches
Plain Diff
Hide feed content if user has not posted yet (
#31
)
parent
f351ef93
No related branches found
Branches containing commit
No related tags found
1 merge request
!19
Fix feed endpoint issues
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend-services/feed-service/app/controllers/DailyController.scala
+2
-2
2 additions, 2 deletions
...rvices/feed-service/app/controllers/DailyController.scala
backend-services/feed-service/app/models/Daily.scala
+26
-7
26 additions, 7 deletions
backend-services/feed-service/app/models/Daily.scala
with
28 additions
and
9 deletions
backend-services/feed-service/app/controllers/DailyController.scala
+
2
−
2
View file @
0f81aa97
...
...
@@ -66,8 +66,8 @@ class DailyController @Inject()(val controllerComponents: ControllerComponents,
try
{
if
(!
ObjectId
.
isValid
(
userId
))
throw
new
InvalidRequestBodyException
(
"Invalid query parameter ID format: userId"
)
val
result
:
Seq
[
Daily
]
=
Daily
.
getUserFeedAsync
(
new
ObjectId
(
userId
),
new
ObjectId
(
questionId
),
request
.
jwt
)
val
jsonResult
:
JsValue
=
Daily
.
t
oJson
(
result
)
val
(
feed
,
hasPosted
)
:
(
Seq
[
Daily
]
,
Boolean
)
=
Daily
.
getUserFeedAsync
(
new
ObjectId
(
userId
),
new
ObjectId
(
questionId
),
request
.
jwt
)
val
jsonResult
:
JsValue
=
Daily
.
feedT
oJson
(
feed
,
hasPosted
)
Ok
(
jsonResult
)
}
catch
{
case
_:
TimeoutException
=>
BadRequest
(
"Request timed out"
)
...
...
This diff is collapsed.
Click to expand it.
backend-services/feed-service/app/models/Daily.scala
+
26
−
7
View file @
0f81aa97
...
...
@@ -12,7 +12,7 @@ import java.util.Date
import
java.time.Instant
import
java.text.SimpleDateFormat
import
play.api.libs.json.
{
Json
,
JsValue
,
JsString
,
JsObject
,
JsArray
}
import
play.api.libs.json.
{
Json
,
JsValue
,
JsString
,
JsObject
,
JsArray
,
JsBoolean
}
import
org.bson.
{
BsonWriter
,
BsonReader
,
BsonType
}
import
org.bson.codecs.
{
Codec
,
EncoderContext
,
DecoderContext
}
...
...
@@ -54,20 +54,33 @@ object Daily {
Await
.
result
[
Seq
[
Daily
]](
future
,
timeout
.
seconds
)
}
def
getUserFeedAsync
(
userId
:
ObjectId
,
questionId
:
ObjectId
,
jwt
:
String
,
timeout
:
Int
=
4
)
:
Seq
[
Daily
]
=
{
// Sequentially waits for Future objects to complete before calling next method
val
result
:
Future
[
Seq
[
Daily
]]
=
for
{
def
getUserFeedAsync
(
userId
:
ObjectId
,
questionId
:
ObjectId
,
jwt
:
String
,
timeout
:
Int
=
4
)
:
(
Seq
[
Daily
],
Boolean
)
=
{
val
result
=
for
{
friends
:
Seq
[
ObjectId
]
<-
User
.
getUserFriends
(
userId
,
jwt
)
hasPosted
:
Boolean
<-
hasUserPosted
(
userId
,
questionId
)
feed
:
Seq
[
Daily
]
<-
{
val
friendsFilter
:
Bson
=
Filters
.
in
(
"user_id"
,
friends
:
_
*
)
val
questionFilter
:
Bson
=
Filters
.
eq
(
"question_id"
,
questionId
)
val
filters
:
Bson
=
Filters
.
and
(
friendsFilter
,
questionFilter
)
dailyRepo
.
getAll
(
Some
(
filters
),
None
,
None
,
None
)
val
futureFeed
:
Future
[
Seq
[
Daily
]]
=
dailyRepo
.
getAll
(
Some
(
filters
),
None
,
None
,
None
)
if
(!
hasPosted
)
futureFeed
.
map
(
_
.
map
(
_
.
copy
(
content
=
""
)))
else
futureFeed
}
}
yield
feed
}
yield
(
feed
:
Seq
[
Daily
],
hasPosted
:
Boolean
)
Await
.
result
(
result
,
timeout
.
seconds
)
}
Await
.
result
[
Seq
[
Daily
]](
result
,
timeout
.
seconds
)
def
hasUserPosted
(
userId
:
ObjectId
,
questionId
:
ObjectId
)
:
Future
[
Boolean
]
=
{
val
userFilter
:
Bson
=
Filters
.
eq
(
"user_id"
,
userId
)
val
questionFilter
:
Bson
=
Filters
.
eq
(
"question_id"
,
questionId
)
val
filters
:
Bson
=
Filters
.
and
(
userFilter
,
questionFilter
)
dailyRepo
.
getFirst
(
Some
(
filters
),
None
,
None
).
map
(
_
.
isDefined
)
}
def
likeAsync
(
dailyId
:
ObjectId
,
likerId
:
ObjectId
,
jwt
:
String
,
timeout
:
Int
=
4
)
:
Unit
=
{
...
...
@@ -156,6 +169,12 @@ object Daily {
Json
.
toJson
[
JsArray
](
JsArray
(
dailiesJson
))
}
// Convert from feed set to JSON (serializing to JSON)
def
feedToJson
(
dailies
:
Seq
[
Daily
],
userHasPosted
:
Boolean
)
:
JsValue
=
{
val
jsonDailies
:
JsValue
=
toJson
(
dailies
)
Json
.
toJson
[
JsObject
](
JsObject
(
Seq
(
"userHasPosted"
->
JsBoolean
(
userHasPosted
),
"feed"
->
jsonDailies
)))
}
def
toString
(
daily
:
Daily
)
:
String
=
return
s
"Daily(${daily.id.toString()}, ${daily.userId.toString()}, ${daily.questionId.toString()}, ${daily.content}, ${daily.usersLiked.toString()})"
...
...
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