From cf5b8caf791d6d25b9705985ab636bba130dfcdd Mon Sep 17 00:00:00 2001 From: Julian Hochstetter <julian.hochstetter@mni.thm.de> Date: Fri, 2 Nov 2012 11:28:56 +0100 Subject: [PATCH] Task #4056: countFoodVote is returning a simple integer indicating the total count of food votes today Task #4055: getFoodVote is returning a list of given votes Task #4245: HTTP POST vote for a menu --- .../controller/FoodVoteController.java | 71 ++++++++++++++++ .../java/de/thm/arsnova/dao/CouchDBDao.java | 80 +++++++++++++++++++ .../java/de/thm/arsnova/dao/IDatabaseDao.java | 7 ++ .../de/thm/arsnova/entities/FoodVote.java | 63 +++++++++++++++ .../de/thm/arsnova/services/FoodService.java | 57 +++++++++++++ .../de/thm/arsnova/services/IFoodService.java | 34 ++++++++ .../de/thm/arsnova/dao/StubDatabaseDao.java | 19 +++++ 7 files changed, 331 insertions(+) create mode 100644 src/main/java/de/thm/arsnova/controller/FoodVoteController.java create mode 100644 src/main/java/de/thm/arsnova/entities/FoodVote.java create mode 100644 src/main/java/de/thm/arsnova/services/FoodService.java create mode 100644 src/main/java/de/thm/arsnova/services/IFoodService.java diff --git a/src/main/java/de/thm/arsnova/controller/FoodVoteController.java b/src/main/java/de/thm/arsnova/controller/FoodVoteController.java new file mode 100644 index 00000000..a943b4e9 --- /dev/null +++ b/src/main/java/de/thm/arsnova/controller/FoodVoteController.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package de.thm.arsnova.controller; + +import java.util.List; + +import javax.servlet.http.HttpServletResponse; + +import net.sf.json.JSONObject; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import de.thm.arsnova.entities.FoodVote; +import de.thm.arsnova.services.IFoodService; + +@Controller +public class FoodVoteController extends AbstractController { + + public static final Logger LOGGER = LoggerFactory + .getLogger(FoodVoteController.class); + + @Autowired + private IFoodService foodService; + + @RequestMapping(value = "/canteen/vote", method = RequestMethod.POST) + public final void setFoodVote( + @RequestBody final Object menu, + final HttpServletResponse response + ) { + String menustring = JSONObject.fromObject(menu).getString("menu"); + foodService.vote(menustring); + } + + @RequestMapping(value = "/canteen/foodvote", method = RequestMethod.GET) + @ResponseBody + public final List<FoodVote> getFoodVote() { + return foodService.getFoodVote(); + } + + @RequestMapping(value = "/canteen/foodvotecount", method = RequestMethod.GET) + @ResponseBody + public final int getFoodVoteCount() { + return foodService.getFoodVoteCount(); + } + + + +} diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java index ac68c2c0..ceefc5b5 100644 --- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java +++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java @@ -22,9 +22,11 @@ package de.thm.arsnova.dao; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -54,6 +56,7 @@ import com.fourspaces.couchdb.ViewResults; import de.thm.arsnova.entities.Answer; import de.thm.arsnova.entities.Feedback; +import de.thm.arsnova.entities.FoodVote; import de.thm.arsnova.entities.LoggedIn; import de.thm.arsnova.entities.PossibleAnswer; import de.thm.arsnova.entities.Question; @@ -1031,4 +1034,81 @@ public class CouchDBDao implements IDatabaseDao { } return null; } + + @Override + public void vote(String menu) { + User u = this.userService.getCurrentUser(); + if(u == null) { + throw new UnauthorizedException(); + } + + String date = new SimpleDateFormat("dd-mm-yyyyy").format(new Date()); + try { + View view = new View("food_vote/get_user_vote"); + view.setKey("[" + URLEncoder.encode("\"" + date + "\",\"" + u.getUsername() + "\"", "UTF-8") + "]"); + ViewResults results = this.getDatabase().view(view); + + if(results.getResults().isEmpty()) { + Document vote = new Document(); + vote.put("type", "food_vote"); + vote.put("name", menu); + vote.put("user", u.getUsername()); + vote.put("day", date); + this.database.saveDocument(vote); + } else { + Document vote = results.getResults().get(0); + vote.put("name", menu); + this.database.saveDocument(vote); + } + } catch (UnsupportedEncodingException e) { + LOGGER.error("Error while retrieving user food vote", e); + } catch (IOException e) { + LOGGER.error("Error while saving user food vote", e); + } + } + + @Override + public List<FoodVote> getFoodVote() { + List<FoodVote> foodVotes = new ArrayList<FoodVote>(); + String date = new SimpleDateFormat("dd-mm-yyyyy").format(new Date()); + try { + View view = new View("food_vote/count_by_day"); + view.setStartKey("[" + URLEncoder.encode("\"" + date + "\"", "UTF-8") + "]"); + view.setEndKey("[" + URLEncoder.encode("\"" + date + "\",{}", "UTF-8") + "]"); + view.setGroup(true); + ViewResults results = this.getDatabase().view(view); + for(Document d : results.getResults()) { + FoodVote vote = new FoodVote(); + vote.setCount(d.getJSONObject().optInt("value")); + vote.setDay(date); + vote.setName(d.getJSONObject().getJSONArray("key").getString(1)); + foodVotes.add(vote); + } + + return foodVotes; + + } catch (UnsupportedEncodingException e) { + LOGGER.error("Error while retrieving food vote count", e); + } + return foodVotes; + } + + @Override + public int getFoodVoteCount() { + String date = new SimpleDateFormat("dd-mm-yyyyy").format(new Date()); + try { + View view = new View("food_vote/count_by_day"); + view.setStartKey("[" + URLEncoder.encode("\"" + date + "\"", "UTF-8") + "]"); + view.setEndKey("[" + URLEncoder.encode("\"" + date + "\",{}", "UTF-8") + "]"); + view.setGroup(false); + ViewResults results = this.getDatabase().view(view); + if(results.size() == 0 || results.getResults().size() == 0) { + return 0; + } + return results.getJSONArray("rows").optJSONObject(0).optInt("value"); + } catch (UnsupportedEncodingException e) { + LOGGER.error("Error while retrieving food vote count", e); + } + return 0; + } } diff --git a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java index 0ae4ca13..0679c4b1 100644 --- a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java +++ b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java @@ -23,6 +23,7 @@ import java.util.List; import de.thm.arsnova.entities.Answer; import de.thm.arsnova.entities.Feedback; +import de.thm.arsnova.entities.FoodVote; import de.thm.arsnova.entities.LoggedIn; import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.Session; @@ -82,4 +83,10 @@ public interface IDatabaseDao { int getInterposedCount(String sessionKey); List<Question> getInterposedQuestions(String sessionKey); + + void vote(String menu); + + int getFoodVoteCount(); + + List<FoodVote> getFoodVote(); } diff --git a/src/main/java/de/thm/arsnova/entities/FoodVote.java b/src/main/java/de/thm/arsnova/entities/FoodVote.java new file mode 100644 index 00000000..efad8fac --- /dev/null +++ b/src/main/java/de/thm/arsnova/entities/FoodVote.java @@ -0,0 +1,63 @@ +package de.thm.arsnova.entities; + + +public class FoodVote { + private String _id; + private String _rev; + private String type; + private String name; + private int count; + private String day; + + public FoodVote() { + this.type = "food_vote"; + } + + public String get_id() { + return _id; + } + + public void set_id(String _id) { + this._id = _id; + } + + public String get_rev() { + return _rev; + } + + public void set_rev(String _rev) { + this._rev = _rev; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public String getDay() { + return day; + } + + public void setDay(String day) { + this.day = day; + } +} diff --git a/src/main/java/de/thm/arsnova/services/FoodService.java b/src/main/java/de/thm/arsnova/services/FoodService.java new file mode 100644 index 00000000..46fcabe7 --- /dev/null +++ b/src/main/java/de/thm/arsnova/services/FoodService.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package de.thm.arsnova.services; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import de.thm.arsnova.annotation.Authenticated; +import de.thm.arsnova.dao.IDatabaseDao; +import de.thm.arsnova.entities.FoodVote; + +@Service +public class FoodService implements IFoodService { + + @Autowired + private IDatabaseDao databaseDao; + + public final void setDatabaseDao(IDatabaseDao databaseDao) { + this.databaseDao = databaseDao; + } + + @Override + @Authenticated + public void vote(String menu) { + this.databaseDao.vote(menu); + + } + + @Override + public List<FoodVote> getFoodVote() { + return this.databaseDao.getFoodVote(); + } + + @Override + public int getFoodVoteCount() { + return this.databaseDao.getFoodVoteCount(); + } +} diff --git a/src/main/java/de/thm/arsnova/services/IFoodService.java b/src/main/java/de/thm/arsnova/services/IFoodService.java new file mode 100644 index 00000000..dbd2c1fc --- /dev/null +++ b/src/main/java/de/thm/arsnova/services/IFoodService.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2012 THM webMedia + * + * This file is part of ARSnova. + * + * ARSnova is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ARSnova is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package de.thm.arsnova.services; + +import java.util.List; + +import de.thm.arsnova.entities.FoodVote; + +public interface IFoodService { + + void vote(String menu); + + int getFoodVoteCount(); + + List<FoodVote> getFoodVote(); + +} diff --git a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java index ef84563d..f3d73eb6 100644 --- a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java +++ b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java @@ -10,6 +10,7 @@ import org.springframework.stereotype.Component; import de.thm.arsnova.entities.Answer; import de.thm.arsnova.entities.Feedback; +import de.thm.arsnova.entities.FoodVote; import de.thm.arsnova.entities.LoggedIn; import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.Session; @@ -263,5 +264,23 @@ public class StubDatabaseDao implements IDatabaseDao { // TODO Auto-generated method stub return null; } + + @Override + public void vote(String menu) { + // TODO Auto-generated method stub + + } + + @Override + public int getFoodVoteCount() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public List<FoodVote> getFoodVote() { + // TODO Auto-generated method stub + return null; + } } -- GitLab