120 lines
4.5 KiB
TypeScript
120 lines
4.5 KiB
TypeScript
|
|
import { Injectable } from '@angular/core';
|
|
import { HttpClient, HttpErrorResponse, HttpRequest } from '@angular/common/http';
|
|
|
|
import { Observable, throwError } from 'rxjs';
|
|
import { catchError, tap } from 'rxjs/operators';
|
|
|
|
import { Sermon, MultipleSermonResponse, SingleSermonResponse } from '../interfaces/sermon';
|
|
import { SERMONS_BY_ID,
|
|
SERMONS_BY_PAGE_URL,
|
|
SERMON_MP3_BASE_URL,
|
|
SERMONS_BY_SEARCH_URL,
|
|
SERMON_ADD_URL,
|
|
SERMON_DELETE_URL,
|
|
SERMON_UPDATE_URL } from '../constants/urls';
|
|
|
|
@Injectable()
|
|
export class SermonService {
|
|
private pageSize: number = 10;
|
|
|
|
constructor(private httpClient: HttpClient){
|
|
|
|
}
|
|
|
|
getSermons(count: number): Observable<MultipleSermonResponse> {
|
|
let url = SERMONS_BY_PAGE_URL + 1 + "?pageSize=" + 5;
|
|
return this.httpClient.get<MultipleSermonResponse>(url).pipe(tap(e => this.setMultipleDates(e)), catchError(this.handleError));
|
|
};
|
|
|
|
getSermonsByPage(page: number): Observable<MultipleSermonResponse>{
|
|
let url = SERMONS_BY_PAGE_URL + page + "?pageSize=" + this.pageSize;
|
|
return this.httpClient.get<MultipleSermonResponse>(url).pipe(tap(e => this.setMultipleDates(e)), catchError(this.handleError));
|
|
}
|
|
|
|
getSermonById(id: number): Observable<SingleSermonResponse>{
|
|
let url = SERMONS_BY_ID + id + "?pageSize=" + this.pageSize;
|
|
return this.httpClient.get<SingleSermonResponse>(url).pipe(tap(e => this.setSingleDate(e)), catchError(this.handleError));
|
|
}
|
|
|
|
searchSermons(searchTerm: string, page: number): Observable<MultipleSermonResponse>{
|
|
let url = SERMONS_BY_SEARCH_URL + "?searchTerm=" + searchTerm + "&page=" + page + "&pageSize=" + this.pageSize;
|
|
return this.httpClient.get<MultipleSermonResponse>(url).pipe(tap(e => this.setMultipleDates(e)), catchError(this.handleError));
|
|
}
|
|
|
|
addSermon(sermon: Sermon, sermonFile: File){
|
|
let fd = new FormData();
|
|
fd.append("title",sermon.title);
|
|
fd.append("description",sermon.description);
|
|
fd.append("date",new Date(sermon.date).getTime().toString()); //Pas date as milliseconds since 1970
|
|
fd.append("author",sermon.author);
|
|
fd.append("file",sermonFile);
|
|
fd.append("timezoneOffset",new Date().getTimezoneOffset().toString());
|
|
const req = new HttpRequest('POST', SERMON_ADD_URL, fd, {
|
|
reportProgress: true,
|
|
withCredentials: true
|
|
});
|
|
return this.httpClient.request(req).pipe(
|
|
catchError(this.handleError)
|
|
);
|
|
}
|
|
|
|
updateSermon(sermon: Sermon){
|
|
let fd = new FormData();
|
|
fd.append("id",sermon.id.toString());
|
|
fd.append("title",sermon.title);
|
|
fd.append("description",sermon.description);
|
|
fd.append("date",new Date(sermon.date).toUTCString());
|
|
fd.append("author",sermon.author);
|
|
fd.append("timezoneOffset",new Date().getTimezoneOffset().toString());
|
|
return this.httpClient.put<Sermon>(SERMON_UPDATE_URL,fd,{withCredentials:true})
|
|
.pipe(catchError(this.handleError));
|
|
}
|
|
|
|
|
|
deleteSermon(sermonId: number){
|
|
let options = {
|
|
withCredentials: true,
|
|
body: {
|
|
"id" : sermonId
|
|
}
|
|
};
|
|
return this.httpClient.delete(SERMON_DELETE_URL,options)
|
|
.pipe(catchError(this.handleError));
|
|
}
|
|
|
|
setSingleDate(e : SingleSermonResponse): SingleSermonResponse {
|
|
e.sermon = this.setDates(e.sermon);
|
|
return e;
|
|
}
|
|
|
|
setMultipleDates(e : MultipleSermonResponse) : MultipleSermonResponse {
|
|
e.sermons.forEach(s => {
|
|
s = this.setDates(s);
|
|
});
|
|
return e;
|
|
}
|
|
|
|
setDates(sermon: Sermon): Sermon {
|
|
sermon.date = new Date(sermon.date);
|
|
sermon.uploadDate = new Date(sermon.uploadDate);
|
|
return sermon;
|
|
}
|
|
|
|
private handleError(error: HttpErrorResponse) {
|
|
if (error.error instanceof ErrorEvent) {
|
|
// A client-side or network error occurred. Handle it accordingly.
|
|
console.error('An error occurred:', error.error.message);
|
|
} else {
|
|
// The backend returned an unsuccessful response code.
|
|
// The response body may contain clues as to what went wrong,
|
|
console.error(
|
|
`Backend returned code ${error.status}, ` +
|
|
`body was: ${error.error}`);
|
|
}
|
|
// return an observable with a user-facing error message
|
|
return throwError(
|
|
'Something bad happened; please try again later.');
|
|
};
|
|
}
|