feat: HTTP-Anforderungsdienste mit HttpClient im Frontend implementieren

This commit is contained in:
Developer 02 2024-08-21 17:59:55 +02:00
parent 66a8471f05
commit 320e81719b
4 changed files with 72 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import { MatFormFieldModule } from '@angular/material/form-field';
import { MatStepperModule } from '@angular/material/stepper';
import { MatButtonModule } from '@angular/material/button';
import { MatSelectModule } from '@angular/material/select';
import { ReceiverService } from '../../services/receiver.service';
@Component({
selector: 'app-envelope-creation',
@ -23,6 +24,9 @@ import { MatSelectModule } from '@angular/material/select';
})
export class EnvelopeCreationComponent {
constructor(private receiverService: ReceiverService) {
}
ngOnInit() {
this.selectedType = this.types[0].value;
}
@ -39,8 +43,8 @@ export class EnvelopeCreationComponent {
selectedType: string = "Mietvertrag"
types: any[] = [
{value: 0, viewValue: 'Mietvertrag'},
{value: 1, viewValue: 'Kaufvertrag'}
{ value: 0, viewValue: 'Mietvertrag' },
{ value: 1, viewValue: 'Kaufvertrag' }
];
typeControl = new FormControl('Mietvertrag', [Validators.required]);
}

View File

@ -7,7 +7,7 @@ import { API_URL } from '../tokens/index';
providedIn: 'root'
})
export class EnvelopeReceiverService {
private url: string;
protected url: string;
constructor(private http: HttpClient) {
const api_url = inject(API_URL);
@ -16,12 +16,14 @@ export class EnvelopeReceiverService {
getEnvelopeReceiver(options?: { min_status?: number; max_status?: number; ignore_status?: number[] }): Observable<any> {
let params = new HttpParams();
if (options?.min_status !== undefined)
params = params.set('min_status', options?.min_status.toString());
if (options?.max_status !== undefined)
params = params.set('max_status', options?.max_status.toString());
if (options?.ignore_status !== undefined)
params = params.set('ignore_status', options?.ignore_status.join(','));
if (options) {
if (options.min_status)
params = params.set('min_status', options.min_status.toString());
if (options.max_status)
params = params.set('max_status', options.max_status.toString());
if (options.ignore_status)
params = params.set('ignore_status', options.ignore_status.join(','));
}
return this.http.get<any>(this.url, { params });
}

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ReceiverService } from './receiver.service';
describe('ReceiverService', () => {
let service: ReceiverService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ReceiverService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,41 @@
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { API_URL } from '../tokens';
import { Observable, firstValueFrom } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ReceiverService {
protected url: string;
constructor(private http: HttpClient) {
const api_url = inject(API_URL);
this.url = `${api_url}/receiver`;
}
public getReceiver(options?: { emailAdress?: string; signature?: string; }): Observable<any> {
let params = new HttpParams();
if (options) {
if (options.emailAdress)
params = params.set('emailAdress', options?.emailAdress.toString());
if (options?.signature)
params = params.set('signature', options?.signature.toString());
}
return this.http.get<any>(this.url, { params });
}
public async getReceiverAsync(options?: { emailAdress?: string; signature?: string; }): Promise<any> {
return await firstValueFrom(this.getReceiver(options));
}
public createReceiver(emailAddress: string): Observable<any> {
return this.http.post<any>(this.url, { emailAddress: emailAddress });
}
public async createReceiverAsync(emailAddress: string): Promise<any> {
return await firstValueFrom(this.createReceiver(emailAddress));
}
}