diff --git a/src/app/pages/main/contact.service.spec.ts b/src/app/pages/main/contact.service.spec.ts index 5c98655..fc4b9b8 100644 --- a/src/app/pages/main/contact.service.spec.ts +++ b/src/app/pages/main/contact.service.spec.ts @@ -50,7 +50,7 @@ describe('ContactService', () => { expect(postReq.request.method).toEqual('POST'); expect(postReq.request.body).toEqual(CONTACT_MOCK); CONTACT_MOCK.id = 1; - RES_MOCK.data = CONTACT_MOCK + RES_MOCK.data = CONTACT_MOCK; postReq.flush(RES_MOCK); const getReq = httpTestingController.expectOne(endpointURL); expect(getReq.request.method).toEqual('GET'); @@ -59,12 +59,25 @@ describe('ContactService', () => { 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', () => { const ID_MOCK = 1; service.delete(ID_MOCK).subscribe(); const deleteReq = httpTestingController.expectOne(`${endpointURL}/${ID_MOCK}`); expect(deleteReq.request.method).toEqual('DELETE'); - RES_MOCK.message = "Contact deleted successfully"; + RES_MOCK.message = 'Contact deleted successfully'; deleteReq.flush(RES_MOCK); const getReq = httpTestingController.expectOne(endpointURL); expect(getReq.request.method).toEqual('GET'); diff --git a/src/app/pages/main/contact.service.ts b/src/app/pages/main/contact.service.ts index e4ef8fc..2121cc6 100644 --- a/src/app/pages/main/contact.service.ts +++ b/src/app/pages/main/contact.service.ts @@ -9,27 +9,39 @@ import { BehaviorSubject, map, switchMap, tap } from 'rxjs'; providedIn: 'root', }) export class ContactService { - private readonly httpClient = inject(HttpClient); private readonly contacts = new BehaviorSubject([]); readonly contacts$ = this.contacts.asObservable(); - delete(id: number){ - return this.httpClient.delete>(`${environment.apiUrl}/contacts/${id}`) - .pipe(switchMap(() => this.getAll())) + delete(id: number) { + return this.httpClient + .delete>(`${environment.apiUrl}/contacts/${id}`) + .pipe(switchMap(() => this.getAll())); + } + + findById(id: string) { + return this.httpClient.get>(`${environment.apiUrl}/contacts/${id}`); } getAll() { - return this.httpClient.get>(environment.apiUrl+'/contacts') - .pipe( - map( response => response.data ?? []), - tap( contacts => this.contacts.next(contacts)) - ) + return this.httpClient + .get>(environment.apiUrl + '/contacts') + .pipe( + map((response) => response.data ?? []), + tap((contacts) => this.contacts.next(contacts)) + ); } save(contact: ContactDTO) { - return this.httpClient.post>(environment.apiUrl+'/contacts', contact) - .pipe(switchMap(() => this.getAll())) + return this.httpClient + .post>(environment.apiUrl + '/contacts', contact) + .pipe(switchMap(() => this.getAll())); } + update(contact: ContactDTO) { + return this.httpClient.put>( + `${environment.apiUrl}/contacts/${contact.id}`, + contact + ); + } }