cleaner rest api

This commit is contained in:
Athou
2013-04-16 09:30:34 +02:00
parent b6d8072090
commit 6c544237b2
2 changed files with 120 additions and 96 deletions

View File

@@ -8,7 +8,6 @@ import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
@@ -28,7 +27,7 @@ import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam; import com.wordnik.swagger.annotations.ApiParam;
@Path("/entries/") @Path("/entries/")
@Api(value = "/entries", description = "Operations about entries") @Api(value = "/entries", description = "Operations about feed entries")
public class EntriesREST extends AbstractREST { public class EntriesREST extends AbstractREST {
public static final String ALL = "all"; public static final String ALL = "all";
@@ -41,47 +40,61 @@ public class EntriesREST extends AbstractREST {
all, unread; all, unread;
} }
@Path("/get") @Path("/feed/get")
@GET @GET
@ApiOperation(value = "Get entries", notes = "Get a list of entries matching the query", responseClass = "com.commafeed.frontend.model.Entries") @ApiOperation(value = "Get feed entries", notes = "Get a list of feed entries", responseClass = "com.commafeed.frontend.model.Entries")
public Entries getEntries( public Entries getFeedEntries(
@ApiParam(value = "Type of query", allowableValues = "category,feed", required = true) @QueryParam("type") Type type, @ApiParam(value = "id of the feed", required = true) @QueryParam("id") String id,
@ApiParam(value = "ID of the category or the feed", required = true) @QueryParam("id") String id, @ApiParam(value = "all entries or only unread ones", allowableValues = "all,unread", required = true) @QueryParam("readType") ReadType readType,
@ApiParam(value = "All entries or only unread ones", allowableValues = "all,unread", required = true) @QueryParam("readType") ReadType readType, @ApiParam(value = "offset for paging") @DefaultValue("0") @QueryParam("offset") int offset,
@ApiParam(value = "Offset for paging") @DefaultValue("0") @QueryParam("offset") int offset, @ApiParam(value = "limit for paging") @DefaultValue("-1") @QueryParam("limit") int limit,
@ApiParam(value = "Limit for paging") @DefaultValue("-1") @QueryParam("limit") int limit, @ApiParam(value = "entry date ordering", allowableValues = "asc,desc") @QueryParam("order") @DefaultValue("desc") ReadingOrder order) {
@ApiParam(value = "Ordering", allowableValues = "asc,desc") @QueryParam("order") @DefaultValue("desc") ReadingOrder order) {
Preconditions.checkNotNull(type);
Preconditions.checkNotNull(id); Preconditions.checkNotNull(id);
Preconditions.checkNotNull(readType); Preconditions.checkNotNull(readType);
Entries entries = new Entries(); Entries entries = new Entries();
boolean unreadOnly = readType == ReadType.unread; boolean unreadOnly = readType == ReadType.unread;
if (type == Type.feed) { FeedSubscription subscription = feedSubscriptionDAO.findById(getUser(),
FeedSubscription subscription = feedSubscriptionDAO.findById( Long.valueOf(id));
getUser(), Long.valueOf(id));
if (subscription != null) { if (subscription != null) {
entries.setName(subscription.getTitle()); entries.setName(subscription.getTitle());
entries.setMessage(subscription.getFeed().getMessage()); entries.setMessage(subscription.getFeed().getMessage());
entries.setErrorCount(subscription.getFeed().getErrorCount()); entries.setErrorCount(subscription.getFeed().getErrorCount());
List<FeedEntryStatus> unreadEntries = feedEntryStatusDAO List<FeedEntryStatus> unreadEntries = feedEntryStatusDAO
.findByFeed(subscription.getFeed(), getUser(), .findByFeed(subscription.getFeed(), getUser(), unreadOnly,
unreadOnly, offset, limit, order, true); offset, limit, order, true);
for (FeedEntryStatus status : unreadEntries) { for (FeedEntryStatus status : unreadEntries) {
entries.getEntries().add(buildEntry(status)); entries.getEntries().add(buildEntry(status));
} }
} }
} else { entries.setTimestamp(Calendar.getInstance().getTimeInMillis());
return entries;
}
@Path("/category/get")
@GET
@ApiOperation(value = "Get category entries", notes = "Get a list of category entries", responseClass = "com.commafeed.frontend.model.Entries")
public Entries getCategoryEntries(
@ApiParam(value = "id of the category, or 'all'", required = true) @QueryParam("id") String id,
@ApiParam(value = "all entries or only unread ones", allowableValues = "all,unread", required = true) @QueryParam("readType") ReadType readType,
@ApiParam(value = "offset for paging") @DefaultValue("0") @QueryParam("offset") int offset,
@ApiParam(value = "limit for paging") @DefaultValue("-1") @QueryParam("limit") int limit,
@ApiParam(value = "entry date ordering", allowableValues = "asc,desc") @QueryParam("order") @DefaultValue("desc") ReadingOrder order) {
Preconditions.checkNotNull(id);
Preconditions.checkNotNull(readType);
Entries entries = new Entries();
boolean unreadOnly = readType == ReadType.unread;
if (ALL.equals(id)) { if (ALL.equals(id)) {
entries.setName("All"); entries.setName("All");
List<FeedEntryStatus> unreadEntries = feedEntryStatusDAO List<FeedEntryStatus> unreadEntries = feedEntryStatusDAO.findAll(
.findAll(getUser(), unreadOnly, offset, limit, order, getUser(), unreadOnly, offset, limit, order, true);
true);
for (FeedEntryStatus status : unreadEntries) { for (FeedEntryStatus status : unreadEntries) {
entries.getEntries().add(buildEntry(status)); entries.getEntries().add(buildEntry(status));
} }
@@ -100,7 +113,6 @@ public class EntriesREST extends AbstractREST {
} }
entries.setName(feedCategory.getName()); entries.setName(feedCategory.getName());
} }
}
} }
entries.setTimestamp(Calendar.getInstance().getTimeInMillis()); entries.setTimestamp(Calendar.getInstance().getTimeInMillis());
@@ -129,35 +141,53 @@ public class EntriesREST extends AbstractREST {
return entry; return entry;
} }
@Path("mark") @Path("/entry/mark")
@GET @GET
public Response mark(@QueryParam("type") Type type, @ApiOperation(value = "Mark a feed entry", notes = "Mark a feed entry as read/unread")
@QueryParam("id") String id, @QueryParam("read") boolean read, public Response markFeedEntry(
@QueryParam("olderThan") Long olderThanTimestamp) { @ApiParam(value = "entry id", required = true) @QueryParam("id") String id,
Preconditions.checkNotNull(type); @ApiParam(value = "read status", required = true) @QueryParam("read") boolean read) {
Preconditions.checkNotNull(id); Preconditions.checkNotNull(id);
Preconditions.checkNotNull(read); Preconditions.checkNotNull(read);
Date olderThan = olderThanTimestamp == null ? null : new Date(
olderThanTimestamp);
if (type == Type.entry) {
FeedEntryStatus status = feedEntryStatusDAO.findById(getUser(), FeedEntryStatus status = feedEntryStatusDAO.findById(getUser(),
Long.valueOf(id)); Long.valueOf(id));
status.setRead(read); status.setRead(read);
feedEntryStatusDAO.update(status); feedEntryStatusDAO.update(status);
} else if (type == Type.feed) {
if (read) { return Response.ok(Status.OK).build();
FeedSubscription subscription = feedSubscriptionDAO.findById(
getUser(), Long.valueOf(id));
feedEntryStatusDAO.markFeedEntries(getUser(),
subscription.getFeed(), olderThan);
} else {
throw new WebApplicationException(Response.status(
Status.INTERNAL_SERVER_ERROR).build());
} }
} else if (type == Type.category) {
if (read) { @Path("/feed/mark")
@GET
@ApiOperation(value = "Mark feed entries", notes = "Mark feed entries as read/unread")
public Response markFeedEntries(
@ApiParam(value = "feed id", required = true) @QueryParam("id") String id,
@ApiParam(value = "only entries older than this, prevent marking an entry that was not retrieved") @QueryParam("olderThan") Long olderThanTimestamp) {
Preconditions.checkNotNull(id);
Date olderThan = olderThanTimestamp == null ? null : new Date(
olderThanTimestamp);
FeedSubscription subscription = feedSubscriptionDAO.findById(getUser(),
Long.valueOf(id));
feedEntryStatusDAO.markFeedEntries(getUser(), subscription.getFeed(),
olderThan);
return Response.ok(Status.OK).build();
}
@Path("/category/mark")
@GET
@ApiOperation(value = "Mark feed entries", notes = "Mark feed entries as read/unread")
public Response markCategoryEntries(
@ApiParam(value = "category id, or 'all'", required = true) @QueryParam("id") String id,
@ApiParam(value = "only entries older than this, prevent marking an entry that was not retrieved") @QueryParam("olderThan") Long olderThanTimestamp) {
Preconditions.checkNotNull(id);
Date olderThan = olderThanTimestamp == null ? null : new Date(
olderThanTimestamp);
if (ALL.equals(id)) { if (ALL.equals(id)) {
feedEntryStatusDAO.markAllEntries(getUser(), olderThan); feedEntryStatusDAO.markAllEntries(getUser(), olderThan);
} else { } else {
@@ -166,22 +196,21 @@ public class EntriesREST extends AbstractREST {
getUser(), getUser(),
feedCategoryDAO.findById(getUser(), feedCategoryDAO.findById(getUser(),
Long.valueOf(id))); Long.valueOf(id)));
feedEntryStatusDAO.markCategoryEntries(getUser(), feedEntryStatusDAO.markCategoryEntries(getUser(), categories,
categories, olderThan); olderThan);
}
} else {
throw new WebApplicationException(Response.status(
Status.INTERNAL_SERVER_ERROR).build());
}
} }
return Response.ok(Status.OK).build(); return Response.ok(Status.OK).build();
} }
@Path("search") @Path("/search")
@GET @GET
public Entries searchEntries(@QueryParam("keywords") String keywords, @ApiOperation(value = "Search for entries", notes = "Look through title and content of entries by keywords")
@DefaultValue("0") @QueryParam("offset") int offset, public Entries searchEntries(
@DefaultValue("-1") @QueryParam("limit") int limit) { @ApiParam(value = "keywords separated by spaces, 3 characters minimum", required = true) @QueryParam("keywords") String keywords,
@ApiParam(value = "offset for paging") @DefaultValue("0") @QueryParam("offset") int offset,
@ApiParam(value = "limit for paging") @DefaultValue("-1") @QueryParam("limit") int limit) {
keywords = StringUtils.trimToEmpty(keywords);
Preconditions.checkArgument(StringUtils.length(keywords) >= 3); Preconditions.checkArgument(StringUtils.length(keywords) >= 3);
Entries entries = new Entries(); Entries entries = new Entries();

View File

@@ -162,15 +162,10 @@ module.factory('EntryService', function($resource, $http) {
params : { params : {
_method : 'mark' _method : 'mark'
} }
},
search : {
method : 'GET',
params : {
_method : 'search'
}
} }
}; };
var res = $resource('rest/entries/:_method', {}, actions); var res = $resource('rest/entries/:type/:_method', {}, actions);
res.search = $resource('rest/entries/search', {}, actions).get;
return res; return res;
}); });