mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
handle 'mark as read'
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user