diff --git a/src/scheduler/index.ts b/src/scheduler/index.ts index 66b30b4..0937603 100644 --- a/src/scheduler/index.ts +++ b/src/scheduler/index.ts @@ -34,7 +34,7 @@ var ngMessages = require('angular-messages'), ngStrap = require('angular-strap'); import { ScheduleController } from './schedule.controller'; -import { ScheduleFactory } from './schedule.factory'; +import { ScheduleService } from './schedule.service'; export default angular.module('accountant.scheduler', [ ngMessages, @@ -47,7 +47,7 @@ export default angular.module('accountant.scheduler', [ .factory('logger', downgradeInjectable(Logger)) - .factory('ScheduledOperation', ScheduleFactory) + .factory('scheduleService', downgradeInjectable(ScheduleService)) .controller('SchedulerController', ScheduleController) diff --git a/src/scheduler/schedule.controller.ts b/src/scheduler/schedule.controller.ts index 357ff74..37e19a2 100644 --- a/src/scheduler/schedule.controller.ts +++ b/src/scheduler/schedule.controller.ts @@ -1,7 +1,12 @@ +import { Observable } from 'rxjs/Rx'; + import { Logger } from '@nsalaun/ng-logger'; import { NgbModal } from '@ng-bootstrap/ng-bootstrap'; import { ToastrService } from 'ngx-toastr'; +import { ScheduleService } from './schedule.service'; +import { Schedule } from './schedule'; + var scheduleFormTmpl = require('./schedule.form.tmpl.html'), scheduleDeleteTmpl = require('./schedule.delete.tmpl.html'); @@ -11,41 +16,50 @@ export class ScheduleController{ constructor( private $stateParams, private toastrService: ToastrService, - private ScheduledOperation, + private scheduleService: ScheduleService, private $modal ) { // Load operations on controller initialization. - this.operations = this.load(); + this.load(); } /* * Add a new operation at the beginning of th array. */ add = function() { - var operation = new this.ScheduledOperation({ - account_id: this.$stateParams.accountId - }); + var schedule = new Schedule(); + schedule.account_id = this.$stateParams.accountId; - return this.modify(operation); + return this.modify(schedule); }; /* * Load operations. */ load = function() { - return this.ScheduledOperation.query({ - account_id: this.$stateParams.accountId - }); + return this.scheduleService.query(this.$stateParams.accountId) + .subscribe((schedules: Schedule[]) => { + this.operations = schedules; + } + ); }; /* * Save operation. */ - save = function(operation) { - return operation.$save().then((operation) => { + save = function(operation: Schedule) { + let subscription: Observable; + + if(operation.id) { + subscription = this.scheduleService.create(operation); + } else { + subscription = this.scheduleService.update(operation); + } + + return subscription.subscribe((operation: Schedule) => { this.toastrService.success('Scheduled operation #' + operation.id + ' saved.'); - this.operations = this.load(); + this.load(); return operation; }, (result) => { @@ -58,7 +72,7 @@ export class ScheduleController{ /* * Delete an operation and return a promise. */ - confirmDelete = function(operation) { + confirmDelete = function(operation: Schedule) { var title = "Delete operation #" + operation.id; this.$modal({ @@ -84,13 +98,13 @@ export class ScheduleController{ /* * Delete operation. */ - delete = function(operation) { + delete = function(operation: Schedule) { var id = operation.id; - return operation.$delete().then(() => { + return this.scheduleService.delete(operation).subscribe(() => { this.toastrService.success('Scheduled operation #' + id + ' deleted.'); - this.operations = this.load(); + this.load(); return operation; }, (result) => { @@ -105,7 +119,7 @@ export class ScheduleController{ * Open the popup to modify the operation, save it on confirm. * @returns a promise. */ - modify = function(operation) { + modify = function(operation: Schedule) { // FIXME Alexis Lahouze 2017-06-15 i18n var title = "Operation";