Skip to content

Commit b2b01ba

Browse files
committed
Change SPA app to call basket for order checkout
1 parent e104da9 commit b2b01ba

20 files changed

Lines changed: 353 additions & 157 deletions

File tree

src/Services/Basket/Basket.API/Controllers/BasketController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ public async Task<IActionResult> Post([FromBody]CustomerBasket value)
5252
}
5353

5454
[Route("checkout")]
55-
[HttpPut]
56-
public async Task<IActionResult> Checkout([FromBody]BasketCheckout value)
55+
[HttpPost]
56+
public async Task<IActionResult> Checkout([FromBody]BasketCheckout value, [FromHeader(Name = "x-requestid")] string requestId)
5757
{
5858
var userId = _identitySvc.GetUserIdentity();
59+
value.RequestId = (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) ?
60+
guid : value.RequestId;
61+
5962
var basket = await _repository.GetBasketAsync(userId);
6063
var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, value.City, value.Street,
6164
value.State, value.Country, value.ZipCode, value.CardNumber, value.CardHolderName,

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
using AspNetCore.Http;
44
using Autofac;
55
using Autofac.Extensions.DependencyInjection;
6-
using global::Ordering.API.Application.IntegrationEvents;
7-
using global::Ordering.API.Application.IntegrationEvents.Events;
8-
using global::Ordering.API.Infrastructure.Middlewares;
96
using global::Ordering.API.Application.IntegrationCommands.Commands;
7+
using global::Ordering.API.Application.IntegrationEvents;
108
using global::Ordering.API.Application.IntegrationEvents.Events;
119
using global::Ordering.API.Application.Sagas;
10+
using global::Ordering.API.Infrastructure.Middlewares;
1211
using Infrastructure;
1312
using Infrastructure.Auth;
1413
using Infrastructure.AutofacModules;
@@ -31,7 +30,6 @@
3130
using System;
3231
using System.Data.Common;
3332
using System.Reflection;
34-
using global::Ordering.API.Application.IntegrationEvents.EventHandling;
3533

3634
public class Startup
3735
{

src/Web/WebMVC/Services/BasketService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public async Task Checkout(BasketDTO basket)
6060
var token = await GetUserTokenAsync();
6161
var updateBasketUri = API.Basket.CheckoutBasket(_remoteServiceBaseUrl);
6262

63-
var response = await _apiClient.PutAsync(updateBasketUri, basket, token);
63+
var response = await _apiClient.PostAsync(updateBasketUri, basket, token);
6464

6565
response.EnsureSuccessStatusCode();
6666
}

src/Web/WebSPA/Client/modules/basket/basket.service.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { Router } from '@angular/router';
44

55
import { DataService } from '../shared/services/data.service';
66
import { SecurityService } from '../shared/services/security.service';
7-
import { IBasket } from '../shared/models/basket.model';
7+
import { IBasket } from '../shared/models/basket.model';
8+
import { IOrder } from '../shared/models/order.model';
9+
import { IBasketCheckout } from '../shared/models/basketCheckout.model';
810
import { IBasketItem } from '../shared/models/basketItem.model';
911
import { BasketWrapperService } from '../shared/services/basket.wrapper.service';
1012
import { ConfigurationService } from '../shared/services/configuration.service';
@@ -67,6 +69,12 @@ export class BasketService {
6769
});
6870
}
6971

72+
setBasketCheckout(basketCheckout): Observable<boolean> {
73+
return this.service.postWithId(this.basketUrl + '/checkout', basketCheckout).map((response: Response) => {
74+
return true;
75+
});
76+
}
77+
7078
getBasket(): Observable<IBasket> {
7179
return this.service.get(this.basketUrl + '/' + this.basket.buyerId).map((response: Response) => {
7280
if (response.status === 204) {
@@ -83,6 +91,25 @@ export class BasketService {
8391
this.basketDropedSource.next();
8492
}
8593

94+
mapBasketInfoCheckout(order: IOrder): IBasketCheckout {
95+
let basketCheckout = <IBasketCheckout>{};
96+
97+
basketCheckout.street = order.street
98+
basketCheckout.city = order.city;
99+
basketCheckout.country = order.country;
100+
basketCheckout.state = order.state;
101+
basketCheckout.zipcode = order.zipcode;
102+
basketCheckout.cardexpiration = order.cardexpiration;
103+
basketCheckout.cardnumber = order.cardnumber;
104+
basketCheckout.cardsecuritynumber = order.cardsecuritynumber;
105+
basketCheckout.cardtypeid = order.cardtypeid;
106+
basketCheckout.cardholdername = order.cardholdername;
107+
basketCheckout.total = 0;
108+
basketCheckout.expiration = order.expiration;
109+
110+
return basketCheckout;
111+
}
112+
86113
private loadData() {
87114
this.getBasket().subscribe(basket => {
88115
if (basket != null)

src/Web/WebSPA/Client/modules/orders/orders-new/orders-new.component.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Observable } from 'rxjs/Observable';
3-
import { OrdersService } from '../orders.service';
3+
import { OrdersService } from '../orders.service';
4+
import { BasketService } from '../../basket/basket.service';
45
import { IOrder } from '../../shared/models/order.model';
56
import { BasketWrapperService } from '../../shared/services/basket.wrapper.service';
67

@@ -18,9 +19,9 @@ export class OrdersNewComponent implements OnInit {
1819
errorReceived: boolean;
1920
order: IOrder;
2021

21-
constructor(private service: OrdersService, fb: FormBuilder, private router: Router) {
22+
constructor(private orderService: OrdersService, private basketService: BasketService, fb: FormBuilder, private router: Router) {
2223
// Obtain user profile information
23-
this.order = service.mapBasketAndIdentityInfoNewOrder();
24+
this.order = orderService.mapOrderAndIdentityInfoNewOrder();
2425
this.newOrderForm = fb.group({
2526
'street': [this.order.street, Validators.required],
2627
'city': [this.order.city, Validators.required],
@@ -36,7 +37,7 @@ export class OrdersNewComponent implements OnInit {
3637
ngOnInit() {
3738
}
3839

39-
submitForm(value: any) {
40+
submitForm(value: any) {
4041
this.order.street = this.newOrderForm.controls['street'].value;
4142
this.order.city = this.newOrderForm.controls['city'].value;
4243
this.order.state = this.newOrderForm.controls['state'].value;
@@ -46,8 +47,8 @@ export class OrdersNewComponent implements OnInit {
4647
this.order.cardholdername = this.newOrderForm.controls['cardholdername'].value;
4748
this.order.cardexpiration = new Date(20 + this.newOrderForm.controls['expirationdate'].value.split('/')[1], this.newOrderForm.controls['expirationdate'].value.split('/')[0]);
4849
this.order.cardsecuritynumber = this.newOrderForm.controls['securitycode'].value;
49-
50-
this.service.postOrder(this.order)
50+
let basketCheckout = this.basketService.mapBasketInfoCheckout(this.order);
51+
this.basketService.setBasketCheckout(basketCheckout)
5152
.catch((errMessage) => {
5253
this.errorReceived = true;
5354
this.isOrderProcessing = false;

src/Web/WebSPA/Client/modules/orders/orders.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import { SharedModule } from '../shared/shared.module';
55
import { OrdersComponent } from './orders.component';
66
import { OrdersDetailComponent } from './orders-detail/orders-detail.component';
77
import { OrdersNewComponent } from './orders-new/orders-new.component';
8-
import { OrdersService } from './orders.service';
8+
import { OrdersService } from './orders.service';
9+
import { BasketService } from '../basket/basket.service';
910
import { Header } from '../shared/components/header/header';
1011

1112
@NgModule({
1213
imports: [BrowserModule, SharedModule],
1314
declarations: [OrdersComponent, OrdersDetailComponent, OrdersNewComponent],
14-
providers: [OrdersService]
15+
providers: [OrdersService, BasketService]
1516
})
1617
export class OrdersModule { }

src/Web/WebSPA/Client/modules/orders/orders.service.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,7 @@ export class OrdersService {
4444
});
4545
}
4646

47-
postOrder(item): Observable<boolean> {
48-
return this.service.postWithId(this.ordersUrl + '/api/v1/orders/new', item).map((response: Response) => {
49-
return true;
50-
});
51-
}
52-
53-
mapBasketAndIdentityInfoNewOrder(): IOrder {
47+
mapOrderAndIdentityInfoNewOrder(): IOrder {
5448
let order = <IOrder>{};
5549
let basket = this.basketService.basket;
5650
let identityInfo = this.identityService.UserData;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export interface IBasketCheckout {
2+
city: number;
3+
street: string;
4+
state: string;
5+
country: number;
6+
zipcode: string;
7+
cardnumber: string;
8+
cardexpiration: Date;
9+
expiration: string;
10+
cardsecuritynumber: string;
11+
cardholdername: string;
12+
cardtypeid: number;
13+
buyer: string;
14+
ordernumber: string;
15+
total: number;
16+
}

src/Web/WebSPA/Client/modules/shared/services/data.service.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export class DataService {
3939
return this.doPost(url, data, false, params);
4040
}
4141

42+
putWithId(url: string, data: any, params?: any): Observable<Response> {
43+
return this.doPut(url, data, true, params);
44+
}
45+
4246
private doPost(url: string, data: any, needId: boolean, params?: any): Observable<Response> {
4347
let options: RequestOptionsArgs = {};
4448

@@ -57,6 +61,24 @@ export class DataService {
5761
}).catch(this.handleError);
5862
}
5963

64+
private doPut(url: string, data: any, needId: boolean, params?: any): Observable<Response> {
65+
let options: RequestOptionsArgs = {};
66+
67+
options.headers = new Headers();
68+
if (this.securityService) {
69+
options.headers.append('Authorization', 'Bearer ' + this.securityService.GetToken());
70+
}
71+
if (needId) {
72+
let guid = Guid.newGuid();
73+
options.headers.append('x-requestid', guid);
74+
}
75+
76+
return this.http.put(url, data, options).map(
77+
(res: Response) => {
78+
return res;
79+
}).catch(this.handleError);
80+
}
81+
6082
delete(url: string, params?: any) {
6183
let options: RequestOptionsArgs = {};
6284

src/Web/WebSPA/WebSPA.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@
7272
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
7373
</ItemGroup>
7474

75-
<!-- workaround for https://github.com/aspnet/websdk/issues/114 -->
75+
<!-- workaround for https://github.com/aspnet/websdk/issues/114 --><!--
7676
<Target Name="AddGeneratedContentItems" BeforeTargets="AssignTargetPaths" DependsOnTargets="PrepareForPublish">
7777
<ItemGroup>
7878
<Content Include="wwwroot/**" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder);@(Content)" />
7979
</ItemGroup>
80-
</Target>
80+
</Target>-->
8181

8282
<ItemGroup>
8383
<ProjectReference Include="..\..\BuildingBlocks\HealthChecks\src\Microsoft.AspNetCore.HealthChecks\Microsoft.AspNetCore.HealthChecks.csproj" />

0 commit comments

Comments
 (0)