From deeef323f912b3c303510648a4dd3755403c92bf Mon Sep 17 00:00:00 2001 From: sercan Date: Mon, 26 Apr 2021 15:59:44 +0300 Subject: [PATCH] (apps/academy) Better error handling on courses that are not exist --- src/app/mock-api/apps/academy/api.ts | 5 +++- .../admin/apps/academy/academy.service.ts | 23 +++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/app/mock-api/apps/academy/api.ts b/src/app/mock-api/apps/academy/api.ts index 862282c0..cf4abe7c 100644 --- a/src/app/mock-api/apps/academy/api.ts +++ b/src/app/mock-api/apps/academy/api.ts @@ -75,7 +75,10 @@ export class AcademyMockApi // Find the course and attach steps to it const course = courses.find((item) => item.id === id); - course.steps = steps; + if ( course ) + { + course.steps = steps; + } return [ 200, diff --git a/src/app/modules/admin/apps/academy/academy.service.ts b/src/app/modules/admin/apps/academy/academy.service.ts index d639b119..12385626 100644 --- a/src/app/modules/admin/apps/academy/academy.service.ts +++ b/src/app/modules/admin/apps/academy/academy.service.ts @@ -1,8 +1,9 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; -import { BehaviorSubject, Observable } from 'rxjs'; -import { tap } from 'rxjs/operators'; +import { BehaviorSubject, Observable, of, throwError } from 'rxjs'; +import { map, switchMap, tap } from 'rxjs/operators'; import { Category, Course } from 'app/modules/admin/apps/academy/academy.types'; +import { Chat } from 'app/modules/admin/apps/chat/chat.types'; @Injectable({ providedIn: 'root' @@ -83,8 +84,22 @@ export class AcademyService getCourseById(id: string): Observable { return this._httpClient.get('api/apps/academy/courses/course', {params: {id}}).pipe( - tap((response: any) => { - this._course.next(response); + map((course) => { + + // Update the course + this._course.next(course); + + // Return the course + return course; + }), + switchMap((course) => { + + if ( !course ) + { + return throwError('Could not found course with id of ' + id + '!'); + } + + return of(course); }) ); }