Skip to content

Commit 9e53593

Browse files
Igor SychevSychevIgor
authored andcommitted
swagge responce types and code dotnet-architecture#305
1 parent 8d033f0 commit 9e53593

8 files changed

Lines changed: 63 additions & 3 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
77
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
88
using System;
9+
using System.Net;
910
using System.Threading.Tasks;
1011

1112
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
@@ -18,16 +19,18 @@ public class BasketController : Controller
1819
private readonly IIdentityService _identitySvc;
1920
private readonly IEventBus _eventBus;
2021

21-
public BasketController(IBasketRepository repository,
22+
public BasketController(IBasketRepository repository,
2223
IIdentityService identityService,
2324
IEventBus eventBus)
2425
{
2526
_repository = repository;
2627
_identitySvc = identityService;
2728
_eventBus = eventBus;
2829
}
30+
2931
// GET /id
3032
[HttpGet("{id}")]
33+
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
3134
public async Task<IActionResult> Get(string id)
3235
{
3336
var basket = await _repository.GetBasketAsync(id);
@@ -37,6 +40,7 @@ public async Task<IActionResult> Get(string id)
3740

3841
// POST /value
3942
[HttpPost]
43+
[ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)]
4044
public async Task<IActionResult> Post([FromBody]CustomerBasket value)
4145
{
4246
var basket = await _repository.UpdateBasketAsync(value);
@@ -46,6 +50,8 @@ public async Task<IActionResult> Post([FromBody]CustomerBasket value)
4650

4751
[Route("checkout")]
4852
[HttpPost]
53+
[ProducesResponseType((int)HttpStatusCode.Accepted)]
54+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
4955
public async Task<IActionResult> Checkout([FromBody]BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId)
5056
{
5157
var userId = _identitySvc.GetUserIdentity();

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System;
1010
using System.Collections.Generic;
1111
using System.Linq;
12+
using System.Net;
1213
using System.Threading.Tasks;
1314

1415
namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
@@ -32,6 +33,7 @@ public CatalogController(CatalogContext context, IOptionsSnapshot<CatalogSetting
3233
// GET api/v1/[controller]/items[?pageSize=3&pageIndex=10]
3334
[HttpGet]
3435
[Route("[action]")]
36+
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
3537
public async Task<IActionResult> Items([FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0)
3638

3739
{
@@ -54,6 +56,8 @@ public async Task<IActionResult> Items([FromQuery]int pageSize = 10, [FromQuery]
5456

5557
[HttpGet]
5658
[Route("items/{id:int}")]
59+
[ProducesResponseType((int)HttpStatusCode.NotFound)]
60+
[ProducesResponseType(typeof(CatalogItem),(int)HttpStatusCode.OK)]
5761
public async Task<IActionResult> GetItemById(int id)
5862
{
5963
if (id <= 0)
@@ -73,6 +77,7 @@ public async Task<IActionResult> GetItemById(int id)
7377
// GET api/v1/[controller]/items/withname/samplename[?pageSize=3&pageIndex=10]
7478
[HttpGet]
7579
[Route("[action]/withname/{name:minlength(1)}")]
80+
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
7681
public async Task<IActionResult> Items(string name, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0)
7782
{
7883

@@ -97,6 +102,7 @@ public async Task<IActionResult> Items(string name, [FromQuery]int pageSize = 10
97102
// GET api/v1/[controller]/items/type/1/brand/null[?pageSize=3&pageIndex=10]
98103
[HttpGet]
99104
[Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId}")]
105+
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)]
100106
public async Task<IActionResult> Items(int? catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0)
101107
{
102108
var root = (IQueryable<CatalogItem>)_catalogContext.CatalogItems;
@@ -130,6 +136,7 @@ public async Task<IActionResult> Items(int? catalogTypeId, int? catalogBrandId,
130136
// GET api/v1/[controller]/CatalogTypes
131137
[HttpGet]
132138
[Route("[action]")]
139+
[ProducesResponseType(typeof(List<CatalogItem>), (int)HttpStatusCode.OK)]
133140
public async Task<IActionResult> CatalogTypes()
134141
{
135142
var items = await _catalogContext.CatalogTypes
@@ -141,6 +148,7 @@ public async Task<IActionResult> CatalogTypes()
141148
// GET api/v1/[controller]/CatalogBrands
142149
[HttpGet]
143150
[Route("[action]")]
151+
[ProducesResponseType(typeof(List<CatalogItem>), (int)HttpStatusCode.OK)]
144152
public async Task<IActionResult> CatalogBrands()
145153
{
146154
var items = await _catalogContext.CatalogBrands
@@ -152,6 +160,8 @@ public async Task<IActionResult> CatalogBrands()
152160
//PUT api/v1/[controller]/items
153161
[Route("items")]
154162
[HttpPut]
163+
[ProducesResponseType((int)HttpStatusCode.NotFound)]
164+
[ProducesResponseType((int)HttpStatusCode.Created)]
155165
public async Task<IActionResult> UpdateProduct([FromBody]CatalogItem productToUpdate)
156166
{
157167
var catalogItem = await _catalogContext.CatalogItems
@@ -192,6 +202,7 @@ public async Task<IActionResult> UpdateProduct([FromBody]CatalogItem productToUp
192202
//POST api/v1/[controller]/items
193203
[Route("items")]
194204
[HttpPost]
205+
[ProducesResponseType((int)HttpStatusCode.Created)]
195206
public async Task<IActionResult> CreateProduct([FromBody]CatalogItem product)
196207
{
197208
var item = new CatalogItem
@@ -213,6 +224,7 @@ public async Task<IActionResult> CreateProduct([FromBody]CatalogItem product)
213224
//DELETE api/v1/[controller]/id
214225
[Route("{id}")]
215226
[HttpDelete]
227+
[ProducesResponseType((int)HttpStatusCode.NoContent)]
216228
public async Task<IActionResult> DeleteProduct(int id)
217229
{
218230
var product = _catalogContext.CatalogItems.SingleOrDefault(x => x.Id == id);

src/Services/Catalog/Catalog.API/Controllers/PicController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.EntityFrameworkCore;
44
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
55
using System.IO;
6+
using System.Net;
67
using System.Threading.Tasks;
78

89
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
@@ -23,6 +24,8 @@ public PicController(IHostingEnvironment env,
2324

2425
[HttpGet]
2526
[Route("api/v1/catalog/items/{catalogItemId:int}/pic")]
27+
[ProducesResponseType((int)HttpStatusCode.NotFound)]
28+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
2629
// GET: /<controller>/
2730
public async Task<IActionResult> GetImage(int catalogItemId)
2831
{

src/Services/Location/Locations.API/Controllers/LocationsController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using Microsoft.AspNetCore.Authorization;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services;
4+
using Microsoft.eShopOnContainers.Services.Locations.API.Model;
45
using Microsoft.eShopOnContainers.Services.Locations.API.ViewModel;
56
using System;
7+
using System.Collections.Generic;
8+
using System.Net;
69
using System.Threading.Tasks;
710

811
namespace Locations.API.Controllers
@@ -23,6 +26,7 @@ public LocationsController(ILocationsService locationsService, IIdentityService
2326
//GET api/v1/[controller]/user/1
2427
[Route("user/{userId:guid}")]
2528
[HttpGet]
29+
[ProducesResponseType(typeof(UserLocation), (int)HttpStatusCode.OK)]
2630
public async Task<IActionResult> GetUserLocation(Guid userId)
2731
{
2832
var userLocation = await _locationsService.GetUserLocation(userId.ToString());
@@ -32,6 +36,7 @@ public async Task<IActionResult> GetUserLocation(Guid userId)
3236
//GET api/v1/[controller]/
3337
[Route("")]
3438
[HttpGet]
39+
//[ProducesResponseType(typeof(List<Locations>), (int)HttpStatusCode.OK)]
3540
public async Task<IActionResult> GetAllLocations()
3641
{
3742
var locations = await _locationsService.GetAllLocation();
@@ -41,6 +46,7 @@ public async Task<IActionResult> GetAllLocations()
4146
//GET api/v1/[controller]/1
4247
[Route("{locationId}")]
4348
[HttpGet]
49+
//[ProducesResponseType(typeof(List<Locations>), (int)HttpStatusCode.OK)]
4450
public async Task<IActionResult> GetLocation(int locationId)
4551
{
4652
var location = await _locationsService.GetLocation(locationId);
@@ -50,6 +56,8 @@ public async Task<IActionResult> GetLocation(int locationId)
5056
//POST api/v1/[controller]/
5157
[Route("")]
5258
[HttpPost]
59+
[ProducesResponseType((int)HttpStatusCode.OK)]
60+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
5361
public async Task<IActionResult> CreateOrUpdateUserLocation([FromBody]LocationRequest newLocReq)
5462
{
5563
var userId = _identityService.GetUserIdentity();

src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers
1616
using Extensions.Options;
1717
using Microsoft.eShopOnContainers.Services.Marketing.API.ViewModel;
1818
using Microsoft.AspNetCore.Http;
19+
using System.Net;
1920

2021
[Route("api/v1/[controller]")]
2122
[Authorize]
@@ -38,6 +39,7 @@ public CampaignsController(MarketingContext context,
3839
}
3940

4041
[HttpGet]
42+
[ProducesResponseType(typeof(List<CampaignDTO>), (int)HttpStatusCode.OK)]
4143
public async Task<IActionResult> GetAllCampaigns()
4244
{
4345
var campaignList = await _context.Campaigns
@@ -54,6 +56,8 @@ public async Task<IActionResult> GetAllCampaigns()
5456
}
5557

5658
[HttpGet("{id:int}")]
59+
[ProducesResponseType(typeof(CampaignDTO), (int)HttpStatusCode.OK)]
60+
[ProducesResponseType((int)HttpStatusCode.NotFound)]
5761
public async Task<IActionResult> GetCampaignById(int id)
5862
{
5963
var campaign = await _context.Campaigns
@@ -70,6 +74,8 @@ public async Task<IActionResult> GetCampaignById(int id)
7074
}
7175

7276
[HttpPost]
77+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
78+
[ProducesResponseType((int)HttpStatusCode.Created)]
7379
public async Task<IActionResult> CreateCampaign([FromBody] CampaignDTO campaignDto)
7480
{
7581
if (campaignDto is null)
@@ -86,6 +92,9 @@ public async Task<IActionResult> CreateCampaign([FromBody] CampaignDTO campaignD
8692
}
8793

8894
[HttpPut("{id:int}")]
95+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
96+
[ProducesResponseType((int)HttpStatusCode.NotFound)]
97+
[ProducesResponseType((int)HttpStatusCode.Created)]
8998
public async Task<IActionResult> UpdateCampaign(int id, [FromBody] CampaignDTO campaignDto)
9099
{
91100
if (id < 1 || campaignDto is null)
@@ -111,6 +120,9 @@ public async Task<IActionResult> UpdateCampaign(int id, [FromBody] CampaignDTO c
111120
}
112121

113122
[HttpDelete("{id:int}")]
123+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
124+
[ProducesResponseType((int)HttpStatusCode.NotFound)]
125+
[ProducesResponseType((int)HttpStatusCode.NoContent)]
114126
public async Task<IActionResult> Delete(int id)
115127
{
116128
if (id < 1)
@@ -131,6 +143,7 @@ public async Task<IActionResult> Delete(int id)
131143
}
132144

133145
[HttpGet("user")]
146+
[ProducesResponseType(typeof(PaginatedItemsViewModel<CampaignDTO>), (int)HttpStatusCode.OK)]
134147
public async Task<IActionResult> GetCampaignsByUserId( int pageSize = 10, int pageIndex = 0)
135148
{
136149
var userId = _identityService.GetUserIdentity();

src/Services/Marketing/Marketing.API/Controllers/LocationsController.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers
77
using Microsoft.eShopOnContainers.Services.Marketing.API.Model;
88
using System.Collections.Generic;
99
using System.Linq;
10+
using System.Net;
1011
using System.Threading.Tasks;
1112

1213
[Authorize]
@@ -21,6 +22,9 @@ public LocationsController(MarketingContext context)
2122

2223
[HttpGet]
2324
[Route("api/v1/campaigns/{campaignId:int}/locations/{userLocationRuleId:int}")]
25+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
26+
[ProducesResponseType((int)HttpStatusCode.NotFound)]
27+
[ProducesResponseType(typeof(UserLocationRuleDTO),(int)HttpStatusCode.OK)]
2428
public IActionResult GetLocationByCampaignAndLocationRuleId(int campaignId,
2529
int userLocationRuleId)
2630
{
@@ -45,6 +49,9 @@ public IActionResult GetLocationByCampaignAndLocationRuleId(int campaignId,
4549

4650
[HttpGet]
4751
[Route("api/v1/campaigns/{campaignId:int}/locations")]
52+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
53+
[ProducesResponseType((int)HttpStatusCode.OK)]
54+
[ProducesResponseType(typeof(List<UserLocationRuleDTO>), (int)HttpStatusCode.OK)]
4855
public IActionResult GetAllLocationsByCampaignId(int campaignId)
4956
{
5057
if (campaignId < 1)
@@ -69,6 +76,8 @@ public IActionResult GetAllLocationsByCampaignId(int campaignId)
6976

7077
[HttpPost]
7178
[Route("api/v1/campaigns/{campaignId:int}/locations")]
79+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
80+
[ProducesResponseType((int)HttpStatusCode.Created)]
7281
public async Task<IActionResult> CreateLocation(int campaignId,
7382
[FromBody] UserLocationRuleDTO locationRuleDto)
7483
{
@@ -89,6 +98,8 @@ public async Task<IActionResult> CreateLocation(int campaignId,
8998

9099
[HttpDelete]
91100
[Route("api/v1/campaigns/{campaignId:int}/locations/{userLocationRuleId:int}")]
101+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
102+
[ProducesResponseType((int)HttpStatusCode.NotFound)]
92103
public async Task<IActionResult> DeleteLocationById(int campaignId, int userLocationRuleId)
93104
{
94105
if (campaignId < 1 || userLocationRuleId < 1)

src/Services/Marketing/Marketing.API/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
"IIS Express": {
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
14-
"launchUrl": "api/values",
14+
"launchUrl": "swagger",
1515
"environmentVariables": {
1616
"ASPNETCORE_ENVIRONMENT": "Development"
1717
}
1818
},
1919
"Marketing.API": {
2020
"commandName": "Project",
2121
"launchBrowser": true,
22-
"launchUrl": "api/values",
22+
"launchUrl": "swagger",
2323
"environmentVariables": {
2424
"ASPNETCORE_ENVIRONMENT": "Development"
2525
},

src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Ordering.API.Application.Commands;
88
using System;
99
using System.Collections.Generic;
10+
using System.Net;
1011
using System.Threading.Tasks;
1112

1213
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
@@ -29,6 +30,8 @@ public OrdersController(IMediator mediator, IOrderQueries orderQueries, IIdentit
2930

3031
[Route("cancel")]
3132
[HttpPut]
33+
[ProducesResponseType((int)HttpStatusCode.OK)]
34+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
3235
public async Task<IActionResult> CancelOrder([FromBody]CancelOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId)
3336
{
3437
bool commandResult = false;
@@ -44,6 +47,8 @@ public async Task<IActionResult> CancelOrder([FromBody]CancelOrderCommand comman
4447

4548
[Route("ship")]
4649
[HttpPut]
50+
[ProducesResponseType((int)HttpStatusCode.OK)]
51+
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
4752
public async Task<IActionResult> ShipOrder([FromBody]ShipOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId)
4853
{
4954
bool commandResult = false;
@@ -59,6 +64,8 @@ public async Task<IActionResult> ShipOrder([FromBody]ShipOrderCommand command, [
5964

6065
[Route("{orderId:int}")]
6166
[HttpGet]
67+
[ProducesResponseType((int)HttpStatusCode.OK)]
68+
[ProducesResponseType((int)HttpStatusCode.NotFound)]
6269
public async Task<IActionResult> GetOrder(int orderId)
6370
{
6471
try

0 commit comments

Comments
 (0)