diff options
-rw-r--r-- | js/build/app.js | 19 | ||||
-rw-r--r-- | js/service/ItemResource.js | 47 | ||||
-rw-r--r-- | js/service/Resource.js | 9 | ||||
-rw-r--r-- | js/tests/unit/service/ItemResourceSpec.js | 99 |
4 files changed, 163 insertions, 11 deletions
diff --git a/js/build/app.js b/js/build/app.js index eb88e72dc..ba5391389 100644 --- a/js/build/app.js +++ b/js/build/app.js @@ -273,18 +273,27 @@ var $__build_47_app__ = function () { markRead: function (itemId) { var read = arguments[1] !== void 0 ? arguments[1] : true; this.get(itemId).unread = !read; + return this.http({ + url: this.BASE_URL + '/items/' + itemId + '/read', + method: 'POST', + data: { isRead: read } + }); }, markFeedRead: function (feedId) { - for (var $item in this.values.filter(function (i) { - return i.feedId === feedId; - })) { + var read = arguments[1] !== void 0 ? arguments[1] : true; + for (var $__3 = this.values.filter(function (i) { + return i.feedId === feedId; + })[$traceurRuntime.toProperty(Symbol.iterator)](), $__4; !($__4 = $__3.next()).done;) { try { throw undefined; } catch (item) { - item = $item; - this.markRead(item); + item = $__4.value; + { + item.unread = !read; + } } } + return this.http.post(this.BASE_URL + '/feeds/' + feedId + '/read'); } }, {}, Resource); return new ItemResource($http, BASE_URL); diff --git a/js/service/ItemResource.js b/js/service/ItemResource.js index e6a8e1d2d..33f3efc52 100644 --- a/js/service/ItemResource.js +++ b/js/service/ItemResource.js @@ -12,10 +12,12 @@ app.factory('ItemResource', (Resource, $http, BASE_URL) => { class ItemResource extends Resource { + constructor ($http, BASE_URL) { super($http, BASE_URL); } + receive (value, channel) { switch (channel) { @@ -31,25 +33,60 @@ app.factory('ItemResource', (Resource, $http, BASE_URL) => { } } + getNewestItemId () { return this.newestItemId; } + getStarredCount () { return this.starredCount; } - markRead (itemId, read=true) { + + star (itemId, star=true) { + let item = this.get(itemId); + let base = this.BASE_URL; + let url = `${base}/items/${item.feedId}/${item.guidHash}/star`; + + item.starred = star; + + return this.http({ + url: url, + method: 'POST', + data: { + isStarred: star + } + }); + } + + + read (itemId, read=true) { this.get(itemId).unread = !read; - //http.get(); + return this.http({ + url: `${this.BASE_URL}/items/${itemId}/read`, + method: 'POST', + data: { + isRead: read + } + }); } - markFeedRead (feedId) { - for (let item in this.values.filter(i => i.feedId === feedId)) { - this.markRead(item); + + keepUnread (itemId) { + this.get(itemId).keepUnread = true; + return this.read(itemId, false); + } + + + readFeed (feedId, read=true) { + for (let item of this.values.filter(i => i.feedId === feedId)) { + item.unread = !read; } + return this.http.post(`${this.BASE_URL}/feeds/${feedId}/read`); } + } return new ItemResource($http, BASE_URL); diff --git a/js/service/Resource.js b/js/service/Resource.js index 7683099da..072abca18 100644 --- a/js/service/Resource.js +++ b/js/service/Resource.js @@ -12,6 +12,7 @@ app.factory('Resource', () => { class Resource { + constructor (http, BASE_URL, id='id') { this.id = id; this.values = []; @@ -20,12 +21,14 @@ app.factory('Resource', () => { this.BASE_URL = BASE_URL; } + receive (objs) { for (let obj of objs) { this.add(obj); } } + add (obj) { let existing = this.hashMap[obj[this.id]]; @@ -40,14 +43,17 @@ app.factory('Resource', () => { } } + size () { return this.values.length; } + get (id) { return this.hashMap[id]; } + delete (id) { // find index of object that should be deleted let deleteAtIndex = this.values.findIndex(e => e[this.id] === id); @@ -61,6 +67,7 @@ app.factory('Resource', () => { } } + clear () { this.hashMap = {}; @@ -72,10 +79,12 @@ app.factory('Resource', () => { } } + getAll () { return this.values; } + } return Resource; diff --git a/js/tests/unit/service/ItemResourceSpec.js b/js/tests/unit/service/ItemResourceSpec.js index 8d0f1864e..49989472f 100644 --- a/js/tests/unit/service/ItemResourceSpec.js +++ b/js/tests/unit/service/ItemResourceSpec.js @@ -10,10 +10,16 @@ describe('ItemResource', () => { 'use strict'; + let http; + beforeEach(module('News', ($provide) => { $provide.value('BASE_URL', 'base'); })); + beforeEach(inject(($httpBackend) => { + http = $httpBackend; + })); + it('should receive the newestItemId', inject((ItemResource) => { ItemResource.receive(3, 'newestItemId'); @@ -43,7 +49,83 @@ describe('ItemResource', () => { })); + it ('should keep item unread', inject((ItemResource) => { + http.expectPOST('base/items/3/read', {isRead: false}).respond(200, {}); + + ItemResource.receive([ + { + id: 3, + feedId: 4, + unread: false + }, + { + id: 4, + feedId: 3, + unread: false + } + ], 'items'); + + ItemResource.keepUnread(3); + + http.flush(); + + expect(ItemResource.get(3).keepUnread).toBe(true); + expect(ItemResource.get(3).unread).toBe(true); + })); + + it ('should mark item as read', inject((ItemResource) => { + http.expectPOST('base/items/3/read', {isRead: true}).respond(200, {}); + + ItemResource.receive([ + { + id: 3, + feedId: 4, + unread: true + }, + { + id: 4, + feedId: 3, + unread: true + } + ], 'items'); + + ItemResource.read(3); + + http.flush(); + + expect(ItemResource.get(3).unread).toBe(false); + })); + + + it ('should star item', inject((ItemResource) => { + http.expectPOST('base/items/4/a/star', {isStarred: true}).respond(200, {}); + + ItemResource.receive([ + { + id: 3, + feedId: 4, + starred: false, + guidHash: 'a' + }, + { + id: 4, + feedId: 3, + starred: false + } + ], 'items'); + + ItemResource.star(3); + + http.flush(); + + expect(ItemResource.get(3).starred).toBe(true); + })); + + + it ('should mark feed as read', inject((ItemResource) => { + http.expectPOST('base/feeds/4/read').respond(200, {}); + ItemResource.receive([ { id: 3, @@ -54,12 +136,27 @@ describe('ItemResource', () => { id: 4, feedId: 3, unread: true + }, + { + id: 5, + feedId: 4, + unread: true } ], 'items'); - ItemResource.markRead(3); + ItemResource.readFeed(4); + + http.flush(); expect(ItemResource.get(3).unread).toBe(false); + expect(ItemResource.get(5).unread).toBe(false); })); + + afterEach(() => { + http.verifyNoOutstandingExpectation(); + http.verifyNoOutstandingRequest(); + }); + + });
\ No newline at end of file |