Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
MilderJoghurt
rlf-server
Commits
03be86b6
Commit
03be86b6
authored
May 27, 2015
by
Fjen Undso
Browse files
add json checks and reduce depth in controllermethods
parent
09151924
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/controllers/QuestionController.java
View file @
03be86b6
package
controllers
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
models.QuestionAnswer
;
import
models.Session
;
import
models.Vote
;
...
...
@@ -11,20 +13,24 @@ public class QuestionController extends Controller {
public
static
Result
createAnswer
(
String
sid
)
{
Session
session
=
Session
.
find
.
byId
(
sid
);
if
(
session
!=
null
)
{
QuestionAnswer
answer
=
Json
.
fromJson
(
request
().
body
().
asJson
(),
QuestionAnswer
.
class
);
if
(!
answer
.
owner
.
isEmpty
())
{
QuestionAnswer
inserted
=
new
QuestionAnswer
(
session
,
answer
.
owner
,
answer
.
answer
);
session
.
addQuestionAnswer
(
inserted
);
session
.
save
();
return
created
(
Json
.
toJson
(
inserted
));
}
else
{
return
badRequest
(
"owner missing"
);
}
}
else
{
if
(
session
==
null
)
{
return
notFound
(
"session not found"
);
}
JsonNode
json
=
request
().
body
().
asJson
();
if
(
json
==
null
)
{
return
badRequest
(
"Expecting Json data"
);
}
QuestionAnswer
answer
=
Json
.
fromJson
(
json
,
QuestionAnswer
.
class
);
if
(!
answer
.
owner
.
isEmpty
())
{
QuestionAnswer
inserted
=
new
QuestionAnswer
(
session
,
answer
.
owner
,
answer
.
answer
);
session
.
addQuestionAnswer
(
inserted
);
session
.
save
();
return
created
(
Json
.
toJson
(
inserted
));
}
else
{
return
badRequest
(
"owner missing"
);
}
}
}
app/controllers/SessionController.java
View file @
03be86b6
...
...
@@ -2,6 +2,8 @@ package controllers;
import
java.util.List
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
models.Session
;
import
play.libs.Json
;
import
play.mvc.Controller
;
...
...
@@ -27,8 +29,12 @@ public class SessionController extends Controller {
}
public
static
Result
createSession
()
{
Session
session
=
Json
.
fromJson
(
request
().
body
().
asJson
(),
Session
.
class
);
JsonNode
json
=
request
().
body
().
asJson
();
if
(
json
==
null
)
{
return
badRequest
(
"Expecting Json data"
);
}
Session
session
=
Json
.
fromJson
(
json
,
Session
.
class
);
if
(!(
session
.
name
.
isEmpty
()
||
session
.
owner
.
isEmpty
()))
{
Session
inserted
=
new
Session
(
session
.
owner
,
session
.
name
);
inserted
.
save
();
...
...
@@ -39,67 +45,71 @@ public class SessionController extends Controller {
}
public
static
Result
updateSession
(
String
sid
)
{
Session
session
=
Json
.
fromJson
(
request
().
body
().
asJson
(),
Session
.
class
);
JsonNode
json
=
request
().
body
().
asJson
();
if
(
json
==
null
)
{
return
badRequest
(
"Expecting Json data"
);
}
Session
session
=
Json
.
fromJson
(
json
,
Session
.
class
);
Session
sessionSaved
=
Session
.
find
.
byId
(
sid
);
if
(
sessionSaved
!=
null
)
{
if
(
sessionSaved
.
owner
==
session
.
owner
)
{
sessionSaved
.
name
=
session
.
name
;
sessionSaved
.
date
=
session
.
date
;
sessionSaved
.
save
();
return
noContent
();
}
else
{
return
forbidden
(
"wrong owner"
);
}
}
else
{
if
(
sessionSaved
==
null
)
{
return
notFound
(
"session not found"
);
}
if
(
sessionSaved
.
owner
==
session
.
owner
)
{
sessionSaved
.
name
=
session
.
name
;
sessionSaved
.
date
=
session
.
date
;
sessionSaved
.
save
();
return
noContent
();
}
else
{
return
forbidden
(
"wrong owner"
);
}
}
public
static
Result
deleteSession
(
String
sid
,
String
owner
)
{
Session
session
=
Session
.
find
.
byId
(
sid
);
if
(
session
!=
null
)
{
if
(
session
.
owner
==
owner
)
{
session
.
delete
();
return
noContent
();
}
else
{
return
forbidden
(
"wrong owner"
);
}
}
else
{
if
(
session
==
null
)
{
return
notFound
(
"session not found"
);
}
if
(
session
.
owner
==
owner
)
{
session
.
delete
();
return
noContent
();
}
else
{
return
forbidden
(
"wrong owner"
);
}
}
public
static
Result
getVotes
(
String
sid
)
{
Session
session
=
Session
.
find
.
byId
(
sid
);
if
(
session
!=
null
)
{
return
ok
(
Json
.
toJson
(
session
.
votes
));
}
else
{
if
(
session
==
null
)
{
return
notFound
(
"session not found"
);
}
else
{
return
ok
(
Json
.
toJson
(
session
.
votes
));
}
}
public
static
Result
getAnswers
(
String
sid
)
{
Session
session
=
Session
.
find
.
byId
(
sid
);
if
(
session
!=
null
)
{
return
ok
(
Json
.
toJson
(
session
.
questionAnswers
));
}
else
{
if
(
session
==
null
)
{
return
notFound
(
"session not found"
);
}
else
{
return
ok
(
Json
.
toJson
(
session
.
questionAnswers
));
}
}
public
static
Result
resetAnswers
(
String
sid
,
String
owner
)
{
Session
session
=
Session
.
find
.
byId
(
sid
);
if
(
session
!=
null
)
{
if
(
session
.
owner
==
owner
)
{
session
.
resetAnswers
();
session
.
save
();
return
noContent
();
}
else
{
return
forbidden
(
"wrong owner"
);
}
}
else
{
if
(
session
==
null
)
{
return
notFound
(
"session not found"
);
}
if
(
session
.
owner
==
owner
)
{
session
.
resetAnswers
();
session
.
save
();
return
noContent
();
}
else
{
return
forbidden
(
"wrong owner"
);
}
}
}
\ No newline at end of file
app/controllers/VoteController.java
View file @
03be86b6
package
controllers
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
models.Session
;
import
models.Vote
;
import
play.libs.Json
;
...
...
@@ -10,19 +12,23 @@ public class VoteController extends Controller {
public
static
Result
createVote
(
String
sid
)
{
Session
session
=
Session
.
find
.
byId
(
sid
);
if
(
session
!=
null
)
{
Vote
vote
=
Json
.
fromJson
(
request
().
body
().
asJson
(),
Vote
.
class
);
if
(!
vote
.
owner
.
isEmpty
())
{
Vote
inserted
=
new
Vote
(
session
,
vote
.
owner
,
vote
.
type
,
vote
.
vote
);
session
.
addVote
(
inserted
);
session
.
save
();
return
created
(
Json
.
toJson
(
inserted
));
}
else
{
return
badRequest
(
"owner missing"
);
}
}
else
{
if
(
session
==
null
)
{
return
notFound
(
"session not found"
);
}
JsonNode
json
=
request
().
body
().
asJson
();
if
(
json
==
null
)
{
return
badRequest
(
"Expecting Json data"
);
}
Vote
vote
=
Json
.
fromJson
(
json
,
Vote
.
class
);
if
(!
vote
.
owner
.
isEmpty
())
{
Vote
inserted
=
new
Vote
(
session
,
vote
.
owner
,
vote
.
type
,
vote
.
vote
);
session
.
addVote
(
inserted
);
session
.
save
();
return
created
(
Json
.
toJson
(
inserted
));
}
else
{
return
badRequest
(
"owner missing"
);
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment