(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 // Find the course and attach steps to it
const course = courses.find((item) => item.id === id); const course = courses.find((item) => item.id === id);
if ( course )
{
course.steps = steps; course.steps = steps;
}
return [ return [
200, 200,

View File

@ -1,8 +1,9 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs'; import { BehaviorSubject, Observable, of, throwError } from 'rxjs';
import { tap } from 'rxjs/operators'; import { map, switchMap, tap } from 'rxjs/operators';
import { Category, Course } from 'app/modules/admin/apps/academy/academy.types'; import { Category, Course } from 'app/modules/admin/apps/academy/academy.types';
import { Chat } from 'app/modules/admin/apps/chat/chat.types';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -83,8 +84,22 @@ export class AcademyService
getCourseById(id: string): Observable<Course> getCourseById(id: string): Observable<Course>
{ {
return this._httpClient.get<Course>('api/apps/academy/courses/course', {params: {id}}).pipe( return this._httpClient.get<Course>('api/apps/academy/courses/course', {params: {id}}).pipe(
tap((response: any) => { map((course) => {
this._course.next(response);
// 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);
}) })
); );
} }