handle 'mark as read'

This commit is contained in:
Athou
2013-03-23 17:17:27 +01:00
parent 4df1e7f722
commit 84e4751e07
8 changed files with 90 additions and 47 deletions

View File

@@ -1,10 +1,30 @@
package com.commafeed.backend.dao;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.User;
import com.commafeed.frontend.utils.ModelFactory.MF;
import com.uaihebert.factory.EasyCriteriaFactory;
import com.uaihebert.model.EasyCriteria;
@Stateless
public class FeedEntryStatusService extends GenericDAO<FeedEntryStatus, Long> {
public FeedEntryStatus getStatus(User user, FeedEntry entry) {
EasyCriteria<FeedEntryStatus> criteria = EasyCriteriaFactory
.createQueryCriteria(em, getType());
criteria.andEquals(MF.i(proxy().getUser()), user);
criteria.andEquals(MF.i(proxy().getEntry()), entry);
FeedEntryStatus status = null;
try {
criteria.getSingleResult();
} catch (NoResultException e) {
status = null;
}
return status;
}
}

View File

@@ -10,6 +10,7 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.commafeed.frontend.utils.ModelFactory.MF;
import com.google.common.reflect.TypeToken;
import com.uaihebert.factory.EasyCriteriaFactory;
import com.uaihebert.model.EasyCriteria;
@@ -88,4 +89,8 @@ public abstract class GenericDAO<T, K> implements Serializable {
return (Class<T>) type.getRawType();
}
protected T proxy() {
return MF.p(getType());
}
}

View File

@@ -5,6 +5,7 @@ import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import com.commafeed.frontend.rest.resources.AbstractREST;
import com.commafeed.frontend.rest.resources.EntriesREST;
import com.commafeed.frontend.rest.resources.SubscriptionsREST;
import com.google.common.collect.Sets;
@@ -17,6 +18,7 @@ public class RESTApplication extends Application {
Set<Class<?>> set = Sets.newHashSet();
set.add(JSONMessageBodyWriter.class);
set.add(AbstractREST.class);
set.add(SubscriptionsREST.class);
set.add(EntriesREST.class);
return set;

View File

@@ -5,8 +5,10 @@ import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.wicket.Application;
import org.apache.wicket.ThreadContext;
@@ -51,6 +53,10 @@ public abstract class AbstractREST {
ThreadContext.setRequestCycle(cycle);
Application.get().fetchCreateAndSetSession(
Application.get().createRequestCycle(swreq, swresp));
if (getUser() == null) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
}
protected User getUser() {

View File

@@ -93,4 +93,26 @@ public class EntriesREST extends AbstractREST {
return entry;
}
@Path("mark/{type}/{id}/{read}")
@GET
public void mark(@PathParam("type") String type,
@PathParam("id") String id, @PathParam("read") boolean read) {
if ("entry".equals(type)) {
FeedEntry entry = feedEntryService.findById(Long.valueOf(id));
FeedEntryStatus status = feedEntryStatusService.getStatus(
getUser(), entry);
if (status == null) {
status = new FeedEntryStatus();
status.setUser(getUser());
status.setEntry(entry);
}
status.setRead(read);
if (status.getId() == null) {
feedEntryStatusService.save(status);
} else {
feedEntryStatusService.update(status);
}
}
}
}

View File

@@ -1,8 +1,9 @@
var module = angular.module('commafeed.controllers', []);
module.run(function($rootScope) {
$rootScope.$on('emitMarkAsRead', function(event, args) {
$rootScope.$broadcast('markAsRead', args);
$rootScope.$on('emitMark', function(event, args) {
// args.entry - the entry
$rootScope.$broadcast('mark', args);
});
});
@@ -57,25 +58,25 @@ module.controller('CategoryTreeCtrl', function($scope, $routeParams, $location,
$location.path('/feeds/view/category/' + id);
};
var markAsRead = function(node, entry, read) {
var mark = function(node, entry) {
if (node.children) {
for ( var i = 0; i < node.children.length; i++) {
markAsRead(node.children[i], entry, read);
mark(node.children[i], entry);
}
}
if (node.feeds) {
for ( var i = 0; i < node.feeds.length; i++) {
var feed = node.feeds[i];
if (feed.id == entry.feedId) {
var c = read ? -1 : 1;
var c = entry.read ? -1 : 1;
feed.unread = feed.unread + c;
}
}
}
};
$scope.$on('markAsRead', function(event, args) {
markAsRead($scope.root, args.entry, args.read)
$scope.$on('mark', function(event, args) {
mark($scope.root, args.entry)
});
});
@@ -85,44 +86,22 @@ module.controller('FeedListCtrl', function($scope, $routeParams, $http,
$scope.selectedType = $routeParams._type;
$scope.selectedId = $routeParams._id;
$scope.entryList = EntryService.getUnread({
$scope.entryList = EntryService.get({
_type : $scope.selectedType,
_id : $scope.selectedId,
_readtype : 'unread'
})
$scope.entryList2 = {
name : 'aaa',
entries : [ {
id : '1',
title : 'my title',
content : 'my content',
date : 'my date',
feedId : '1',
feedName : 'my feed',
url : 'my url',
read : false,
starred : false,
}, {
id : '2',
title : 'my other title',
content : 'my other content',
date : 'my other date',
feedId : '2',
feedName : 'my other feed',
url : 'my other url',
read : false,
starred : false,
} ]
};
$scope.markAsRead = function(entry) {
var read = entry.read;
entry.read = true;
$scope.mark = function(entry, read) {
if (entry.read != read) {
$scope.$emit('emitMarkAsRead', {
entry : entry,
read : true
entry.read = read;
$scope.$emit('emitMark', {
entry : entry
});
EntryService.mark({
_type : 'entry',
_id : entry.id,
_readtype : read
});
}
};

View File

@@ -6,21 +6,30 @@ module.factory('CategoryService', [ '$resource', '$http',
'get' : {
method : 'GET'
}
}
};
res = $resource('rest/subscriptions', {}, actions);
return res
return res;
} ]);
module.factory('EntryService', [ '$resource', '$http',
module.factory('EntryService', [
'$resource',
'$http',
function($resource, $http) {
var actions = {
'getUnread' : {
'get' : {
method : 'GET',
params : {
_method : 'get'
}
},
mark : {
method : 'GET',
params : {
_method : 'mark'
}
}
}
res = $resource('rest/entries/:_method/:_type/:_id/:_readtype', {}, actions);
return res
};
res = $resource('rest/entries/:_method/:_type/:_id/:_readtype', {},
actions);
return res;
} ]);

View File

@@ -5,7 +5,7 @@
<div class="accordion-group" ng-repeat="entry in entryList.entries">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse"
ng-click="markAsRead(entry)"
ng-click="mark(entry, true)"
ng-class="{unread: entry.read == false}"
data-parent="#feed-accordion" href="{{'#feed-body' + $index}}">
<span ng-show="selectedType == 'category'">{{entry.feedName}}