Write an Angular service for these Rails routes using Restangular
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Some routes from my Rails application are pasted below.

I am looking for a cleanly written & commented Angular example code for:

(1) a single Service to wrap all of these endpoints,
(2) an example function for each endpoint to use the routes from within an Angular controller.

Code should include error handling.

Also cater for passing query params to the route

Angular 1.2 so be careful with the changes in promises.

Bonus $50 for the same example in 2 more forms: using $http and using $resource (Angular 1.2)

namespace :api, defaults: {format: :json} do
resources :batches, only: [:index, :create, :update, :destroy, :show] do
resources :batch_details, only: [:index, :create, :update, :destroy, :show]
post 'api/batches/spreadsheet_upload' => 'api/batches#spreadsheet_upload'
post 'api/batches/:id/pipeline' => 'api/batches#pipeline'
post 'api/batches/mass_delete' => 'api/batches#mass_delete'

api_batches_mass_delete POST /api/batches/mass_delete(.:format) api/batches#mass_delete
api_batches_assay_table GET /api/batches/assay_table(.:format) api/batches#assay_table
worksheet_export GET /api/batches/:id/worksheet_export(.:format) api/batches#worksheet_export
api_batch_batch_details GET /api/batches/:batch_id/batch_details(.:format) api/batch_details#index {:format=>:json}
POST /api/batches/:batch_id/batch_details(.:format) api/batch_details#create {:format=>:json}
api_batch_batch_detail GET /api/batches/:batch_id/batch_details/:id(.:format) api/batch_details#show {:format=>:json}
PATCH /api/batches/:batch_id/batch_details/:id(.:format) api/batch_details#update {:format=>:json}
PUT /api/batches/:batch_id/batch_details/:id(.:format) api/batch_details#update {:format=>:json}
DELETE /api/batches/:batch_id/batch_details/:id(.:format) api/batch_details#destroy {:format=>:json}
api_batches GET /api/batches(.:format) api/batches#index {:format=>:json}
POST /api/batches(.:format) api/batches#create {:format=>:json}
api_batch GET /api/batches/:id(.:format) api/batches#show {:format=>:json}
PATCH /api/batches/:id(.:format) api/batches#update {:format=>:json}
PUT /api/batches/:id(.:format) api/batches#update {:format=>:json}
DELETE /api/batches/:id(.:format) api/batches#destroy {:format=>:json}
POST /api/batches/:id/pipeline(.:format) api/batches#pipeline


I have already written these services, but please improve and correct bugs.

angular.module('assaypipelineApp').factory('Batch', ['$resource', function($resource) {
function Batch() {
this.service = $resource('/api/batches/:batch_id', {batch_id: '@id'}, {'update': { method: 'PUT' }});
Batch.prototype.index = function() {
return this.service.query();
Batch.prototype.delete = function(batchID) {
return this.service.remove({batch_id: batchID});
Batch.prototype.create = function(attr) {
return this.service.save(attr);
Batch.prototype.update = function(attr) {
return this.service.update(attr);
Batch.prototype.pipeline = function(batchID) {
return $resource('/api/batches/pipeline', {batch_id: '@id'}).get({batch_id: batchID});
Batch.prototype.duplicate = function(batchID) {
return $resource('/api/batches/:batch_id', {batch_id: '@id'}).get({duplicate: batchID});
return new Batch;

angular.module('assaypipelineApp').factory('BatchDetail', ['$resource', function($resource) {
function BatchDetail() {
this.service = $resource('/api/batches/:batch_id/batch_detail/:id', {batch_id: '@batchID', id: '@id'}, {'update': { method: 'PUT' }});
BatchDetail.prototype.index = function() {
return this.service.query();
BatchDetail.prototype.delete = function(batchID, id) {
return this.service.remove({batch_id: batchID});
BatchDetail.prototype.create = function(attr) {
return this.service.save(attr);
BatchDetail.prototype.update = function(attr) {
return this.service.update(attr);
return new BatchDetail;

Please note the answer should be in Javascript, not Coffeescript.
kusadasi over 7 years ago
awarded to Wikimedia

Crowdsource coding tasks.

0 Solutions