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
280c1eab
Commit
280c1eab
authored
1 year ago
by
Felipe D'Abrantes
Browse files
Options
Downloads
Patches
Plain Diff
Add endpoint to insert a question
parent
a72d1562
No related branches found
Branches containing commit
No related tags found
1 merge request
!17
Implement Question Service
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend-services/feed-service/app/controllers/QuestionController.scala
+71
-0
71 additions, 0 deletions
...ces/feed-service/app/controllers/QuestionController.scala
backend-services/feed-service/conf/routes
+2
-0
2 additions, 0 deletions
backend-services/feed-service/conf/routes
with
73 additions
and
0 deletions
backend-services/feed-service/app/controllers/QuestionController.scala
0 → 100644
+
71
−
0
View file @
280c1eab
package
controllers
import
models.
{
Question
}
import
models.exceptions.
{
ConflictException
,
InvalidRequestBodyException
}
import
javax.inject._
import
play.api.mvc._
import
play.api.libs.json.
{
JsValue
}
import
play.api.libs.json.JsLookupResult
import
scala.concurrent.TimeoutException
/**
* This controller handles all the Question endpoints.
*/
@Singleton
class
QuestionController
@Inject
()(
val
controllerComponents
:
ControllerComponents
)
extends
BaseController
{
/**
* Create an Action to insert a Question to the DB.
*/
def
insertQuestion
()
=
Action
{
implicit
request
:
Request
[
AnyContent
]
=>
println
(
"QuestionController:insertQuestion"
)
try
{
val
questionText
:
String
=
fetchInsertQuestionRequestBody
(
request
.
body
)
Question
.
createQuestionAsync
(
questionText
)
Ok
(
"Inserted question"
)
}
catch
{
case
_:
TimeoutException
=>
BadRequest
(
"Request timed out"
)
case
ex
:
ConflictException
=>
BadRequest
(
ex
.
getMessage
())
case
_:
Throwable
=>
BadRequest
(
"Exception raised"
)
}
}
/**
* Fetch the needed values from the request body for the liking/unliking a Daily endpoint.
*
* @param requestBody The request's body.
*/
def
fetchInsertQuestionRequestBody
(
requestBody
:
AnyContent
)
:
String
=
{
if
(!
requestBody
.
asJson
.
isDefined
)
throw
new
InvalidRequestBodyException
(
"Request body must be in JSON format."
)
val
bodyJson
=
requestBody
.
asJson
.
get
fetchJsonBodyString
(
bodyJson
,
"questionText"
)
}
/**
* Fetch the value of the given field name from the JSON.
*
* @param bodyJson The JSON.
* @param fieldName The field name.
*/
def
fetchJsonBodyValue
(
bodyJson
:
JsValue
,
fieldName
:
String
)
:
JsValue
=
{
val
value
:
JsLookupResult
=
(
bodyJson
\
fieldName
)
if
(!
value
.
isDefined
)
throw
new
InvalidRequestBodyException
(
"Missing parameter: "
+
fieldName
)
value
.
get
}
/**
* Fetch the String value of the field name from the JSON.
*
* @param bodyJson The JSON.
* @param fieldName The field name.
*/
def
fetchJsonBodyString
(
bodyJson
:
JsValue
,
fieldName
:
String
)
:
String
=
{
fetchJsonBodyValue
(
bodyJson
,
fieldName
).
as
[
String
]
}
}
This diff is collapsed.
Click to expand it.
backend-services/feed-service/conf/routes
+
2
−
0
View file @
280c1eab
...
...
@@ -26,3 +26,5 @@ GET /test/verifyUser controllers.TestController.verifyUser(userId: St
GET /question controllers.DailyQuestionController.getDailyQuestion()
POST /question controllers.QuestionController.insertQuestion()
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