Commit fc8c7f5b authored by Roman Klauser's avatar Roman Klauser

...

parent 891ad06e
......@@ -3,11 +3,12 @@
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"start": "ng serve --proxy-config ./proxy.conf.json",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
"e2e": "ng e2e",
"server": "./node_modules/.bin/ts-node ./server/server.ts"
},
"private": true,
"dependencies": {
......
{
"/api": {
"target": "http://localhost:9000",
"secure": false
}
}
This diff is collapsed.
import {Request, Response} from 'express';
import {COURSES} from "./db-data";
export function getAllCourses(req: Request, res: Response) {
/*
const error = (Math.random() >= 0.5);
if (error) {
console.log("ERROR loading courses!");
res.status(500).json({message: 'random error occurred.'});
}
else { */
setTimeout(() => {
res.status(200).json({payload:Object.values(COURSES)});
}, 200);
// }
}
export function getCourseById(req: Request, res: Response) {
const courseId = req.params["id"];
const courses:any = Object.values(COURSES);
const course = courses.find(course => course.id == courseId);
res.status(200).json(course);
}
\ No newline at end of file
import {Request, Response} from 'express';
import {COURSES} from "./db-data";
import {setTimeout} from 'timers';
export function saveCourse(req: Request, res: Response) {
const id = req.params["id"],
changes = req.body;
console.log("Saving course", id, JSON.stringify(changes));
COURSES[id] = {
...COURSES[id],
...changes
};
setTimeout(() => {
res.status(200).json(COURSES[id]);
}, 2000);
}
import {Request, Response} from 'express';
import {LESSONS} from "./db-data";
import {setTimeout} from "timers";
export function searchLessons(req: Request, res: Response) {
const queryParams = req.query;
const courseId = queryParams.courseId,
filter = queryParams.filter || '',
sortOrder = queryParams.sortOrder || 'asc',
pageNumber = parseInt(queryParams.pageNumber) || 0,
pageSize = parseInt(queryParams.pageSize) || 3;
let lessons = Object.values(LESSONS).filter(lesson => lesson.courseId == courseId).sort((l1, l2) => l1.id - l2.id);
if (filter) {
lessons = lessons.filter(lesson => lesson.description.trim().toLowerCase().search(filter.toLowerCase()) >= 0);
}
if (sortOrder == "desc") {
lessons = lessons.reverse();
}
const initialPos = pageNumber * pageSize;
const lessonsPage = lessons.slice(initialPos, initialPos + pageSize);
setTimeout(() => {
res.status(200).json({payload: lessonsPage});
},1000);
}
\ No newline at end of file
import * as express from 'express';
import {Application} from "express";
import {getAllCourses, getCourseById} from "./get-courses.route";
import {searchLessons} from "./search-lessons.route";
import {saveCourse} from './save-course.route';
const bodyParser = require('body-parser');
const app: Application = express();
app.use(bodyParser.json());
app.route('/api/courses').get(getAllCourses);
app.route('/api/courses/:id').get(getCourseById);
app.route('/api/lessons').get(searchLessons);
app.route('/api/courses/:id').put(saveCourse);
const httpServer = app.listen(9000, () => {
console.log("HTTP REST API Server running at http://localhost:" + httpServer.address().port);
});
import { Component } from '@angular/core';
import {Component, OnInit} from '@angular/core';
import {fromEvent, interval, timer} from 'rxjs/index';
import {switchMap} from 'rxjs/internal/operators';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
export class AppComponent implements OnInit {
ngOnInit(): void {
document.addEventListener('click', (event) => {
console.log('clicked: ', event);
});
setTimeout(() => {
console.log('time outed');
}, 3000);
let counter = 0;
setInterval(() => {
console.log('interval: ', ++counter);
}, 2000);
// const interval$ = interval(1000);
// const timer$ = timer(3000, 1000);
// const click$ = fromEvent(document, 'click');
// click$.pipe(switchMap(e => timer$))
// .subscribe(console.log);
}
}
......@@ -2,15 +2,17 @@ import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import {HttpObservableComponent} from './http-observable/http-observable.component';
@NgModule({
declarations: [
AppComponent
AppComponent,
HttpObservableComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
bootstrap: [HttpObservableComponent]
})
export class AppModule { }
<h1>HttpObservable</h1>
<pre>
{{ beginners$ | async | json }}
</pre>
<hr>
<pre>
{{ advanced$ | async | json }}
</pre>
import {Component, OnInit} from '@angular/core';
import {createHttpObservable} from './util';
import {map, shareReplay} from 'rxjs/internal/operators';
import {Observable} from 'rxjs/index';
@Component({
selector: 'app-root',
templateUrl: './http-observable.component.html'
})
export class HttpObservableComponent implements OnInit {
beginners$: Observable<any>;
advanced$: Observable<any>;
ngOnInit(): void {
const http$ = createHttpObservable('/api/courses');
const courses$ = http$.pipe(map(res => res['payload']), shareReplay());
this.beginners$ = courses$.pipe(map((courses: any[]) => courses.filter(c => c.category === 'BEGINNER')));
this.advanced$ = courses$.pipe(map((courses: any[]) => courses.filter(c => c.category === 'ADVANCED')));
}
}
import {Observable} from 'rxjs/index';
export function createHttpObservable(url: string) {
return Observable.create(observer => {
fetch('/api/courses')
.then(response => {
return response.json();
})
.then(response => {
observer.next(response);
observer.complete();
})
.catch(error => observer.error(error));
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment