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
7338eb28
Commit
7338eb28
authored
1 year ago
by
Felipe D'Abrantes
Browse files
Options
Downloads
Patches
Plain Diff
Return JSON intead of string (
#19
)
parent
d6350708
No related branches found
Branches containing commit
No related tags found
1 merge request
!14
Add endpoints to manage Dailies
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend-services/feed-service/app/controllers/DailyController.scala
+4
-3
4 additions, 3 deletions
...rvices/feed-service/app/controllers/DailyController.scala
backend-services/feed-service/app/models/Daily.scala
+19
-2
19 additions, 2 deletions
backend-services/feed-service/app/models/Daily.scala
with
23 additions
and
5 deletions
backend-services/feed-service/app/controllers/DailyController.scala
+
4
−
3
View file @
7338eb28
...
...
@@ -24,8 +24,8 @@ class DailyController @Inject()(val controllerComponents: ControllerComponents)
try
{
val
result
:
Seq
[
Daily
]
=
Daily
.
getAllDailiesAsync
()
val
jsonResult
:
Seq
[
String
]
=
result
.
map
(
daily
=>
Daily
.
toString
(
daily
)
)
Ok
(
jsonResult
.
toString
()
)
val
jsonResult
=
Daily
.
toJson
(
result
)
Ok
(
jsonResult
)
}
catch
{
case
ex
:
TimeoutException
=>
BadRequest
(
"Request timed out"
)
case
ex
=>
BadRequest
(
"Exception raised"
)
...
...
@@ -38,7 +38,8 @@ class DailyController @Inject()(val controllerComponents: ControllerComponents)
try
{
// Dummy data
val
result
=
Daily
.
createDailyAsync
(
None
,
new
ObjectId
(
"641128f7e80bcd1ba39d04ae"
),
new
ObjectId
(
"641128f7e80bcd1ba39d04ae"
),
"asddas"
,
221
)
Ok
(
Daily
.
toString
(
result
))
val
jsonResult
=
Daily
.
toJson
(
result
)
Ok
(
jsonResult
)
}
catch
{
case
e
:
TimeoutException
=>
BadRequest
(
"Request timed out"
)
case
ex
=>
BadRequest
(
"Exception raised"
)
...
...
This diff is collapsed.
Click to expand it.
backend-services/feed-service/app/models/Daily.scala
+
19
−
2
View file @
7338eb28
...
...
@@ -6,6 +6,9 @@ import scala.concurrent.ExecutionContext.Implicits.global
import
scala.concurrent.
{
Future
,
Await
}
import
scala.concurrent.duration._
import
play.api.libs.json.Json
import
play.api.libs.json.
{
Json
,
JsValue
,
JsString
,
JsNumber
,
JsObject
,
JsArray
}
case
class
Daily
(
id
:
Option
[
ObjectId
],
...
...
@@ -36,10 +39,24 @@ object Daily {
Await
.
result
(
future
,
timeout
.
seconds
)
}
// Convert from Daily object to JSON (serializing to JSON)
def
toJson
(
daily
:
Daily
)
:
JsValue
=
{
val
dailyJson
=
Seq
(
"id"
->
JsString
(
daily
.
id
.
getOrElse
(
""
).
toString
()),
"userId"
->
JsString
(
daily
.
userId
.
toString
()),
"questionId"
->
JsString
(
daily
.
questionId
.
toString
()),
"content"
->
JsString
(
daily
.
content
),
"likes"
->
JsNumber
(
daily
.
likes
)
)
Json
.
toJson
(
JsObject
(
dailyJson
))
}
def
getAllDailies
()
:
Future
[
Seq
[
Daily
]]
=
{
dailyRepo
.
getAllDailies
()
// Convert from Daily set to JSON (serializing to JSON)
def
toJson
(
dailies
:
Seq
[
Daily
])
:
JsValue
=
{
val
dailiesJson
=
dailies
.
map
(
daily
=>
Daily
.
toJson
(
daily
))
Json
.
toJson
(
JsArray
(
dailiesJson
))
}
def
toString
(
daily
:
Daily
)
:
String
=
...
...
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