Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/api/src/app/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('AppController', () => {
beforeAll(async () => {
app = await Test.createTestingModule({
controllers: [AppController],
providers: [ProductService]
providers: [ProductService],
}).compile();
});

Expand Down
17 changes: 15 additions & 2 deletions apps/api/src/app/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {
Get,
Param,
Post,
UseInterceptors
UseInterceptors,
} from '@nestjs/common';
import {
BasicProduct,
CartItem,
Product,
ProductRating,
CartItem
Review,
} from '@ngrx-nx-workshop/api-interfaces';

import { CartService } from './cart/cart.service';
Expand Down Expand Up @@ -76,4 +77,16 @@ export class AppController {
getRatings(): ProductRating[] {
return this.ratingService.getRatings();
}

@Get('reviews/get/:productId')
getReviews(@Param('productId') productId: string): Review[] {
return this.ratingService.getReviews(productId);
}

@Post('reviews/post/:id')
addReview(
@Body() review: Pick<Review, 'productId' | 'reviewText' | 'reviewer'>
): Review {
return this.ratingService.addReview(review);
}
}
2 changes: 1 addition & 1 deletion apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import { RatingService } from './rating/rating.service';
@Module({
imports: [],
controllers: [AppController],
providers: [ProductService, CartService, RatingService]
providers: [ProductService, CartService, RatingService],
})
export class AppModule {}
2 changes: 1 addition & 1 deletion apps/api/src/app/cart/cart.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('CartService', () => {

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [CartService]
providers: [CartService],
}).compile();

service = module.get<CartService>(CartService);
Expand Down
9 changes: 6 additions & 3 deletions apps/api/src/app/cart/cart.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CartItem } from '@ngrx-nx-workshop/api-interfaces';

@Injectable()
export class CartService {
private cartProducts: CartItem[] = [];

private getItem(id: string): CartItem | undefined {
return this.cartProducts.find(cartItem => cartItem.productId === id);
return this.cartProducts.find((cartItem) => cartItem.productId === id);
}

addProduct(id: string): CartItem[] {
if (Math.random() < 0.25) {
throw new HttpException('cart failed', HttpStatus.INTERNAL_SERVER_ERROR);
}
const item = this.getItem(id);
if (item) {
item.quantity += 1;
Expand All @@ -25,7 +28,7 @@ export class CartService {
item.quantity -= 1;
} else {
this.cartProducts = this.cartProducts.filter(
cartProduct => cartProduct.productId !== id
(cartProduct) => cartProduct.productId !== id
);
}
return this.cartProducts;
Expand Down
8 changes: 4 additions & 4 deletions apps/api/src/app/delay.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor
NestInterceptor,
} from '@nestjs/common';
import { Observable, timer, throwError } from 'rxjs';
import { delay, catchError, switchMap } from 'rxjs/operators';
import { Observable, throwError, timer } from 'rxjs';
import { catchError, delay, switchMap } from 'rxjs/operators';

@Injectable()
export class DelayInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(
catchError(error => {
catchError((error) => {
return timer((Math.random() + 1) * 1000).pipe(
switchMap(() => throwError(error))
);
Expand Down
21 changes: 17 additions & 4 deletions apps/api/src/app/product/product.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Product, BasicProduct } from '@ngrx-nx-workshop/api-interfaces';
import {
HttpException,
HttpStatus,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { BasicProduct, Product } from '@ngrx-nx-workshop/api-interfaces';
import { data } from '@ngrx-nx-workshop/data';

function stripDescription(originalData: Product[]): BasicProduct[] {
return originalData.map(({description, ...product}) => product);
// Remove `description` from the object.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return originalData.map(({ description, ...product }) => product);
}

@Injectable()
export class ProductService {
getProductList(): BasicProduct[] {
if (Math.random() < 0.25) {
throw new HttpException(
'products failed',
HttpStatus.INTERNAL_SERVER_ERROR
);
}
return stripDescription(data);
}

getProduct(id: string): Product {
const product = data.find(p => p.id === id);
const product = data.find((p) => p.id === id);
if (!product) {
throw new NotFoundException(`Product with id ${id} is not found`);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/app/rating/rating.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('RatingService', () => {

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RatingService]
providers: [RatingService],
}).compile();

service = module.get<RatingService>(RatingService);
Expand Down
Loading