Skip to content

Commit

Permalink
firebase queue
Browse files Browse the repository at this point in the history
  • Loading branch information
zhentian-wan committed Nov 27, 2016
1 parent 5996239 commit 43b7da6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 7 deletions.
53 changes: 53 additions & 0 deletions batch-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {firebaseConfig} from "./src/environments/firebase.config";
import {initializeApp, auth,database} from 'firebase';
const Queue = require('firebase-queue');
import Promise from "ts-promise";


console.log('Running batch server ...');

initializeApp(firebaseConfig);

auth()
.signInWithEmailAndPassword('two@admin.com', 'admin123')
.then(runConsumer)
.catch(onError);

function onError(err) {
console.error("Could not login", err);
process.exit();
}


function runConsumer() {

console.log("Running consumer ...");

const lessonsRef = database().ref("lessons");
const lessonsPerCourseRef = database().ref("lessonsPerCourse");

const queueRef = database().ref('queue');


const queue = new Queue(queueRef, function(data, progress, resolve, reject) {

console.log('received delete request ...',data);

const deleteLessonPromise = lessonsRef.child(data.lessonId).remove();

const deleteLessonPerCoursePromise =
lessonsPerCourseRef.child(`${data.courseId}/${data.lessonId}`).remove();

Promise.all([deleteLessonPromise, deleteLessonPerCoursePromise])
.then(
() => {
console.log("lesson deleted");
resolve();
}
)
.catch(() => {
console.log("lesson deletion in error");
reject();
});
});
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor",
"batch-server": "./node_modules/.bin/ts-node ./batch-server.ts",
"db": "./node_modules/.bin/ts-node ./popluate-db.ts"
},
"private": true,
Expand All @@ -32,6 +33,7 @@
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
"ts-node": "^1.4.3",
"ts-promise": "^0.3.4",
"zone.js": "^0.6.23"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions src/app/courses/course.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,11 @@ export class CourseService {
return subject.asObservable();
}

deleteLEssonById(lessonId: string, courseId) {
return this.rootDb.child('queue/tasks')
.push({
lessonId,
courseId
})
}
}
12 changes: 11 additions & 1 deletion src/app/courses/lesson-detail/lesson-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {ActivatedRoute} from "@angular/router";
import {Course} from "../courses";
import {CourseService} from "../course.service";
import {Observable} from "rxjs";
import {Lesson} from "../lessons/lessons";

@Component({
selector: 'app-lesson-detail',
Expand All @@ -16,7 +17,7 @@ export class LessonDetailComponent implements OnInit {

}

lesson: Observable<any>;
lesson: Observable<Lesson>;
ngOnInit() {
/*this.route.params
.map(p => p['id'])
Expand All @@ -36,4 +37,13 @@ export class LessonDetailComponent implements OnInit {
)
}

requestLessonDeletion() {
this.courseService.deleteLEssonById(
this.lesson.$key,
this.lesson.courseId
)
.then(() => alert("lesson delete successfully"))
.catch((err) => console.error(err));
}

}
3 changes: 1 addition & 2 deletions src/app/heros/hero/hero.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export class HeroComponent implements OnInit, OnDestroy {
this.editing = false;
this.heroId = id;
});
this.hero = this.route.data
.map((d:any)=> d['hero']);
this.hero = <Observable<Hero>>this.route.data.pluck('hero');


/* // since herocomponent get init everytime, it would be better to use snapshot for proferemence
Expand Down
6 changes: 2 additions & 4 deletions src/app/home/home.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import {MdToolbarModule} from "@angular2-material/toolbar";
import {MdInputModule} from "@angular2-material/input";
import {MdButtonModule} from "@angular2-material/button";
import homeRoutes from './home.routes';
import {MaterialModule} from "@angular/material";
@NgModule({
imports: [
CommonModule,
MdButtonModule.forRoot(),
MdInputModule.forRoot(),
MdToolbarModule.forRoot(),
MdListModule.forRoot(),
MaterialModule,
homeRoutes
],
declarations: [HomeComponent, SearchBarComponent, ResultListComponent],
Expand Down
4 changes: 4 additions & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"target": "es5",
"typeRoots": [
"../node_modules/@types"
],
"types": [
"node",
"firebase"
]
}
}

0 comments on commit 43b7da6

Please sign in to comment.