refactor: add update method
This commit is contained in:
@@ -50,7 +50,7 @@ describe('ContactService', () => {
|
|||||||
expect(postReq.request.method).toEqual('POST');
|
expect(postReq.request.method).toEqual('POST');
|
||||||
expect(postReq.request.body).toEqual(CONTACT_MOCK);
|
expect(postReq.request.body).toEqual(CONTACT_MOCK);
|
||||||
CONTACT_MOCK.id = 1;
|
CONTACT_MOCK.id = 1;
|
||||||
RES_MOCK.data = CONTACT_MOCK
|
RES_MOCK.data = CONTACT_MOCK;
|
||||||
postReq.flush(RES_MOCK);
|
postReq.flush(RES_MOCK);
|
||||||
const getReq = httpTestingController.expectOne(endpointURL);
|
const getReq = httpTestingController.expectOne(endpointURL);
|
||||||
expect(getReq.request.method).toEqual('GET');
|
expect(getReq.request.method).toEqual('GET');
|
||||||
@@ -59,12 +59,25 @@ describe('ContactService', () => {
|
|||||||
httpTestingController.verify();
|
httpTestingController.verify();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should PUT contact with the given ID to the contacts ep ', () => {
|
||||||
|
const UPDATE_CONTACT_MOCK = new ContactDTO(1, 'mock', 'mock', 'mock');
|
||||||
|
service.update(UPDATE_CONTACT_MOCK).subscribe();
|
||||||
|
const putReq = httpTestingController.expectOne(`${endpointURL}/${UPDATE_CONTACT_MOCK.id}`);
|
||||||
|
expect(putReq.request.method).toEqual('PUT');
|
||||||
|
expect(putReq.request.body).toEqual(UPDATE_CONTACT_MOCK);
|
||||||
|
putReq.flush({
|
||||||
|
message: 'Contact updated successfully',
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
httpTestingController.verify();
|
||||||
|
});
|
||||||
|
|
||||||
it('should DELETE with the given ID to the contacts ep', () => {
|
it('should DELETE with the given ID to the contacts ep', () => {
|
||||||
const ID_MOCK = 1;
|
const ID_MOCK = 1;
|
||||||
service.delete(ID_MOCK).subscribe();
|
service.delete(ID_MOCK).subscribe();
|
||||||
const deleteReq = httpTestingController.expectOne(`${endpointURL}/${ID_MOCK}`);
|
const deleteReq = httpTestingController.expectOne(`${endpointURL}/${ID_MOCK}`);
|
||||||
expect(deleteReq.request.method).toEqual('DELETE');
|
expect(deleteReq.request.method).toEqual('DELETE');
|
||||||
RES_MOCK.message = "Contact deleted successfully";
|
RES_MOCK.message = 'Contact deleted successfully';
|
||||||
deleteReq.flush(RES_MOCK);
|
deleteReq.flush(RES_MOCK);
|
||||||
const getReq = httpTestingController.expectOne(endpointURL);
|
const getReq = httpTestingController.expectOne(endpointURL);
|
||||||
expect(getReq.request.method).toEqual('GET');
|
expect(getReq.request.method).toEqual('GET');
|
||||||
|
|||||||
@@ -9,27 +9,39 @@ import { BehaviorSubject, map, switchMap, tap } from 'rxjs';
|
|||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class ContactService {
|
export class ContactService {
|
||||||
|
|
||||||
private readonly httpClient = inject(HttpClient);
|
private readonly httpClient = inject(HttpClient);
|
||||||
private readonly contacts = new BehaviorSubject<ContactDTO[]>([]);
|
private readonly contacts = new BehaviorSubject<ContactDTO[]>([]);
|
||||||
readonly contacts$ = this.contacts.asObservable();
|
readonly contacts$ = this.contacts.asObservable();
|
||||||
|
|
||||||
delete(id: number){
|
delete(id: number) {
|
||||||
return this.httpClient.delete<Response<string>>(`${environment.apiUrl}/contacts/${id}`)
|
return this.httpClient
|
||||||
.pipe(switchMap(() => this.getAll()))
|
.delete<Response<string>>(`${environment.apiUrl}/contacts/${id}`)
|
||||||
|
.pipe(switchMap(() => this.getAll()));
|
||||||
|
}
|
||||||
|
|
||||||
|
findById(id: string) {
|
||||||
|
return this.httpClient.get<Response<ContactDTO | null>>(`${environment.apiUrl}/contacts/${id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
getAll() {
|
getAll() {
|
||||||
return this.httpClient.get<Response<ContactDTO[]|null>>(environment.apiUrl+'/contacts')
|
return this.httpClient
|
||||||
|
.get<Response<ContactDTO[] | null>>(environment.apiUrl + '/contacts')
|
||||||
.pipe(
|
.pipe(
|
||||||
map( response => response.data ?? []),
|
map((response) => response.data ?? []),
|
||||||
tap( contacts => this.contacts.next(contacts))
|
tap((contacts) => this.contacts.next(contacts))
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
save(contact: ContactDTO) {
|
save(contact: ContactDTO) {
|
||||||
return this.httpClient.post<Response<ContactDTO[]|null>>(environment.apiUrl+'/contacts', contact)
|
return this.httpClient
|
||||||
.pipe(switchMap(() => this.getAll()))
|
.post<Response<ContactDTO[] | null>>(environment.apiUrl + '/contacts', contact)
|
||||||
|
.pipe(switchMap(() => this.getAll()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update(contact: ContactDTO) {
|
||||||
|
return this.httpClient.put<Response<ContactDTO[] | null>>(
|
||||||
|
`${environment.apiUrl}/contacts/${contact.id}`,
|
||||||
|
contact
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user