File tree Expand file tree Collapse file tree
Services/Basket/Basket.API
Web/WebSPA/Client/modules Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88
99namespace Basket . API . Infrastructure . Filters
1010{
11- public class HttpGlobalExceptionFilter : IExceptionFilter
11+ public partial class HttpGlobalExceptionFilter : IExceptionFilter
1212 {
1313 private readonly IHostingEnvironment env ;
1414 private readonly ILogger < HttpGlobalExceptionFilter > logger ;
@@ -52,12 +52,5 @@ public void OnException(ExceptionContext context)
5252 }
5353 context . ExceptionHandled = true ;
5454 }
55-
56- private class JsonErrorResponse
57- {
58- public string [ ] Messages { get ; set ; }
59-
60- public object DeveloperMessage { get ; set ; }
61- }
6255 }
6356}
Original file line number Diff line number Diff line change 1+ namespace Basket . API . Infrastructure . Filters
2+ {
3+ public class JsonErrorResponse
4+ {
5+ public string [ ] Messages { get ; set ; }
6+
7+ public object DeveloperMessage { get ; set ; }
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ using System . Linq ;
2+ using Microsoft . AspNetCore . Mvc ;
3+ using Microsoft . AspNetCore . Mvc . Filters ;
4+
5+ namespace Basket . API . Infrastructure . Filters
6+ {
7+ public class ValidateModelStateFilter : ActionFilterAttribute
8+ {
9+ public override void OnActionExecuting ( ActionExecutingContext context )
10+ {
11+ if ( context . ModelState . IsValid )
12+ {
13+ return ;
14+ }
15+
16+ var validationErrors = context . ModelState
17+ . Keys
18+ . SelectMany ( k => context . ModelState [ k ] . Errors )
19+ . Select ( e => e . ErrorMessage )
20+ . ToArray ( ) ;
21+
22+ var json = new JsonErrorResponse
23+ {
24+ Messages = validationErrors
25+ } ;
26+
27+ context . Result = new BadRequestObjectResult ( json ) ;
28+ }
29+ }
30+ }
Original file line number Diff line number Diff line change 1- namespace Microsoft . eShopOnContainers . Services . Basket . API . Model
1+ using System . Collections . Generic ;
2+ using System . ComponentModel . DataAnnotations ;
3+
4+ namespace Microsoft . eShopOnContainers . Services . Basket . API . Model
25{
3- public class BasketItem
6+ public class BasketItem : IValidatableObject
47 {
58 public string Id { get ; set ; }
69 public string ProductId { get ; set ; }
@@ -9,5 +12,16 @@ public class BasketItem
912 public decimal OldUnitPrice { get ; set ; }
1013 public int Quantity { get ; set ; }
1114 public string PictureUrl { get ; set ; }
15+ public IEnumerable < ValidationResult > Validate ( ValidationContext validationContext )
16+ {
17+ var results = new List < ValidationResult > ( ) ;
18+
19+ if ( Quantity < 1 )
20+ {
21+ results . Add ( new ValidationResult ( "Invalid number of units" , new [ ] { "Quantity" } ) ) ;
22+ }
23+
24+ return results ;
25+ }
1226 }
1327}
Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
5454 services . AddMvc ( options =>
5555 {
5656 options . Filters . Add ( typeof ( HttpGlobalExceptionFilter ) ) ;
57+ options . Filters . Add ( typeof ( ValidateModelStateFilter ) ) ;
5758 } ) . AddControllersAsServices ( ) ;
5859
5960 services . AddHealthChecks ( checks =>
Original file line number Diff line number Diff line change 11< div class ="esh-basket ">
22 < esh-header url ="/catalog "> Back to catalog</ esh-header >
3-
3+
44 < div class ="container ">
5+ < div *ngFor ="let errorMessage of errorMessages ">
6+ < div class ="esh-basket-items-margin-left1 row ">
7+ < div class ="alert alert-warning " role ="alert "> {{errorMessage}}</ div >
8+ </ div >
9+ </ div >
10+
511 < article class ="esh-basket-titles row ">
612 < section class ="esh-basket-title col-xs-3 "> Product</ section >
713 < section class ="esh-basket-title col-xs-3 hidden-lg-down "> </ section >
1420 < article class ="esh-basket-items row ">
1521
1622 < section class ="esh-basket-item esh-basket-item--middle col-lg-3 hidden-lg-down ">
17- < img class ="esh-basket-image " src ="{{item.pictureUrl}} " />
23+ < img class ="esh-basket-image " src ="{{item.pictureUrl}} "/>
1824 </ section >
1925 < section class ="esh-basket-item esh-basket-item--middle col-xs-3 "> {{item.productName}}</ section >
2026 < section class ="esh-basket-item esh-basket-item--middle col-xs-2 "> $ {{item.unitPrice | number:'.2-2'}}</ section >
2329 type ="number "
2430 min ="1 "
2531 [(ngModel)] ="item.quantity "
26- (change) ="itemQuantityChanged(item) " />
32+ (change) ="itemQuantityChanged(item) "/>
2733 </ section >
2834 < section class ="esh-basket-item esh-basket-item--middle esh-basket-item--mark col-xs-2 "> $ {{(item.unitPrice * item.quantity) | number:'.2-2'}}</ section >
2935 </ article >
Original file line number Diff line number Diff line change 11import { Component , OnInit } from '@angular/core' ;
22import { Router } from '@angular/router' ;
33
4+ import 'rxjs/Rx' ;
5+ import { Observable } from 'rxjs/Observable' ;
6+ import 'rxjs/add/observable/throw' ;
7+
48import { BasketService } from './basket.service' ;
59import { IBasket } from '../shared/models/basket.model' ;
610import { IBasketItem } from '../shared/models/basketItem.model' ;
@@ -12,6 +16,7 @@ import { BasketWrapperService } from '../shared/services/basket.wrapper.service'
1216 templateUrl : './basket.component.html'
1317} )
1418export class BasketComponent implements OnInit {
19+ errorMessages : any ;
1520 basket : IBasket ;
1621 totalPrice : number = 0 ;
1722
@@ -30,7 +35,10 @@ export class BasketComponent implements OnInit {
3035 }
3136
3237 update ( event : any ) {
33- this . service . setBasket ( this . basket ) . subscribe ( x => console . log ( 'basket updated: ' + x ) ) ;
38+ this . service . setBasket ( this . basket ) . catch ( ( errMessage ) => {
39+ this . errorMessages = errMessage . messages ;
40+ return Observable . throw ( errMessage ) ;
41+ } ) . subscribe ( x => console . log ( 'basket updated: ' + x ) ) ;
3442 }
3543
3644 checkOut ( event : any ) {
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ export class OrdersComponent implements OnInit {
2020
2121 ngOnInit ( ) {
2222 if ( this . configurationService . isReady ) {
23- this . getOrders ( )
23+ this . getOrders ( ) ;
2424 } else {
2525 this . configurationService . settingsLoaded$ . subscribe ( x => {
2626 this . getOrders ( ) ;
@@ -31,7 +31,7 @@ export class OrdersComponent implements OnInit {
3131 this . interval = setTimeout ( ( ) => {
3232 this . service . getOrders ( ) . subscribe ( orders => {
3333 this . orders = orders ;
34- if ( this . orders . length != this . oldOrders . length ) {
34+ if ( this . orders . length !== this . oldOrders . length ) {
3535 clearInterval ( this . interval ) ;
3636 }
3737 } ) ;
Original file line number Diff line number Diff line change @@ -102,7 +102,7 @@ export class DataService {
102102 if ( error instanceof Response ) {
103103 let errMessage = '' ;
104104 try {
105- errMessage = error . json ( ) . error ;
105+ errMessage = error . json ( ) ;
106106 } catch ( err ) {
107107 errMessage = error . statusText ;
108108 }
You can’t perform that action at this time.
0 commit comments