Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vote functionality #62

Merged
merged 16 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lv.ctco.javaschool.eventorganaizer.boundary;

import lv.ctco.javaschool.auth.control.UserStore;
import lv.ctco.javaschool.auth.entity.domain.User;
import lv.ctco.javaschool.eventorganaizer.control.AnswersStore;
import lv.ctco.javaschool.eventorganaizer.control.EventStore;
import lv.ctco.javaschool.eventorganaizer.control.PollStore;
Expand All @@ -13,13 +14,12 @@
import lv.ctco.javaschool.eventorganaizer.entity.PollDto;
import lv.ctco.javaschool.eventorganaizer.entity.TopicDto;
import lv.ctco.javaschool.eventorganaizer.entity.TopicListDto;
import lv.ctco.javaschool.eventorganaizer.entity.UserPoll;

import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand All @@ -32,9 +32,6 @@
@Path("/event")
@Stateless
public class EventOrganizationApi {
@PersistenceContext
private EntityManager em;
//
@Inject
private UserStore userStore;

Expand Down Expand Up @@ -122,14 +119,35 @@ public List<PollDto> getPollForEvent(@PathParam("id") Long id) {

@POST
@RolesAllowed({"ADMIN", "USER"})
@Path("/{id}/vote")
@Path("/vote/{id}")
public void updateVoteCounter(@PathParam("id") Long id) {
Optional<Answer> answer = answersStore.getAnswerByID(id);
if (answer.isPresent()) {
answer.get().setCounter(answer.get().getCounter() + 1);
User currentUser = userStore.getCurrentUser();
Optional<Answer> userAnswer = answersStore.getAnswerByID(id);
if (userAnswer.isPresent()) {
Answer answer = userAnswer.get();
Optional<UserPoll> userVote = pollStore.getUserPollByUserAndPoll(currentUser, answer.getPoll());
if (userVote.isPresent()) {
throw new EntityNotFoundException();
}
registerNewUserPoll(currentUser, answer);
}
}

@GET
@RolesAllowed({"ADMIN", "USER"})
@Path("/{id}/getAnswers")
public List<PollDto> getPollsByEventId() {
User currentUser = userStore.getCurrentUser();
List<UserPoll> userPollList = pollStore.getUserPollByUser(currentUser);
List<PollDto> pollDtos = new ArrayList<>();
userPollList.forEach(up -> {
PollDto pollDto = new PollDto();
pollDto.setId(up.getPoll().getId());
pollDtos.add(pollDto);
});
return pollDtos;
}

@GET
@RolesAllowed({"ADMIN", "USER"})
@Path("/{id}/getVotes")
Expand All @@ -138,7 +156,9 @@ public List<AnswerDto> getVotes(@PathParam("id") Long id) {
List<AnswerDto> answerDtos = new ArrayList<>();
poll.ifPresent(p -> {
List<Answer> answerList = answersStore.getAnswersByPollID(p);
mapper.mapAnswerToAnswerDto(answerList);
mapper.mapAnswerToAnswerDto(answerList).forEach(a -> {
answerDtos.add(a);
});
});
return answerDtos;
}
Expand All @@ -156,7 +176,8 @@ public List<PollDto> getFeedbackForEvent(@PathParam("id") Long id) {
@Path("/{id}/getVotingPoll")
public List<PollDto> getVotingForEvent(@PathParam("id") Long id) {
List<Poll> poll = pollStore.getVotingPoll(id);
return mapper.mapPollToDto(poll);
List<PollDto> pollDtos = mapper.mapPollToDto(poll);
return pollDtos;
}

@POST
Expand All @@ -172,4 +193,12 @@ public void deleteEvent(@PathParam("id") Long id) throws IllegalArgumentExceptio
public void deletePoll(@PathParam("id") Long id) throws IllegalArgumentException {
pollStore.deletePollById(id);
}

private void registerNewUserPoll(User currentUser, Answer answer) {
UserPoll newUserPoll = new UserPoll();
newUserPoll.setUser(currentUser);
newUserPoll.setPoll(answer.getPoll());
pollStore.persistUserPoll(newUserPoll);
answer.setCounter(answer.getCounter() + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import lv.ctco.javaschool.eventorganaizer.entity.Poll;
import lv.ctco.javaschool.eventorganaizer.entity.PollDto;

import javax.ws.rs.PathParam;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -50,7 +49,7 @@ public List<AnswerDto> mapAnswerToAnswerDto(List<Answer> answerList) {
List<AnswerDto> answerDtos = new ArrayList<>();
answerList.forEach(al -> {
AnswerDto a = new AnswerDto();
a.setAnswerCounter(al.getCounter());
a.setCounter(al.getCounter());
a.setThisAnswerID(al.getId());
answerDtos.add(a);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package lv.ctco.javaschool.eventorganaizer.control;

import lv.ctco.javaschool.auth.entity.domain.User;
import lv.ctco.javaschool.eventorganaizer.entity.Answer;
import lv.ctco.javaschool.eventorganaizer.entity.Poll;
import lv.ctco.javaschool.eventorganaizer.entity.UserPoll;

import javax.ejb.Stateless;
import javax.inject.Inject;
Expand All @@ -15,9 +17,6 @@ public class AnswersStore {
@PersistenceContext
private EntityManager em;

@Inject
private AnswersStore answersStore;

public List<Answer> getAnswersByPollID(Poll poll) {
return em.createQuery("select a from Answer a" +
" where a.poll = :poll", Answer.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ public class EventStore {
private UserStore userStore;

public List<Event> getAllEvents() {
List<Event> list;
list = em.createQuery("select e from Event e" +
return em.createQuery("select e from Event e" +
" where e.status = :status1 or e.status=:status2", Event.class)
.setParameter("status1", EventStatus.OPEN)
.setParameter("status2", EventStatus.CLOSED)
.getResultList();
return list;
}

public Optional<Event> getEventById(Long id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package lv.ctco.javaschool.eventorganaizer.control;

import lv.ctco.javaschool.eventorganaizer.entity.Event;
import lv.ctco.javaschool.auth.entity.domain.User;
import lv.ctco.javaschool.eventorganaizer.entity.Answer;
import lv.ctco.javaschool.eventorganaizer.entity.Poll;
import lv.ctco.javaschool.eventorganaizer.entity.UserPoll;

import javax.ejb.Stateless;
import javax.inject.Inject;
Expand All @@ -15,9 +17,6 @@ public class PollStore {
@PersistenceContext
private EntityManager em;

@Inject
private PollStore pollStore;

public List<Poll> getPollForEvent(Long id) {
return em.createQuery("select p from Poll p" +
" where p.eventID=:id", Poll.class)
Expand All @@ -34,14 +33,14 @@ public int deletePollById(Long id) {

public List<Poll> getVotingPoll(Long id) {
return em.createQuery("select p from Poll p" +
" where p.eventID=:id and p.isFeedback=false", Poll.class)
" where p.eventID = :id and p.isFeedback = false", Poll.class)
.setParameter("id", id)
.getResultList();
}

public List<Poll> getFeedbackPoll(Long id) {
return em.createQuery("select p from Poll p" +
" where p.eventID=:id and p.isFeedback=true", Poll.class)
" where p.eventID = :id and p.isFeedback = true", Poll.class)
.setParameter("id", id)
.getResultList();
}
Expand All @@ -54,6 +53,26 @@ public Optional<Poll> getPollById (Long id) {
.findFirst();
}

public Optional<UserPoll> getUserPollByUserAndPoll(User user, Poll poll) {
return em.createQuery("select ua from UserPoll ua" +
" where ua.user = :user and ua.poll = :poll", UserPoll.class)
.setParameter("user", user)
.setParameter("poll", poll)
.getResultStream()
.findFirst();
}

public List<UserPoll> getUserPollByUser(User user) {
return em.createQuery("select ua from UserPoll ua" +
" where ua.user = :user", UserPoll.class)
.setParameter("user", user)
.getResultList();
}

public void persistUserPoll(UserPoll userPoll) {
em.persist(userPoll);
}

public void persistPoll(Poll poll) {
em.persist(poll);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ public int getCounter() {
public void setCounter(int counter) {
this.counter = counter;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

public class AnswerDto {
private Long thisAnswerID;
private int answerCounter;
private int counter;
private String text;

public AnswerDto() {
}

public AnswerDto(Long answerID, int counter) {
this.thisAnswerID = answerID;
this.answerCounter = counter;
this.counter = counter;
}

public Long getThisAnswerID() {
Expand All @@ -21,12 +21,12 @@ public void setThisAnswerID(Long thisAnswerID) {
this.thisAnswerID = thisAnswerID;
}

public int getAnswerCounter() {
return answerCounter;
public int getCounter() {
return counter;
}

public void setAnswerCounter(int answerCounter) {
this.answerCounter = answerCounter;
public void setCounter(int counter) {
this.counter = counter;
}

public String getText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@
import lv.ctco.javaschool.auth.entity.domain.User;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "events")
public class Event {
@Id
@GeneratedValue
private Long id;

private String name;

@ManyToOne
@JoinColumn(name = "user_id")
private User author;

private String name;
private String date;
private String time;
private String description;
private String agenda;

@Enumerated(EnumType.STRING)
private EventStatus status;

public Event() {
Expand Down
Loading