Modification du service API du frontend Angular pour permettre de récupérer la réponse au complet au lieu de seulement le corps.

This commit is contained in:
FyloZ 2021-02-07 19:00:18 -05:00
parent c38552d703
commit ecdb6f52c8
3 changed files with 40 additions and 34 deletions

View File

@ -18,14 +18,16 @@
let-control="control"
let-field="field">
<div class="simdut-file w-100 d-flex justify-content-between">
<button mat-raised-button color="primary" [disabled]="!hasSimdut"
[attr.title]="!hasSimdut ? 'Ce produit n\'a pas de fiche signalitique' : null" (click)="openSimdutUrl()">
<button
mat-raised-button
color="primary"
[disabled]="!hasSimdut"
[attr.title]="!hasSimdut ? 'Ce produit n\'a pas de fiche signalitique' : null"
(click)="openSimdutUrl()">
Voir la fiche signalitique
</button>
<div class="edit-simdut-file-input">
<button mat-raised-button color="accent" type="button">Modifier la fiche
signalitique
</button>
<button mat-raised-button color="accent" type="button">Modifier la fiche signalitique</button>
<mat-form-field>
<mat-label>{{field.label}}</mat-label>
<ngx-mat-file-input #simdutFileInput [accept]="field.fileType" [formControl]="control"></ngx-mat-file-input>

View File

@ -1,10 +1,10 @@
import {Injectable, OnDestroy} from '@angular/core';
import {HttpClient, HttpHeaders, HttpParams} from "@angular/common/http";
import {HttpClient} from "@angular/common/http";
import {Observable, Subject} from "rxjs";
import {environment} from "../../../../environments/environment";
import {AppState} from "../app-state";
import {Router} from "@angular/router";
import {share, takeUntil} from "rxjs/operators";
import {map, share, takeUntil} from "rxjs/operators";
@Injectable({
providedIn: 'root'
@ -24,50 +24,44 @@ export class ApiService implements OnDestroy {
this._destroy$.complete()
}
get<T>(url: string, needAuthentication = true, options: any = {}): Observable<T> {
get<T>(url: string, needAuthentication = true, requestOptions: ApiRequestOptions = new ApiRequestOptions()): Observable<T> {
// @ts-ignore
return this.executeHttpRequest(
httpOptions => this.http.get<T>(environment.apiUrl + url, httpOptions),
httpOptions => this.http.get<any>(environment.apiUrl + url, httpOptions),
needAuthentication,
options
requestOptions
)
}
post<T>(url: string, body: any = {}, needAuthentication = true, options: any = {}): Observable<T> {
post<T>(url: string, body: any = {}, needAuthentication = true, requestOptions: ApiRequestOptions = new ApiRequestOptions()): Observable<T> {
// @ts-ignore
return this.executeHttpRequest(
httpOptions => this.http.post<T>(environment.apiUrl + url, body, httpOptions),
httpOptions => this.http.post<any>(environment.apiUrl + url, body, httpOptions),
needAuthentication,
options
requestOptions
)
}
put<T>(url: string, body: any = {}, needAuthentication = true, options: any = {}): Observable<T> {
put<T>(url: string, body: any = {}, needAuthentication = true, requestOptions: ApiRequestOptions = new ApiRequestOptions()): Observable<T> {
// @ts-ignore
return this.executeHttpRequest(
httpOptions => this.http.put<T>(environment.apiUrl + url, body, httpOptions),
httpOptions => this.http.put<any>(environment.apiUrl + url, body, httpOptions),
needAuthentication,
options
requestOptions
)
}
delete<T>(url: string, needAuthentication = true, options: any = {}): Observable<T> {
delete<T>(url: string, needAuthentication = true, requestOptions: ApiRequestOptions = new ApiRequestOptions()): Observable<T> {
// @ts-ignore
return this.executeHttpRequest(
httpOptions => this.http.delete<T>(environment.apiUrl + url, httpOptions),
httpOptions => this.http.delete<any>(environment.apiUrl + url, httpOptions),
needAuthentication,
options
requestOptions
)
}
private executeHttpRequest<T>(requestFn: (httpOptions?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}) => Observable<T>, needAuthentication = true, httpOptions: any = {}): Observable<T> {
private executeHttpRequest<T>(requestFn: (httpOptions) => Observable<any>, needAuthentication = true, requestOptions: ApiRequestOptions = new ApiRequestOptions()): Observable<T> {
const httpOptions = {withCredentials: false, observe: 'response'}
if (needAuthentication) {
if (this.checkAuthenticated()) {
if (httpOptions) {
@ -80,8 +74,11 @@ export class ApiService implements OnDestroy {
}
}
const result$ = requestFn(httpOptions)
.pipe(takeUntil(this._destroy$), share())
const result$ = requestOptions.takeFullResponse
? requestFn(httpOptions)
.pipe(takeUntil(this._destroy$), share())
: requestFn(httpOptions)
.pipe(takeUntil(this._destroy$), map(r => r.body), share())
const errorCheckSubscription = result$.subscribe({
next: () => this.appState.isServerOnline = true,
@ -102,3 +99,10 @@ export class ApiService implements OnDestroy {
this.router.navigate(['/account/login'])
}
}
export class ApiRequestOptions {
constructor(
public takeFullResponse = false
) {
}
}

View File

@ -49,7 +49,7 @@ class RecipeImageController(val recipeImageService: RecipeImageService) {
@ResponseStatus(HttpStatus.CREATED)
fun save(@PathVariable recipeId: Long, image: MultipartFile): ResponseEntity<Void> {
val id = recipeImageService.save(image, recipeId)
return ResponseEntity.created(URI.create("$RECIPE_CONTROLLER_PATH/$recipeId/image/$id")).build()
return ResponseEntity.created(URI.create("/$RECIPE_CONTROLLER_PATH/$recipeId/image/$id")).build()
}
@DeleteMapping("{recipeId}/image/{id}")