forked from angular-university/angular-material-course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
199 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"/api": { | ||
"target": "http://localhost:9000", | ||
"secure": false | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
|
||
import {Request, Response} from 'express'; | ||
import {COURSES} from "./db-data"; | ||
|
||
|
||
|
||
export function getAllCourses(req: Request, res: Response) { | ||
|
||
res.status(200).json({payload:Object.values(COURSES)}); | ||
|
||
} | ||
|
||
|
||
export function getCourseById(req: Request, res: Response) { | ||
|
||
const courseId = req.params["id"]; | ||
|
||
const courses = Object.values(COURSES); | ||
|
||
const course = courses.find(course => course.id == courseId); | ||
|
||
res.status(200).json(course); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
|
||
|
||
import {Request, Response} from 'express'; | ||
import {LESSONS} from "./db-data"; | ||
|
||
|
||
|
||
export function searchLessons(req: Request, res: Response) { | ||
|
||
const queryParams = req.query; | ||
|
||
const filter = queryParams.filter, | ||
sortOrder = queryParams.order, | ||
pageNumber = parseInt(queryParams.pageNumber), | ||
pageSize = parseInt(queryParams.pageSize); | ||
|
||
let lessons = Object.values(LESSONS).sort((l1, l2) => l1.id - l2.id); | ||
|
||
if (filter) { | ||
lessons = lessons.filter(lesson => lesson.description.toLowerCase().search(filter) >= 0); | ||
} | ||
|
||
if (sortOrder === "desc") { | ||
lessons = lessons.reverse(); | ||
} | ||
|
||
const initialPos = (pageNumber - 1) * pageSize; | ||
|
||
const lessonsPage = lessons.slice(initialPos, initialPos + pageSize); | ||
|
||
|
||
res.status(200).json({payload: lessonsPage}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
|
||
import * as express from 'express'; | ||
import {Application} from "express"; | ||
import {getAllCourses, getCourseById} from "./get-courses.route"; | ||
import {searchLessons} from "./search-lessons.route"; | ||
|
||
|
||
const app: Application = express(); | ||
|
||
|
||
app.route('/api/courses').get(getAllCourses); | ||
|
||
app.route('/api/courses/:id').get(getCourseById); | ||
|
||
app.route('/api/lessons').get(searchLessons); | ||
|
||
|
||
|
||
|
||
const httpServer = app.listen(9000, () => { | ||
console.log("HTTP REST API Server running at http://localhost:" + httpServer.address().port); | ||
}); | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
|
||
|
||
import {Injectable} from "@angular/core"; | ||
import {ActivatedRouteSnapshot, Resolve, RouterStateSnapshot} from "@angular/router"; | ||
import {Course} from "../model/course"; | ||
import {Observable} from "rxjs/Observable"; | ||
import {CoursesService} from "./courses.service"; | ||
|
||
|
||
|
||
@Injectable() | ||
export class CourseResolver implements Resolve<Course> { | ||
|
||
constructor(private coursesService:CoursesService) { | ||
|
||
} | ||
|
||
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Course> { | ||
return this.coursesService.findCourseById(route.params['id']); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
|
||
import {Injectable} from "@angular/core"; | ||
import {HttpClient} from "@angular/common/http"; | ||
import {Observable} from "rxjs/Observable"; | ||
import {Course} from "../model/course"; | ||
import {map} from "rxjs/operators"; | ||
|
||
|
||
@Injectable() | ||
export class CoursesService { | ||
|
||
constructor(private http:HttpClient) { | ||
|
||
} | ||
|
||
findCourseById(courseId: number): Observable<Course> { | ||
return this.http.get<Course>(`/api/courses/${courseId}`); | ||
} | ||
|
||
findAllCourses(): Observable<Course[]> { | ||
return this.http.get('/api/courses') | ||
.pipe( | ||
map(res => res['payload']) | ||
); | ||
} | ||
|
||
findLessons(courseId:number, filter = '', sortOrder = 'asc', pageNumber = 1, pageSize = 3) { | ||
|
||
|
||
|
||
} | ||
|
||
} |