package com.pollistics.services;

import com.pollistics.models.Poll;
import com.pollistics.repositories.PollRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class PollService {
	@Autowired
	private PollRepository pollRepo;

	public Poll getPoll(String id) {
		return pollRepo.findOne(id);
	}

	public List<Poll> getAllPolls() {
		return pollRepo.findAll();
	}

	public String createPoll(String title, HashMap<String, Integer> options) {
		Poll poll = pollRepo.insert(new Poll(title, options));
		return poll.getId();
	}

	public boolean deletePoll(String id) {
		try {
			pollRepo.delete(id);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean voteOption(Poll p, String option) {
		boolean result = p.vote(option);
		if (!result) {
			return false;
		}
		pollRepo.save(p); // todo false when not works
		return true;
	}
}

@@ -62,16 +62,51 @@
 s.Poll;%0A
+import com.pollistics.models.User;%0A
 import c
@@ -148,16 +148,16 @@
 sitory;%0A
-
 import o
@@ -259,16 +259,44 @@
 rvice;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -711,24 +711,202 @@
 .getId();%0A%09%7D
+%0A%09%0A%09public String createPoll(String title, HashMap%3CString, Integer%3E options, User user) %7B%0A%09%09Poll poll = pollRepo.insert(new Poll(title, options, user));%0A%09%09return poll.getId();%0A%09%7D
 %0A%0A%09public bo
@@ -1208,16 +1208,16 @@
 t works%0A
-
 %09%09return
@@ -1226,10 +1226,92 @@
 rue;%0A%09%7D%0A
+%09%0A%09public List%3CPoll%3E getPolls(User user) %7B%0A%09%09return pollRepo.findByUser(user);%0A%09%7D%0A
 %7D%0A

create poll with user parameter