From 9e535930cebfdad35921cd2aa707157e9d6d8746 Mon Sep 17 00:00:00 2001 From: Igor Sychev Date: Mon, 9 Oct 2017 18:45:59 +0100 Subject: [PATCH] swagge responce types and code https://github.com/dotnet-architecture/eShopOnContainers/issues/305 --- .../Basket.API/Controllers/BasketController.cs | 8 +++++++- .../Catalog.API/Controllers/CatalogController.cs | 12 ++++++++++++ .../Catalog.API/Controllers/PicController.cs | 3 +++ .../Controllers/LocationsController.cs | 8 ++++++++ .../Controllers/CampaignsController.cs | 13 +++++++++++++ .../Controllers/LocationsController.cs | 11 +++++++++++ .../Marketing.API/Properties/launchSettings.json | 4 ++-- .../Ordering.API/Controllers/OrdersController.cs | 7 +++++++ 8 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/Services/Basket/Basket.API/Controllers/BasketController.cs b/src/Services/Basket/Basket.API/Controllers/BasketController.cs index dff9ba54a..18a3b221d 100644 --- a/src/Services/Basket/Basket.API/Controllers/BasketController.cs +++ b/src/Services/Basket/Basket.API/Controllers/BasketController.cs @@ -6,6 +6,7 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.Services.Basket.API.Model; using Microsoft.eShopOnContainers.Services.Basket.API.Services; using System; +using System.Net; using System.Threading.Tasks; namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers @@ -18,7 +19,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers private readonly IIdentityService _identitySvc; private readonly IEventBus _eventBus; - public BasketController(IBasketRepository repository, + public BasketController(IBasketRepository repository, IIdentityService identityService, IEventBus eventBus) { @@ -26,8 +27,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers _identitySvc = identityService; _eventBus = eventBus; } + // GET /id [HttpGet("{id}")] + [ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)] public async Task Get(string id) { var basket = await _repository.GetBasketAsync(id); @@ -37,6 +40,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers // POST /value [HttpPost] + [ProducesResponseType(typeof(CustomerBasket), (int)HttpStatusCode.OK)] public async Task Post([FromBody]CustomerBasket value) { var basket = await _repository.UpdateBasketAsync(value); @@ -46,6 +50,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers [Route("checkout")] [HttpPost] + [ProducesResponseType((int)HttpStatusCode.Accepted)] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] public async Task Checkout([FromBody]BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId) { var userId = _identitySvc.GetUserIdentity(); diff --git a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs index fe52b9128..1830fd90a 100644 --- a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs +++ b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Threading.Tasks; namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers @@ -32,6 +33,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers // GET api/v1/[controller]/items[?pageSize=3&pageIndex=10] [HttpGet] [Route("[action]")] + [ProducesResponseType(typeof(PaginatedItemsViewModel), (int)HttpStatusCode.OK)] public async Task Items([FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) { @@ -54,6 +56,8 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers [HttpGet] [Route("items/{id:int}")] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + [ProducesResponseType(typeof(CatalogItem),(int)HttpStatusCode.OK)] public async Task GetItemById(int id) { if (id <= 0) @@ -73,6 +77,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers // GET api/v1/[controller]/items/withname/samplename[?pageSize=3&pageIndex=10] [HttpGet] [Route("[action]/withname/{name:minlength(1)}")] + [ProducesResponseType(typeof(PaginatedItemsViewModel), (int)HttpStatusCode.OK)] public async Task Items(string name, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) { @@ -97,6 +102,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers // GET api/v1/[controller]/items/type/1/brand/null[?pageSize=3&pageIndex=10] [HttpGet] [Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId}")] + [ProducesResponseType(typeof(PaginatedItemsViewModel), (int)HttpStatusCode.OK)] public async Task Items(int? catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) { var root = (IQueryable)_catalogContext.CatalogItems; @@ -130,6 +136,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers // GET api/v1/[controller]/CatalogTypes [HttpGet] [Route("[action]")] + [ProducesResponseType(typeof(List), (int)HttpStatusCode.OK)] public async Task CatalogTypes() { var items = await _catalogContext.CatalogTypes @@ -141,6 +148,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers // GET api/v1/[controller]/CatalogBrands [HttpGet] [Route("[action]")] + [ProducesResponseType(typeof(List), (int)HttpStatusCode.OK)] public async Task CatalogBrands() { var items = await _catalogContext.CatalogBrands @@ -152,6 +160,8 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers //PUT api/v1/[controller]/items [Route("items")] [HttpPut] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + [ProducesResponseType((int)HttpStatusCode.Created)] public async Task UpdateProduct([FromBody]CatalogItem productToUpdate) { var catalogItem = await _catalogContext.CatalogItems @@ -192,6 +202,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers //POST api/v1/[controller]/items [Route("items")] [HttpPost] + [ProducesResponseType((int)HttpStatusCode.Created)] public async Task CreateProduct([FromBody]CatalogItem product) { var item = new CatalogItem @@ -213,6 +224,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers //DELETE api/v1/[controller]/id [Route("{id}")] [HttpDelete] + [ProducesResponseType((int)HttpStatusCode.NoContent)] public async Task DeleteProduct(int id) { var product = _catalogContext.CatalogItems.SingleOrDefault(x => x.Id == id); diff --git a/src/Services/Catalog/Catalog.API/Controllers/PicController.cs b/src/Services/Catalog/Catalog.API/Controllers/PicController.cs index 7043ce9f7..f8e986e50 100644 --- a/src/Services/Catalog/Catalog.API/Controllers/PicController.cs +++ b/src/Services/Catalog/Catalog.API/Controllers/PicController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; using System.IO; +using System.Net; using System.Threading.Tasks; // For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 @@ -23,6 +24,8 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers [HttpGet] [Route("api/v1/catalog/items/{catalogItemId:int}/pic")] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] // GET: // public async Task GetImage(int catalogItemId) { diff --git a/src/Services/Location/Locations.API/Controllers/LocationsController.cs b/src/Services/Location/Locations.API/Controllers/LocationsController.cs index a61c7f2d0..45f4d2ba7 100644 --- a/src/Services/Location/Locations.API/Controllers/LocationsController.cs +++ b/src/Services/Location/Locations.API/Controllers/LocationsController.cs @@ -1,8 +1,11 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services; +using Microsoft.eShopOnContainers.Services.Locations.API.Model; using Microsoft.eShopOnContainers.Services.Locations.API.ViewModel; using System; +using System.Collections.Generic; +using System.Net; using System.Threading.Tasks; namespace Locations.API.Controllers @@ -23,6 +26,7 @@ namespace Locations.API.Controllers //GET api/v1/[controller]/user/1 [Route("user/{userId:guid}")] [HttpGet] + [ProducesResponseType(typeof(UserLocation), (int)HttpStatusCode.OK)] public async Task GetUserLocation(Guid userId) { var userLocation = await _locationsService.GetUserLocation(userId.ToString()); @@ -32,6 +36,7 @@ namespace Locations.API.Controllers //GET api/v1/[controller]/ [Route("")] [HttpGet] + //[ProducesResponseType(typeof(List), (int)HttpStatusCode.OK)] public async Task GetAllLocations() { var locations = await _locationsService.GetAllLocation(); @@ -41,6 +46,7 @@ namespace Locations.API.Controllers //GET api/v1/[controller]/1 [Route("{locationId}")] [HttpGet] + //[ProducesResponseType(typeof(List), (int)HttpStatusCode.OK)] public async Task GetLocation(int locationId) { var location = await _locationsService.GetLocation(locationId); @@ -50,6 +56,8 @@ namespace Locations.API.Controllers //POST api/v1/[controller]/ [Route("")] [HttpPost] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] public async Task CreateOrUpdateUserLocation([FromBody]LocationRequest newLocReq) { var userId = _identityService.GetUserIdentity(); diff --git a/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs b/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs index 146802626..313d4d77b 100644 --- a/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs +++ b/src/Services/Marketing/Marketing.API/Controllers/CampaignsController.cs @@ -16,6 +16,7 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers using Extensions.Options; using Microsoft.eShopOnContainers.Services.Marketing.API.ViewModel; using Microsoft.AspNetCore.Http; + using System.Net; [Route("api/v1/[controller]")] [Authorize] @@ -38,6 +39,7 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers } [HttpGet] + [ProducesResponseType(typeof(List), (int)HttpStatusCode.OK)] public async Task GetAllCampaigns() { var campaignList = await _context.Campaigns @@ -54,6 +56,8 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers } [HttpGet("{id:int}")] + [ProducesResponseType(typeof(CampaignDTO), (int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetCampaignById(int id) { var campaign = await _context.Campaigns @@ -70,6 +74,8 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers } [HttpPost] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] + [ProducesResponseType((int)HttpStatusCode.Created)] public async Task CreateCampaign([FromBody] CampaignDTO campaignDto) { if (campaignDto is null) @@ -86,6 +92,9 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers } [HttpPut("{id:int}")] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + [ProducesResponseType((int)HttpStatusCode.Created)] public async Task UpdateCampaign(int id, [FromBody] CampaignDTO campaignDto) { if (id < 1 || campaignDto is null) @@ -111,6 +120,9 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers } [HttpDelete("{id:int}")] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + [ProducesResponseType((int)HttpStatusCode.NoContent)] public async Task Delete(int id) { if (id < 1) @@ -131,6 +143,7 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers } [HttpGet("user")] + [ProducesResponseType(typeof(PaginatedItemsViewModel), (int)HttpStatusCode.OK)] public async Task GetCampaignsByUserId( int pageSize = 10, int pageIndex = 0) { var userId = _identityService.GetUserIdentity(); diff --git a/src/Services/Marketing/Marketing.API/Controllers/LocationsController.cs b/src/Services/Marketing/Marketing.API/Controllers/LocationsController.cs index 57f652d4c..0d47a63a4 100644 --- a/src/Services/Marketing/Marketing.API/Controllers/LocationsController.cs +++ b/src/Services/Marketing/Marketing.API/Controllers/LocationsController.cs @@ -7,6 +7,7 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers using Microsoft.eShopOnContainers.Services.Marketing.API.Model; using System.Collections.Generic; using System.Linq; + using System.Net; using System.Threading.Tasks; [Authorize] @@ -21,6 +22,9 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers [HttpGet] [Route("api/v1/campaigns/{campaignId:int}/locations/{userLocationRuleId:int}")] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + [ProducesResponseType(typeof(UserLocationRuleDTO),(int)HttpStatusCode.OK)] public IActionResult GetLocationByCampaignAndLocationRuleId(int campaignId, int userLocationRuleId) { @@ -45,6 +49,9 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers [HttpGet] [Route("api/v1/campaigns/{campaignId:int}/locations")] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType(typeof(List), (int)HttpStatusCode.OK)] public IActionResult GetAllLocationsByCampaignId(int campaignId) { if (campaignId < 1) @@ -69,6 +76,8 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers [HttpPost] [Route("api/v1/campaigns/{campaignId:int}/locations")] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] + [ProducesResponseType((int)HttpStatusCode.Created)] public async Task CreateLocation(int campaignId, [FromBody] UserLocationRuleDTO locationRuleDto) { @@ -89,6 +98,8 @@ namespace Microsoft.eShopOnContainers.Services.Marketing.API.Controllers [HttpDelete] [Route("api/v1/campaigns/{campaignId:int}/locations/{userLocationRuleId:int}")] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] + [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task DeleteLocationById(int campaignId, int userLocationRuleId) { if (campaignId < 1 || userLocationRuleId < 1) diff --git a/src/Services/Marketing/Marketing.API/Properties/launchSettings.json b/src/Services/Marketing/Marketing.API/Properties/launchSettings.json index ec47d57ce..e222e065d 100644 --- a/src/Services/Marketing/Marketing.API/Properties/launchSettings.json +++ b/src/Services/Marketing/Marketing.API/Properties/launchSettings.json @@ -11,7 +11,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "api/values", + "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -19,7 +19,7 @@ "Marketing.API": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "api/values", + "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, diff --git a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs index 1e40910be..c43e65d63 100644 --- a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs +++ b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs @@ -7,6 +7,7 @@ using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services; using Ordering.API.Application.Commands; using System; using System.Collections.Generic; +using System.Net; using System.Threading.Tasks; namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers @@ -29,6 +30,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers [Route("cancel")] [HttpPut] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] public async Task CancelOrder([FromBody]CancelOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId) { bool commandResult = false; @@ -44,6 +47,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers [Route("ship")] [HttpPut] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.BadRequest)] public async Task ShipOrder([FromBody]ShipOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId) { bool commandResult = false; @@ -59,6 +64,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers [Route("{orderId:int}")] [HttpGet] + [ProducesResponseType((int)HttpStatusCode.OK)] + [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetOrder(int orderId) { try