Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 3x 3x 3x 3x 3x 3x 3x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { map } from 'rxjs/operators';
import { Link } from 'ish-core/models/link/link.model';
import { ApiService, unpackEnvelope } from 'ish-core/services/api/api.service';
import { ProductReview, ProductReviewCreationType } from '../../models/product-reviews/product-review.model';
import { ProductReviewsMapper } from '../../models/product-reviews/product-reviews.mapper';
import { ProductReviews } from '../../models/product-reviews/product-reviews.model';
@Injectable({ providedIn: 'root' })
export class ReviewsService {
constructor(private apiService: ApiService) {}
/**
* Gets the reviews for a given product.
*
* @param sku The product sku.
* @returns The available reviews of a product.
*/
getProductReviews(sku: string): Observable<ProductReviews> {
Iif (!sku) {
return throwError(() => new Error('getProductReviews() called without sku'));
}
const params = new HttpParams().set('attrs', 'own');
return this.apiService
.get(`products/${this.apiService.encodeResourceId(sku)}/reviews`, { sendSPGID: true, params })
.pipe(
unpackEnvelope<Link>(),
this.apiService.resolveLinks<ProductReview>(),
map(reviews => ProductReviewsMapper.fromData(sku, reviews))
);
}
/**
* Creates a user review for a given product, the user name is always sent to the server.
*
* @param sku The product sku.
* @param review The review and rating for the product.
* @returns The created product review.
*/
createProductReview(sku: string, review: ProductReviewCreationType) {
Iif (!sku) {
return throwError(() => new Error('createProductReview() called without sku'));
}
Iif (!review) {
return throwError(() => new Error('createProductReview() called without review'));
}
return this.apiService
.post(`products/${this.apiService.encodeResourceId(sku)}/reviews`, { ...review, showAuthorNameFlag: true })
.pipe(
this.apiService.resolveLink<ProductReview>(),
map(review => ProductReviewsMapper.fromData(sku, [{ ...review, own: true }]))
);
}
/**
* Deletes a review of the current user for a given product.
*
* @param sku The product sku.
* @param review The review id.
*/
deleteProductReview(sku: string, reviewId: string) {
Iif (!sku) {
return throwError(() => new Error('deleteProductReview() called without sku'));
}
Iif (!reviewId) {
return throwError(() => new Error('deleteProductReview() called without reviewId'));
}
return this.apiService.delete(
`products/${this.apiService.encodeResourceId(sku)}/reviews/${this.apiService.encodeResourceId(reviewId)}`
);
}
}
|