Skip to content

Commit deb3d38

Browse files
committed
add features api
1 parent 574e07b commit deb3d38

25 files changed

Lines changed: 170 additions & 151 deletions

File tree

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Controllers/BasketController.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Controllers
1111
{
1212
[Route("api/v1/[controller]")]
1313
[Authorize]
14-
public class BasketController : Controller
14+
[ApiController]
15+
public class BasketController : ControllerBase
1516
{
1617
private readonly ICatalogService _catalog;
1718
private readonly IBasketService _basket;
@@ -23,7 +24,7 @@ public BasketController(ICatalogService catalogService, IBasketService basketSer
2324

2425
[HttpPost]
2526
[HttpPut]
26-
public async Task<IActionResult> UpdateAllBasket([FromBody] UpdateBasketRequest data)
27+
public async Task<ActionResult<BasketData>> UpdateAllBasket([FromBody] UpdateBasketRequest data)
2728
{
2829

2930
if (data.Items == null || !data.Items.Any())
@@ -61,12 +62,13 @@ public async Task<IActionResult> UpdateAllBasket([FromBody] UpdateBasketRequest
6162
}
6263

6364
await _basket.Update(newBasket);
64-
return Ok(newBasket);
65+
66+
return newBasket;
6567
}
6668

6769
[HttpPut]
6870
[Route("items")]
69-
public async Task<IActionResult> UpdateQuantities([FromBody] UpdateBasketItemsRequest data)
71+
public async Task<ActionResult<BasketData>> UpdateQuantities([FromBody] UpdateBasketItemsRequest data)
7072
{
7173
if (!data.Updates.Any())
7274
{
@@ -93,12 +95,13 @@ public async Task<IActionResult> UpdateQuantities([FromBody] UpdateBasketItemsRe
9395

9496
// Save the updated basket
9597
await _basket.Update(currentBasket);
96-
return Ok(currentBasket);
98+
99+
return currentBasket;
97100
}
98101

99102
[HttpPost]
100103
[Route("items")]
101-
public async Task<IActionResult> AddBasketItem([FromBody] AddBasketItemRequest data)
104+
public async Task<ActionResult> AddBasketItem([FromBody] AddBasketItemRequest data)
102105
{
103106
if (data == null || data.Quantity == 0)
104107
{
@@ -126,7 +129,6 @@ public async Task<IActionResult> AddBasketItem([FromBody] AddBasketItemRequest d
126129
// Step 4: Update basket
127130
await _basket.Update(currentBasket);
128131

129-
130132
return Ok();
131133
}
132134
}

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Controllers/OrderController.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services;
4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
74
using System.Threading.Tasks;
85

96
namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Controllers
107
{
118
[Route("api/v1/[controller]")]
129
[Authorize]
13-
public class OrderController : Controller
10+
[ApiController]
11+
public class OrderController : ControllerBase
1412
{
1513
private readonly IBasketService _basketService;
1614
private readonly IOrderApiClient _orderClient;

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Builder;
77
using Microsoft.AspNetCore.Hosting;
88
using Microsoft.AspNetCore.Http;
9+
using Microsoft.AspNetCore.Mvc;
910
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config;
1011
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Filters.Basket.API.Infrastructure.Filters;
1112
using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Infrastructure;
@@ -53,9 +54,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
5354
{
5455
app.UseDeveloperExceptionPage();
5556
}
57+
else
58+
{
59+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
60+
app.UseHsts();
61+
}
5662

5763
app.UseAuthentication();
58-
64+
app.UseHttpsRedirection();
5965
app.UseMvc();
6066

6167
app.UseSwagger().UseSwaggerUI(c =>
@@ -77,7 +83,8 @@ public static IServiceCollection AddCustomMvc(this IServiceCollection services,
7783
services.AddOptions();
7884
services.Configure<UrlsConfig>(configuration.GetSection("urls"));
7985

80-
services.AddMvc();
86+
services.AddMvc()
87+
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
8188

8289
services.AddSwaggerGen(options =>
8390
{

src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/BasketController.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers
1111
{
1212
[Route("api/v1/[controller]")]
1313
[Authorize]
14-
public class BasketController : Controller
14+
[ApiController]
15+
public class BasketController : ControllerBase
1516
{
1617
private readonly ICatalogService _catalog;
1718
private readonly IBasketService _basket;
@@ -23,7 +24,7 @@ public BasketController(ICatalogService catalogService, IBasketService basketSer
2324

2425
[HttpPost]
2526
[HttpPut]
26-
public async Task<IActionResult> UpdateAllBasket([FromBody] UpdateBasketRequest data)
27+
public async Task<ActionResult<BasketData>> UpdateAllBasket([FromBody] UpdateBasketRequest data)
2728
{
2829

2930
if (data.Items == null || !data.Items.Any())
@@ -61,12 +62,13 @@ public async Task<IActionResult> UpdateAllBasket([FromBody] UpdateBasketRequest
6162
}
6263

6364
await _basket.Update(newBasket);
64-
return Ok(newBasket);
65+
66+
return newBasket;
6567
}
6668

6769
[HttpPut]
6870
[Route("items")]
69-
public async Task<IActionResult> UpdateQuantities([FromBody] UpdateBasketItemsRequest data)
71+
public async Task<ActionResult<BasketData>> UpdateQuantities([FromBody] UpdateBasketItemsRequest data)
7072
{
7173
if (!data.Updates.Any())
7274
{
@@ -93,12 +95,13 @@ public async Task<IActionResult> UpdateQuantities([FromBody] UpdateBasketItemsRe
9395

9496
// Save the updated basket
9597
await _basket.Update(currentBasket);
96-
return Ok(currentBasket);
98+
99+
return currentBasket;
97100
}
98101

99102
[HttpPost]
100103
[Route("items")]
101-
public async Task<IActionResult> AddBasketItem([FromBody] AddBasketItemRequest data)
104+
public async Task<ActionResult> AddBasketItem([FromBody] AddBasketItemRequest data)
102105
{
103106
if (data == null || data.Quantity == 0)
104107
{

src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/OrderController.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
34
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services;
45
using System;
56
using System.Collections.Generic;
@@ -10,7 +11,8 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers
1011
{
1112
[Route("api/v1/[controller]")]
1213
[Authorize]
13-
public class OrderController : Controller
14+
[ApiController]
15+
public class OrderController : ControllerBase
1416
{
1517
private readonly IBasketService _basketService;
1618
private readonly IOrderApiClient _orderClient;
@@ -22,7 +24,7 @@ public OrderController(IBasketService basketService, IOrderApiClient orderClient
2224

2325
[Route("draft/{basketId}")]
2426
[HttpGet]
25-
public async Task<IActionResult> GetOrderDraft(string basketId)
27+
public async Task<ActionResult<OrderData>> GetOrderDraft(string basketId)
2628
{
2729
if (string.IsNullOrEmpty(basketId))
2830
{
@@ -35,8 +37,7 @@ public async Task<IActionResult> GetOrderDraft(string basketId)
3537
return BadRequest($"No basket found for id {basketId}");
3638
}
3739

38-
var orderDraft = await _orderClient.GetOrderDraftFromBasket(basket);
39-
return Ok(orderDraft);
40+
return await _orderClient.GetOrderDraftFromBasket(basket);
4041
}
4142
}
4243
}

src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.AspNetCore.Builder;
33
using Microsoft.AspNetCore.Hosting;
44
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Mvc;
56
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config;
67
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Filters.Basket.API.Infrastructure.Filters;
78
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Infrastructure;
@@ -53,9 +54,14 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
5354
{
5455
app.UseDeveloperExceptionPage();
5556
}
57+
else
58+
{
59+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
60+
app.UseHsts();
61+
}
5662

5763
app.UseAuthentication();
58-
64+
app.UseHttpsRedirection();
5965
app.UseMvc();
6066

6167
app.UseSwagger().UseSwaggerUI(c =>
@@ -104,7 +110,8 @@ public static IServiceCollection AddCustomMvc(this IServiceCollection services,
104110
services.AddOptions();
105111
services.Configure<UrlsConfig>(configuration.GetSection("urls"));
106112

107-
services.AddMvc();
113+
services.AddMvc()
114+
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
108115

109116
services.AddSwaggerGen(options =>
110117
{

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
1313
{
1414
[Route("api/v1/[controller]")]
1515
[Authorize]
16-
public class BasketController : Controller
16+
[ApiController]
17+
public class BasketController : ControllerBase
1718
{
1819
private readonly IBasketRepository _repository;
1920
private readonly IIdentityService _identitySvc;
@@ -31,32 +32,31 @@ public BasketController(IBasketRepository repository,
3132
// GET /id
3233
[HttpGet("{id}")]
3334
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
34-
public async Task<IActionResult> Get(string id)
35+
public async Task<ActionResult<CustomerBasket>> Get(string id)
3536
{
3637
var basket = await _repository.GetBasketAsync(id);
38+
3739
if (basket == null)
3840
{
39-
return Ok(new CustomerBasket(id) { });
41+
return new CustomerBasket(id);
4042
}
4143

42-
return Ok(basket);
44+
return basket;
4345
}
4446

4547
// POST /value
4648
[HttpPost]
4749
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
48-
public async Task<IActionResult> Post([FromBody]CustomerBasket value)
50+
public async Task<ActionResult<CustomerBasket>> Post([FromBody]CustomerBasket value)
4951
{
50-
var basket = await _repository.UpdateBasketAsync(value);
51-
52-
return Ok(basket);
52+
return await _repository.UpdateBasketAsync(value);
5353
}
5454

5555
[Route("checkout")]
5656
[HttpPost]
5757
[ProducesResponseType((int)HttpStatusCode.Accepted)]
5858
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
59-
public async Task<IActionResult> Checkout([FromBody]BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId)
59+
public async Task<ActionResult> Checkout([FromBody]BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId)
6060
{
6161
var userId = _identitySvc.GetUserIdentity();
6262

@@ -91,6 +91,5 @@ public void Delete(string id)
9191
{
9292
_repository.DeleteBasketAsync(id);
9393
}
94-
9594
}
9695
}

src/Services/Basket/Basket.API/Startup.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.AspNetCore.Builder;
1111
using Microsoft.AspNetCore.Hosting;
1212
using Microsoft.AspNetCore.Http;
13+
using Microsoft.AspNetCore.Mvc;
1314
using Microsoft.Azure.ServiceBus;
1415
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
1516
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
@@ -51,11 +52,13 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
5152

5253
// Add framework services.
5354
services.AddMvc(options =>
54-
{
55-
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
56-
options.Filters.Add(typeof(ValidateModelStateFilter));
55+
{
56+
options.Filters.Add(typeof(HttpGlobalExceptionFilter));
57+
options.Filters.Add(typeof(ValidateModelStateFilter));
5758

58-
}).AddControllersAsServices();
59+
})
60+
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
61+
.AddControllersAsServices();
5962

6063
ConfigureAuthService(services);
6164

src/Services/Basket/Basket.UnitTests/Application/BasketWebApiTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public async Task Get_customer_basket_success()
4444
//Act
4545
var basketController = new BasketController(
4646
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
47-
var actionResult = await basketController.Get(fakeCustomerId) as OkObjectResult;
47+
var actionResult = await basketController.Get(fakeCustomerId);
4848

4949
//Assert
50-
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
50+
Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK);
5151
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
5252
}
5353

@@ -67,10 +67,10 @@ public async Task Post_customer_basket_success()
6767
var basketController = new BasketController(
6868
_basketRepositoryMock.Object, _identityServiceMock.Object, _serviceBusMock.Object);
6969

70-
var actionResult = await basketController.Post(fakeCustomerBasket) as OkObjectResult;
70+
var actionResult = await basketController.Post(fakeCustomerBasket);
7171

7272
//Assert
73-
Assert.Equal(actionResult.StatusCode, (int)System.Net.HttpStatusCode.OK);
73+
Assert.Equal((actionResult.Result as OkObjectResult).StatusCode, (int)System.Net.HttpStatusCode.OK);
7474
Assert.Equal(((CustomerBasket)actionResult.Value).BuyerId, fakeCustomerId);
7575
}
7676

0 commit comments

Comments
 (0)