refactor: add update method

This commit is contained in:
2025-12-17 22:25:52 -03:00
parent 9efb64eea8
commit 458cf97ba0
2 changed files with 38 additions and 13 deletions

View File

@@ -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');

View File

@@ -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<ContactDTO[]>([]);
readonly contacts$ = this.contacts.asObservable();
delete(id: number) {
return this.httpClient.delete<Response<string>>(`${environment.apiUrl}/contacts/${id}`)
.pipe(switchMap(() => this.getAll()))
return this.httpClient
.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() {
return this.httpClient.get<Response<ContactDTO[]|null>>(environment.apiUrl+'/contacts')
return this.httpClient
.get<Response<ContactDTO[] | null>>(environment.apiUrl + '/contacts')
.pipe(
map( response => response.data ?? []),
tap( contacts => this.contacts.next(contacts))
)
map((response) => response.data ?? []),
tap((contacts) => this.contacts.next(contacts))
);
}
save(contact: ContactDTO) {
return this.httpClient.post<Response<ContactDTO[]|null>>(environment.apiUrl+'/contacts', contact)
.pipe(switchMap(() => this.getAll()))
return this.httpClient
.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
);
}
}