Feature: Added the customer module to list / edit customers. This is needed to add the customer discount functionality
This commit is contained in:
1 parent
214c40fbde
commit
0da16e9548
24 files changed
+560
-7
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from 'rxjs/internal/Observable';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
|
||||
import { Customer } from '../core/customer';
|
||||
import { ErrorLoggerService } from '../core/error-logger.service';
|
||||
|
||||
const httpOptions = {
|
||||
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
|
||||
};
|
||||
const url = '/api/customers';
|
||||
const serviceName = 'CustomerService';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class CustomerService {
|
||||
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
||||
|
||||
get(id: string | null): Observable<Customer> {
|
||||
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
||||
return this.http
|
||||
.get<Customer>(getUrl)
|
||||
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Customer>;
|
||||
}
|
||||
|
||||
list(): Observable<Customer[]> {
|
||||
return this.http
|
||||
.get<Customer[]>(`${url}/list`)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<Customer[]>;
|
||||
}
|
||||
|
||||
save(customer: Customer): Observable<Customer> {
|
||||
return this.http
|
||||
.post<Customer>(`${url}`, customer, httpOptions)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Customer>;
|
||||
}
|
||||
|
||||
update(customer: Customer): Observable<Customer> {
|
||||
return this.http
|
||||
.put<Customer>(`${url}/${customer.id}`, customer, httpOptions)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Customer>;
|
||||
}
|
||||
|
||||
saveOrUpdate(customer: Customer): Observable<Customer> {
|
||||
if (!customer.id) {
|
||||
return this.save(customer);
|
||||
}
|
||||
return this.update(customer);
|
||||
}
|
||||
|
||||
delete(id: string): Observable<Customer> {
|
||||
return this.http
|
||||
.delete<Customer>(`${url}/${id}`, httpOptions)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Customer>;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user