@DELETE @Path("/feeds/{id}") public Response remove(@PathParam("id") Long id) { RssFeed originRssFeed = dao.getById(id); if (originRssFeed == null) { System.err.println("RSS item with such ID is not found."); throw new FeedNotFoundException("RSS item with such ID is not found."); } dao.remove(id); return Response.ok().build(); }
@POST @Path("/feeds") @Consumes(MediaType.APPLICATION_JSON) public Response add(RssFeed rssFeed) { if (dao.getByLink(rssFeed.getLink()) == null) { dao.add(rssFeed); URI location = URI.create("/rss/feeds" + rssFeed.getId()); return Response.created(location).build(); } else { FeedInfo feedInfo = new FeedInfo(); feedInfo.setInfo(rssFeed); System.err.println("Such feed is already in the DB"); throw new DataBaseFeedException("Such feed is already in the DB", feedInfo); } }
@PUT @Consumes(MediaType.APPLICATION_JSON) @Path("/feeds/{id}") public Response update(@PathParam("id") Long id, RssFeed rssFeed) { RssFeed originRssFeed = dao.getById(id); if (originRssFeed == null) { System.err.println("Nothing to update: no such element by this ID."); throw new FeedNotFoundException("Nothing to update: no such element by this ID."); } else { System.err.print("RSS Feed was found."); originRssFeed.setName(rssFeed.getName()); originRssFeed.setLink(rssFeed.getLink()); dao.update(originRssFeed); System.err.println("Updated."); return Response.ok().build(); } }
@POST @Path("/feeds/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response upload(@FormDataParam("file") InputStream file) { FeedParser parser = new FeedParser(); List<RssFeed> feeds = parser.parseFeeds(file); if (feeds == null) throw new MultiPartQueryException("Unable to read sources from document."); feeds .stream() .forEach( feed -> { if (dao.getByLink(feed.getLink()) == null) { dao.add(feed); } else throw new DataBaseFeedException("Some feeds are already in the database."); }); return Response.status(Response.Status.OK).build(); }
@GET @Path("/feeds/{feed_id}/items/{item_id}") public ItemInfo getBySource( @PathParam("feed_id") Long feed_id, @PathParam("item_id") Long item_id) { RssItem item = dao.getBySource(feed_id, item_id); if (item == null) throw new ItemsNotFoundException("Item not found."); ItemInfo information = new ItemInfo(); information.setInfo(item); return information; }
@GET @Path("/feeds") public List<FeedInfo> getFeeds( @QueryParam("page") Integer page, @QueryParam("pageSize") Integer pageSize, @QueryParam("name") String name) { List<RssFeed> feeds = dao.getFeeds(page, pageSize, name); if (feeds.size() == 0) throw new NoFeedsFoundException("No feeds were found in DB."); FeedInfo feedInfo = new FeedInfo(); List<FeedInfo> information = feedInfo.setFeedListInfo(feeds); return information; }