-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added a new service (with basic unit tests) that wraps ngResoruce. Intended to replace all access to to ngResource. - Add Bookmark service. Configured so it automatically downloads application bookmarks for user on load. - Enhanced UserProfile service so that it utilises a promise chain that is able to be hooked into from other services (like Bookmark)
- Loading branch information
Showing
10 changed files
with
292 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
angular.module("bawApp.services.resource", ["ngResource"]) | ||
.factory("bawResource", ["$resource", function ($resource) { | ||
|
||
/** | ||
* | ||
* @param uri | ||
* @returns {*} | ||
*/ | ||
function uriConvert(uri) { | ||
// find all place holders in this form: '{identifier}' | ||
// replace with placeholder in this form: ':identifier' | ||
return uri.replace(/(\{([^{}]*)\})/g, ":$2"); | ||
} | ||
|
||
/** | ||
* @name bawResource | ||
* Helper method for adding a put request onto the standard angular resource service | ||
* @param {string} path - the web server path | ||
* @param {Object} paramDefaults - the default parameters | ||
* @param {Object} [actions] - a set of actions to also add (extend) | ||
* @return {*} | ||
*/ | ||
var bawResource = function resourcePut(path, paramDefaults, actions) { | ||
path = uriConvert(path); | ||
|
||
var a = actions || {}; | ||
a.update = a.update || { method: 'PUT' }; | ||
var resource = $resource(path, paramDefaults, a); | ||
|
||
resource.modifiedPath = path; | ||
|
||
return resource; | ||
}; | ||
|
||
return bawResource; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
describe("The bawResource service", function () { | ||
|
||
var Bookmark; | ||
|
||
beforeEach(module('bawApp.services')); | ||
|
||
beforeEach(inject(["Bookmark", function (providedBookmark) { | ||
Bookmark = providedBookmark; | ||
}])); | ||
|
||
|
||
it("should return a resource constructor that includes update/put", function () { | ||
|
||
expect(Bookmark).toImplement({ | ||
"get": null, | ||
"save": null, | ||
"query": null, | ||
"remove": null, | ||
"delete": null, | ||
"update": null, | ||
"modifiedPath": null | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
var bawss = bawss || angular.module("bawApp.services", ['bawApp.services.resource', 'bawApp.configuration']); | ||
|
||
|
||
bawss.factory('Bookmark', [ | ||
'bawResource', | ||
'conf.paths', | ||
'conf.constants', | ||
'UserProfile', | ||
'$q', | ||
function (bawResource, paths, constants, UserProfile, $q) { | ||
var bc = constants.bookmark; | ||
|
||
// valid query options: category | ||
// required parameters: userId | ||
// optional parameters: bookmarkId | ||
|
||
// at the moment we only support bookmark modification for users (not for recordings) | ||
var resource = bawResource(paths.api.routes.bookmark.showAbsolute, {}); | ||
|
||
// retrieve or set the playback bookmark | ||
resource.applicationBookmarks = {}; | ||
function getApplicationBookmarks(userProfile) { | ||
console.info("User profile hook success, retrieving app bookmarks", arguments); | ||
var deferred = $q.defer(); | ||
|
||
resource.query({ | ||
category: bc.appCategory, | ||
userId: userProfile.id | ||
}, | ||
function appBookmarksQuerySuccess(values, headers) { | ||
console.info("Application bookmarks received", values); | ||
|
||
// transform into associative hash | ||
values.forEach(function (value, index) { | ||
resource.applicationBookmarks[value.name] = value; | ||
}); | ||
|
||
deferred.resolve(values); | ||
}, | ||
function appBookmarksQueryFailure() { | ||
console.error("Retrieving application bookmarks failed"); | ||
|
||
deferred.reject(); | ||
}); | ||
|
||
return deferred.promise; | ||
} | ||
|
||
resource.applicationBookmarksPromise = UserProfile.get.then( | ||
getApplicationBookmarks, | ||
function () { | ||
console.error("user profile hook failure", arguments); | ||
}); | ||
|
||
|
||
resource.savePlaybackPosition = function savePlaybackPosition(recordingId, offset) { | ||
var bookmark = resource.applicationBookmarks[bc.lastPlaybackPositionName]; | ||
if (bookmark) { | ||
// update | ||
bookmark.offsetSeconds = offset; | ||
bookmark.audioRecordingId = recordingId; | ||
|
||
} | ||
else { | ||
// create | ||
bookmark = { | ||
name: bc.lastPlaybackPositionName, | ||
category: bc.appCategory, | ||
offsetSeconds: offset, | ||
audioRecordingId: recordingId | ||
}; | ||
|
||
|
||
} | ||
}; | ||
|
||
|
||
return resource; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
describe("The bookmark service", function () { | ||
|
||
var bawResource; | ||
|
||
beforeEach(module('bawApp.services')); | ||
|
||
beforeEach(inject(["Bookmark", function (providedBawResource) { | ||
bawResource = providedBawResource; | ||
}])); | ||
|
||
|
||
it("will return a promise for retrieving application bookmarks", function() { | ||
|
||
expect(bawResource.applicationBookmarksPromise).toImplement({ | ||
catch: null, | ||
finally: null, | ||
then: null | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.