Skip to content

Commit

Permalink
MemoriesDao: класс статистики
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Dec 2, 2014
1 parent c59afa9 commit 169e80b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/main/java/ru/org/linux/topic/TopicPrepareService.java
Expand Up @@ -420,7 +420,7 @@ public TopicMenu getTopicMenu(
int favsId;
boolean deletable;

List<Integer> topicStats = memoriesDao.getTopicStats(message.getMessage().getId());
MemoriesStat topicStats = memoriesDao.getTopicStats(message.getMessage().getId());

if (currentUser!=null) {
resolvable = (currentUser.isModerator() || (message.getAuthor().getId()==currentUser.getId())) &&
Expand Down Expand Up @@ -453,8 +453,8 @@ public TopicMenu getTopicMenu(
resolvable,
memoriesId,
favsId,
topicStats.get(0),
topicStats.get(1),
topicStats.watchCount(),
topicStats.favsCount(),
topicPermissionService.isCommentsAllowed(message.getGroup(), message.getMessage(), currentUser),
deletable,
userpic
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ru/org/linux/user/MemoriesController.java
Expand Up @@ -62,7 +62,7 @@ public Map<String, Integer> add(

int id = memoriesDao.addToMemories(user, topic, watch);

int count = memoriesDao.getTopicStats(msgid).get(watch?0:1);
int count = watch?memoriesDao.getTopicStats(msgid).watchCount():memoriesDao.getTopicStats(msgid).favsCount();

return ImmutableMap.of("id", id, "count", count);
}
Expand Down Expand Up @@ -92,7 +92,7 @@ public int remove(

memoriesDao.delete(id);

return memoriesDao.getTopicStats(m.getTopic()).get(m.isWatch()?0:1);
return m.isWatch()?memoriesDao.getTopicStats(m.getTopic()).watchCount():memoriesDao.getTopicStats(m.getTopic()).favsCount();
} else {
return -1;
}
Expand Down
27 changes: 3 additions & 24 deletions src/main/java/ru/org/linux/user/MemoriesDao.java
Expand Up @@ -68,10 +68,6 @@ private int doAddToMemories(User user, Topic topic, boolean watch) {

/**
* Get memories id or 0 if not in memories
*
* @param user
* @param topic
* @return
*/
public int getId(User user, Topic topic, boolean watch) {
List<Integer> res = jdbcTemplate.queryForList(
Expand All @@ -91,9 +87,8 @@ public int getId(User user, Topic topic, boolean watch) {

/**
* get number of memories/favs for topic
* @return list(0) - memories, list(1) - favs
*/
public List<Integer> getTopicStats(int topic) {
public MemoriesStat getTopicStats(int topic) {
final List<Integer> res = Lists.newArrayList(0, 0);

jdbcTemplate.query(
Expand All @@ -111,8 +106,7 @@ public void processRow(ResultSet rs) throws SQLException {
topic
);

return res;

return new MemoriesStat(res.get(0), res.get(1));
}

public MemoriesListItem getMemoriesListItem(int id) {
Expand Down Expand Up @@ -141,7 +135,6 @@ public boolean isFavPresetForUser(User user) {
return checkMemoriesPresent(user, false);
}


private boolean checkMemoriesPresent(User user, boolean watch) {
List<Integer> present = jdbcTemplate.queryForList(
"select memories.id from memories join topics on memories.topic=topics.id where memories.userid=? and watch=? and not deleted limit 1;",
Expand All @@ -160,21 +153,7 @@ private boolean checkMemoriesPresent(User user, boolean watch) {
*/
public int getWatchCountForUser(User user) {
List<Integer> ret = jdbcTemplate.queryForList("select count(id) from memories where userid=? and watch='t'", Integer.class, user.getId());
if(ret == null || ret.isEmpty()) {
return 0;
} else {
return ret.get(0);
}
}

/**
* get number of favorite memories for user
* @param user user
* @return count memories
*/
public int getFavCountForUser(User user) {
List<Integer> ret = jdbcTemplate.queryForList("select count(id) from memories where userid=? and watch='f'", Integer.class, user.getId());
if(ret == null || ret.size() == 0) {
if(ret.isEmpty()) {
return 0;
} else {
return ret.get(0);
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/ru/org/linux/user/MemoriesStat.scala
@@ -0,0 +1,3 @@
package ru.org.linux.user

case class MemoriesStat(watchCount:Int, favsCount:Int)

0 comments on commit 169e80b

Please sign in to comment.