save result
This commit is contained in:
@@ -37,6 +37,10 @@ export interface CourseApiResponse {
|
||||
typesParisOuverts: Array<string>;
|
||||
}
|
||||
|
||||
export interface NonApiRequest {
|
||||
nonPartants: String[]
|
||||
}
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class CourseService {
|
||||
private apiUrl = environment.apiBaseUrl + API_BASE;
|
||||
@@ -454,34 +458,20 @@ export class CourseService {
|
||||
}
|
||||
|
||||
addNonPartant(courseId: string, npList: string[]) {
|
||||
const payload = {
|
||||
nonPartants: [...npList]
|
||||
}
|
||||
console.warn('addNonPartant is deprecated. Use setNonPartants instead.');
|
||||
return this.setNonPartants(courseId, npList);
|
||||
return this.setNonPartants(courseId, payload);
|
||||
}
|
||||
|
||||
setNonPartants(courseId: string, npList: string[]): Observable<Course | undefined> {
|
||||
if (USE_SERVER) {
|
||||
// Use PUT endpoint to replace the entire list
|
||||
return this.nonPartantService.replaceNonPartants(courseId, npList).pipe(
|
||||
switchMap((updatedNonPartants) => {
|
||||
// Fetch the updated course to return it
|
||||
return this.getById(courseId).pipe(
|
||||
map((course) => {
|
||||
if (course) {
|
||||
return {
|
||||
...course,
|
||||
nonPartants: updatedNonPartants,
|
||||
};
|
||||
}
|
||||
return undefined;
|
||||
})
|
||||
);
|
||||
}),
|
||||
catchError((err) => {
|
||||
console.error(`Error setting nonPartants for course ${courseId}:`, err);
|
||||
return of(undefined);
|
||||
})
|
||||
);
|
||||
}
|
||||
throw new Error('Server mode is required');
|
||||
setNonPartants(courseId: string, npList: NonApiRequest): Observable<CourseApiResponse | undefined> {
|
||||
return this.nonPartantService.replaceNonPartants(courseId, npList).pipe(
|
||||
map((updatedNonPartants) => updatedNonPartants),
|
||||
catchError((err) => {
|
||||
console.error(`Error setting nonPartants for course ${courseId}:`, err);
|
||||
return of(undefined);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { HttpClient } from '@angular/common/http';
|
||||
import { Observable, of } from 'rxjs';
|
||||
import { map, catchError } from 'rxjs/operators';
|
||||
import { environment } from 'src/environments/environment.development';
|
||||
import { CourseApiResponse, NonApiRequest } from './course';
|
||||
|
||||
const USE_SERVER = true;
|
||||
|
||||
@@ -20,21 +21,22 @@ export class NonPartantService {
|
||||
}
|
||||
|
||||
// PUT /api/v1/courses/{courseId}/non-partants - Replace the list of non-partants for a course
|
||||
replaceNonPartants(courseId: string, nonPartants: string[]): Observable<string[]> {
|
||||
if (USE_SERVER) {
|
||||
const courseApiUrl = environment.apiBaseUrl + '/api/v1/courses';
|
||||
return this.http
|
||||
.put<string[]>(`${courseApiUrl}/${courseId}/non-partants`, nonPartants, {
|
||||
headers: this.getNgrokHeaders(),
|
||||
replaceNonPartants(courseId: string, nonPartants: NonApiRequest): Observable<CourseApiResponse | undefined> {
|
||||
const courseApiUrl = environment.apiBaseUrl + '/api/courses';
|
||||
return this.http
|
||||
.patch<CourseApiResponse>(`${courseApiUrl}/${courseId}/non-partants`, nonPartants, {
|
||||
headers: this.getNgrokHeaders(),
|
||||
})
|
||||
.pipe(
|
||||
map((res) => {
|
||||
|
||||
return res
|
||||
}),
|
||||
catchError((err) => {
|
||||
console.log(err);
|
||||
console.error(`Error replacing non-partants for course ${courseId}:`, err);
|
||||
return of(undefined);
|
||||
})
|
||||
.pipe(
|
||||
map((list) => list.map((np) => String(np))),
|
||||
catchError((err) => {
|
||||
console.error(`Error replacing non-partants for course ${courseId}:`, err);
|
||||
return of([]);
|
||||
})
|
||||
);
|
||||
}
|
||||
return of([]);
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user