Skip to content

Commit

Permalink
Implement voting
Browse files Browse the repository at this point in the history
Fixes #200
  • Loading branch information
1-alex98 committed Apr 11, 2018
1 parent 8b2c06c commit 08301d8
Show file tree
Hide file tree
Showing 18 changed files with 553 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/faforever/api/data/domain/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Setter;
import org.hibernate.annotations.BatchSize;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
Expand All @@ -30,6 +31,7 @@ public class Player extends Login {
private List<ClanMembership> clanMemberships;
private List<NameRecord> names;
private List<AvatarAssignment> avatarAssignments;
private List<Vote> votes;

@OneToOne(mappedBy = "player", fetch = FetchType.LAZY)
@BatchSize(size = 1000)
Expand Down Expand Up @@ -78,4 +80,10 @@ public List<AvatarAssignment> getAvatarAssignments() {
public String toString() {
return "Player(" + getId() + ", " + getLogin() + ")";
}

@Transient
@OneToMany(mappedBy = "player", cascade = CascadeType.ALL, orphanRemoval = true)
public List<Vote> getVotes() {
return votes;
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/faforever/api/data/domain/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.faforever.api.data.domain;

import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ReadPermission;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.List;

@Entity
@Table(name = "vote")
@Include(type = Vote.TYPE_NAME)
@ReadPermission(expression = "Prefab.Role.None")
@Setter
public class Vote extends AbstractEntity {
public static final String TYPE_NAME = "vote";

private Player player;
private VotingSubject votingSubject;
private List<VotingAnswer> votingAnswers;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "player_id")
public Player getPlayer() {
return player;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "voting_subject_id")
public VotingSubject getVotingSubject() {
return votingSubject;
}

@OneToMany(mappedBy = "vote", cascade = CascadeType.ALL, orphanRemoval = true)
public List<VotingAnswer> getVotingAnswers() {
return votingAnswers;
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/faforever/api/data/domain/VotingAnswer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.faforever.api.data.domain;

import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ReadPermission;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "voting_answer")
@Include(type = VotingAnswer.TYPE_NAME)
@ReadPermission(expression = "Prefab.Role.None")
@Setter
public class VotingAnswer extends AbstractEntity {
public static final String TYPE_NAME = "votingAnswer";

private VotingQuestion votingQuestion;
private Vote vote;
private VotingChoice votingChoice;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "voting_question_id")
public VotingQuestion getVotingQuestion() {
return votingQuestion;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vote_id")
public Vote getVote() {
return vote;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "voting_choice_id")
public VotingChoice getVotingChoice() {
return votingChoice;
}
}
75 changes: 75 additions & 0 deletions src/main/java/com/faforever/api/data/domain/VotingChoice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.faforever.api.data.domain;

import com.faforever.api.data.checks.permission.IsModerator;
import com.faforever.api.data.listeners.VotingChoiceEnricher;
import com.yahoo.elide.annotation.ComputedAttribute;
import com.yahoo.elide.annotation.CreatePermission;
import com.yahoo.elide.annotation.DeletePermission;
import com.yahoo.elide.annotation.Exclude;
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import java.util.List;

@Entity
@Table(name = "voting_choice")
@ReadPermission(expression = "Prefab.Role.All")
@SharePermission(expression = IsModerator.EXPRESSION)
@DeletePermission(expression = IsModerator.EXPRESSION)
@UpdatePermission(expression = IsModerator.EXPRESSION)
@CreatePermission(expression = IsModerator.EXPRESSION)
@Include(rootLevel = true, type = VotingChoice.TYPE_NAME)
@Setter
@EntityListeners(VotingChoiceEnricher.class)
public class VotingChoice extends AbstractEntity {
public static final String TYPE_NAME = "votingChoice";

private String name;
private int numberOfAnswers;
private String description;
private VotingQuestion votingQuestion;
private List<VotingAnswer> votingAnswers;

@NotNull
@Column(name = "name", nullable = false)
public String getName() {
return name;
}

@Transient
@ComputedAttribute
public int getNumberOfAnswers() {
return numberOfAnswers;
}

@Column(name = "description")
public String getDescription() {
return description;
}

@JoinColumn(name = "voting_question_id")
@ManyToOne(fetch = FetchType.LAZY)
public VotingQuestion getVotingQuestion() {
return votingQuestion;
}

@Exclude
@OneToMany(mappedBy = "votingChoice", cascade = CascadeType.ALL, orphanRemoval = true)
public List<VotingAnswer> getVotingAnswers() {
return votingAnswers;
}
}
79 changes: 79 additions & 0 deletions src/main/java/com/faforever/api/data/domain/VotingQuestion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.faforever.api.data.domain;

import com.faforever.api.data.checks.permission.IsModerator;
import com.faforever.api.data.listeners.VotingQuestionEnricher;
import com.yahoo.elide.annotation.ComputedAttribute;
import com.yahoo.elide.annotation.CreatePermission;
import com.yahoo.elide.annotation.DeletePermission;
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import java.util.List;

@Entity
@Table(name = "voting_question")
@ReadPermission(expression = "Prefab.Role.All")
@SharePermission(expression = IsModerator.EXPRESSION)
@DeletePermission(expression = IsModerator.EXPRESSION)
@UpdatePermission(expression = IsModerator.EXPRESSION)
@CreatePermission(expression = IsModerator.EXPRESSION)
@Include(rootLevel = true, type = VotingQuestion.TYPE_NAME)
@Setter
@EntityListeners(VotingQuestionEnricher.class)
public class VotingQuestion extends AbstractEntity {
public static final String TYPE_NAME = "votingQuestion";

private int numberOfAnswers;
private String question;
private String description;
private int maxAnswers;
private VotingSubject votingSubject;
private List<VotingChoice> votingChoices;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "voting_subject_id")
public VotingSubject getVotingSubject() {
return votingSubject;
}

@NotNull
@Column(name = "question", nullable = false)
public String getQuestion() {
return question;
}

@Column(name = "description")
public String getDescription() {
return description;
}

@Column(name = "max_answers")
public int getMaxAnswers() {
return maxAnswers;
}

@Transient
@ComputedAttribute
public int getNumberOfAnswers() {
return numberOfAnswers;
}

@OneToMany(mappedBy = "votingQuestion", cascade = CascadeType.ALL, orphanRemoval = true)
public List<VotingChoice> getVotingChoices() {
return votingChoices;
}
}
103 changes: 103 additions & 0 deletions src/main/java/com/faforever/api/data/domain/VotingSubject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.faforever.api.data.domain;

import com.faforever.api.data.checks.permission.IsModerator;
import com.faforever.api.data.listeners.VotingSubjectEnricher;
import com.yahoo.elide.annotation.ComputedAttribute;
import com.yahoo.elide.annotation.CreatePermission;
import com.yahoo.elide.annotation.DeletePermission;
import com.yahoo.elide.annotation.Exclude;
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotNull;
import java.time.OffsetDateTime;
import java.util.List;

@Entity
@Table(name = "voting_subject")
@Include(rootLevel = true, type = VotingSubject.TYPE_NAME)
@ReadPermission(expression = "Prefab.Role.All")
@SharePermission(expression = IsModerator.EXPRESSION)
@DeletePermission(expression = IsModerator.EXPRESSION)
@UpdatePermission(expression = IsModerator.EXPRESSION)
@CreatePermission(expression = IsModerator.EXPRESSION)
@Setter
@EntityListeners(VotingSubjectEnricher.class)
public class VotingSubject extends AbstractEntity {
public static final String TYPE_NAME = "votingSubject";

private String name;
private int numberOfVotes;
private String topicUrl;
private OffsetDateTime beginOfVoteTime;
private OffsetDateTime endOfVoteTime;
private int minGamesToVote;
private String description;
private List<Vote> votes;
private List<VotingQuestion> votingQuestions;

@Column(name = "name")
@NotNull
public String getName() {
return name;
}

@Transient
@ComputedAttribute
public int getNumberOfVotes() {
return numberOfVotes;
}


@NotNull
@Column(name = "begin_of_vote_time")
public OffsetDateTime getBeginOfVoteTime() {
return beginOfVoteTime;
}

@NotNull
@Column(name = "end_of_vote_time")
public OffsetDateTime getEndOfVoteTime() {
return endOfVoteTime;
}

@DecimalMin("0")
@Column(name = "min_games_to_vote")
public int getMinGamesToVote() {
return minGamesToVote;
}

@Column(name = "description")
public String getDescription() {
return description;
}

@Column(name = "topic_url")
public String getTopicUrl() {
return topicUrl;
}

@Exclude
@OneToMany(mappedBy = "votingSubject", cascade = CascadeType.ALL, orphanRemoval = true)
public List<Vote> getVotes() {
return votes;
}

@NotEmpty(message = "A subject needs at least one Question")
@OneToMany(mappedBy = "votingSubject", cascade = CascadeType.ALL, orphanRemoval = true)
public List<VotingQuestion> getVotingQuestions() {
return votingQuestions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.faforever.api.data.listeners;

import com.faforever.api.data.domain.VotingChoice;
import org.springframework.stereotype.Component;

import javax.persistence.PostLoad;

@Component
public class VotingChoiceEnricher {

@PostLoad
public void enhance(VotingChoice votingChoice) {
votingChoice.setNumberOfAnswers(votingChoice.getVotingAnswers().size());
}
}

0 comments on commit 08301d8

Please sign in to comment.