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
6eb37cd1
Commit
6eb37cd1
authored
Jun 23, 2015
by
Fjen Undso
Browse files
add VoteStats. incomplete aggregation
parent
e3e278c5
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/controllers/VoteController.java
View file @
6eb37cd1
package
controllers
;
import
java.util.ArrayList
;
import
java.util.List
;
import
models.Session
;
import
models.Vote
;
import
models.VoteStats
;
import
play.libs.Json
;
import
play.mvc.BodyParser
;
import
play.mvc.Controller
;
...
...
@@ -34,7 +38,7 @@ public class VoteController extends Controller {
JsonNode
json
=
request
().
body
().
asJson
();
Vote
vote
=
Json
.
fromJson
(
json
,
Vote
.
class
);
if
(!
vote
.
owner
.
isEmpty
())
{
Vote
inserted
=
new
Vote
(
session
,
vote
.
owner
,
vote
.
type
,
vote
.
v
ot
e
);
Vote
inserted
=
new
Vote
(
session
,
vote
.
owner
,
vote
.
type
,
vote
.
v
alu
e
);
session
.
addVote
(
inserted
);
session
.
save
();
return
created
(
Json
.
toJson
(
inserted
));
// 201
...
...
@@ -61,4 +65,53 @@ public class VoteController extends Controller {
return
ok
(
Json
.
toJson
(
session
.
votes
));
// 200
}
}
/**
* Gets aggregated statistics of {@link Vote}s for a {@link Session} as
* {@link VoteStats}
*
* @param sid
* the ID of a Session
* @return list of VoteStats
*/
public
static
Result
getVoteStats
(
String
sid
)
{
Session
session
=
Session
.
find
.
byId
(
sid
);
if
(
session
==
null
)
{
return
notFound
(
"session not found"
);
// 404
}
else
{
VoteStats
sAll
=
new
VoteStats
(
VoteStats
.
Type
.
ALL
,
0
);
VoteStats
sSpeed
=
new
VoteStats
(
VoteStats
.
Type
.
SPEED
,
0
);
VoteStats
sUnderstandability
=
new
VoteStats
(
VoteStats
.
Type
.
UNDERSTANDABILITY
,
0
);
VoteStats
sRequests
=
new
VoteStats
(
VoteStats
.
Type
.
REQUEST
,
0
);
List
<
VoteStats
>
vsList
=
new
ArrayList
<
VoteStats
>();
vsList
.
add
(
sAll
);
vsList
.
add
(
sSpeed
);
vsList
.
add
(
sUnderstandability
);
vsList
.
add
(
sRequests
);
// generate statistics
for
(
Vote
v
:
session
.
votes
)
{
switch
(
v
.
type
)
{
case
SPEED:
// TODO: aggregate
sSpeed
.
value
=
v
.
value
;
sAll
.
value
=
v
.
value
;
break
;
case
UNDERSTANDABILITY:
// TODO: aggregate
sUnderstandability
.
value
=
v
.
value
;
sAll
.
value
=
v
.
value
;
break
;
case
REQUEST:
sRequests
.
value
+=
v
.
value
.
compareTo
(
0
);
break
;
default
:
break
;
}
}
return
ok
(
Json
.
toJson
(
vsList
));
// 200
}
}
}
app/models/Vote.java
View file @
6eb37cd1
...
...
@@ -34,7 +34,7 @@ public class Vote extends Model {
public
Date
date
=
new
Date
();
@Column
(
nullable
=
true
)
public
Integer
v
ot
e
;
public
Integer
v
alu
e
;
@Constraints
.
Required
public
Type
type
;
...
...
@@ -48,19 +48,16 @@ public class Vote extends Model {
@EnumValue
(
"R"
)
REQUEST
,
@EnumValue
(
"N"
)
NOREQUEST
,
}
public
static
Finder
<
Long
,
Vote
>
find
=
new
Finder
<
Long
,
Vote
>(
Long
.
class
,
Vote
.
class
);
public
Vote
(
Session
session
,
String
owner
,
Type
type
,
Integer
v
ot
e
)
{
public
Vote
(
Session
session
,
String
owner
,
Type
type
,
Integer
v
alu
e
)
{
this
.
session
=
session
;
this
.
owner
=
owner
;
this
.
type
=
type
;
this
.
v
ot
e
=
v
ot
e
;
this
.
v
alu
e
=
v
alu
e
;
}
}
app/models/VoteStats.java
0 → 100644
View file @
6eb37cd1
package
models
;
import
java.util.Date
;
import
javax.persistence.Column
;
import
javax.persistence.Entity
;
import
javax.persistence.Id
;
import
play.data.format.Formats
;
import
play.data.validation.Constraints
;
import
play.db.ebean.Model
;
import
com.avaje.ebean.annotation.EnumValue
;
@Entity
public
class
VoteStats
extends
Model
{
private
static
final
long
serialVersionUID
=
8660259739658830119L
;
@Id
public
Long
id
;
@Formats
.
DateTime
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
public
Date
date
=
new
Date
();
@Column
(
nullable
=
true
)
public
Integer
value
;
@Constraints
.
Required
public
Type
type
;
public
enum
Type
{
@EnumValue
(
"A"
)
ALL
,
@EnumValue
(
"S"
)
SPEED
,
@EnumValue
(
"U"
)
UNDERSTANDABILITY
,
@EnumValue
(
"R"
)
REQUEST
,
}
public
static
Finder
<
Long
,
VoteStats
>
find
=
new
Finder
<
Long
,
VoteStats
>(
Long
.
class
,
VoteStats
.
class
);
public
VoteStats
(
Type
type
,
Integer
value
)
{
this
.
type
=
type
;
this
.
value
=
value
;
}
}
conf/routes
View file @
6eb37cd1
...
...
@@ -18,4 +18,4 @@ POST /sessions/:sid/resetanswers/:owner controllers.QuestionController.reset
# Vote
GET /sessions/:sid/votes controllers.VoteController.getVotes(sid: String)
POST /sessions/:sid/votes controllers.VoteController.createVote(sid: String)
# TODO: aggregierte daten abfragen
\ No newline at end of file
GET /sessions/:sid/votestats controllers.VoteController.getVoteStats(sid: String)
\ No newline at end of file
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