Skip to content

Instantly share code, notes, and snippets.

@Epotignano
Created November 16, 2015 12:26
Show Gist options
  • Save Epotignano/ff95f66e6afe45b13f8d to your computer and use it in GitHub Desktop.
Save Epotignano/ff95f66e6afe45b13f8d to your computer and use it in GitHub Desktop.
module app.services {
interface ICoursesService {
getCollection():void;
get(courseId);
create(courseObj);
update(courseObj, courseId);
remove(courseId);
}
export class CoursesService implements ICoursesService {
private collectionKey: string;
private thread: Rx.Subject<{}>;
public filteredCollection;
/* @ngInject */
constructor(private FirebaseCRUDFactory : app.services.FirebaseCRUD,
private $firebaseArray : AngularFireArrayService,
private threadsService : app.threads.Threads) {
this.thread = new Rx.Subject<{}>();
this.collectionKey = 'courses';
this.threadsService.setThread('Course', this.thread)
}
getCollection() {
this.FirebaseCRUDFactory.getCollection(this.collectionKey)
.then((data)=> this.thread.onNext({data, 'EVENT': this.threadsService.defaultEvents['COLLECTION_LOADED']}))
.catch((error)=> this.thread.onError({error, 'EVENT': this.threadsService.defaultEvents['COLLECTION_LOADED']}))
}
get(courseId) {
this.FirebaseCRUDFactory.get(courseId, this.collectionKey)
.then((data: any)=> this.thread.onNext({data, 'EVENT': this.threadsService.defaultEvents.OBJECT_LOAD }))
.catch((error: any)=> this.thread.onError({error, 'EVENT': this.threadsService.defaultEvents.OBJECT_LOAD }))
}
create(courseObj) {
this.FirebaseCRUDFactory.create(courseObj, this.collectionKey)
.then((data)=> this.thread.onNext({data, 'EVENT': this.threadsService.defaultEvents.OBJECT_CREATE}))
.catch((error)=> this.thread.onError({error, 'EVENT': this.threadsService.defaultEvents.OBJECT_CREATE}))
}
update(courseObj, courseId) {
this.FirebaseCRUDFactory.update(courseObj, courseId, this.collectionKey)
.then((data)=> this.thread.onNext({data, 'EVENT': this.threadsService.defaultEvents.OBJECT_UPDATE}))
}
remove(courseId) {
this.FirebaseCRUDFactory.remove(courseId, this.collectionKey)
.then((data)=> this.thread.onNext({data, 'EVENT': this.threadsService.defaultEvents.OBJECT_DELETE}))
.catch((error)=> this.thread.onError({error, type:'deletion'}))
}
}
angular.module('smz.services')
.service('coursesService', CoursesService)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment