Skip to content
Snippets Groups Projects
Commit cf5b8caf authored by Julian Hochstetter's avatar Julian Hochstetter
Browse files

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
parent 31a2613b
Branches
Tags
No related merge requests found
/*
* 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();
}
}
...@@ -22,9 +22,11 @@ package de.thm.arsnova.dao; ...@@ -22,9 +22,11 @@ package de.thm.arsnova.dao;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
...@@ -54,6 +56,7 @@ import com.fourspaces.couchdb.ViewResults; ...@@ -54,6 +56,7 @@ import com.fourspaces.couchdb.ViewResults;
import de.thm.arsnova.entities.Answer; import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.FoodVote;
import de.thm.arsnova.entities.LoggedIn; import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.PossibleAnswer; import de.thm.arsnova.entities.PossibleAnswer;
import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.Question;
...@@ -1031,4 +1034,81 @@ public class CouchDBDao implements IDatabaseDao { ...@@ -1031,4 +1034,81 @@ public class CouchDBDao implements IDatabaseDao {
} }
return null; 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;
}
} }
...@@ -23,6 +23,7 @@ import java.util.List; ...@@ -23,6 +23,7 @@ import java.util.List;
import de.thm.arsnova.entities.Answer; import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.FoodVote;
import de.thm.arsnova.entities.LoggedIn; import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.Session;
...@@ -82,4 +83,10 @@ public interface IDatabaseDao { ...@@ -82,4 +83,10 @@ public interface IDatabaseDao {
int getInterposedCount(String sessionKey); int getInterposedCount(String sessionKey);
List<Question> getInterposedQuestions(String sessionKey); List<Question> getInterposedQuestions(String sessionKey);
void vote(String menu);
int getFoodVoteCount();
List<FoodVote> getFoodVote();
} }
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;
}
}
/*
* 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();
}
}
/*
* 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();
}
...@@ -10,6 +10,7 @@ import org.springframework.stereotype.Component; ...@@ -10,6 +10,7 @@ import org.springframework.stereotype.Component;
import de.thm.arsnova.entities.Answer; import de.thm.arsnova.entities.Answer;
import de.thm.arsnova.entities.Feedback; import de.thm.arsnova.entities.Feedback;
import de.thm.arsnova.entities.FoodVote;
import de.thm.arsnova.entities.LoggedIn; import de.thm.arsnova.entities.LoggedIn;
import de.thm.arsnova.entities.Question; import de.thm.arsnova.entities.Question;
import de.thm.arsnova.entities.Session; import de.thm.arsnova.entities.Session;
...@@ -263,5 +264,23 @@ public class StubDatabaseDao implements IDatabaseDao { ...@@ -263,5 +264,23 @@ public class StubDatabaseDao implements IDatabaseDao {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; 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;
}
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment