(apps/academy) Better error handling on courses that are not exist

This commit is contained in:
sercan 2021-04-26 15:59:44 +03:00
parent 284e282761
commit deeef323f9
2 changed files with 23 additions and 5 deletions

View File

@ -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,

View File

@ -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<Course>
{
return this._httpClient.get<Course>('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);
})
);
}