|
|
@ -28,7 +28,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
_catalogIntegrationEventService = catalogIntegrationEventService ?? throw new ArgumentNullException(nameof(catalogIntegrationEventService)); |
|
|
|
_settings = settings.Value; |
|
|
|
|
|
|
|
((DbContext)context).ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; |
|
|
|
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; |
|
|
|
} |
|
|
|
|
|
|
|
// GET api/v1/[controller]/items[?pageSize=3&pageIndex=10]
|
|
|
@ -36,11 +36,19 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
[Route("items")] |
|
|
|
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)] |
|
|
|
[ProducesResponseType(typeof(IEnumerable<CatalogItem>), (int)HttpStatusCode.OK)] |
|
|
|
public async Task<IActionResult> Items([FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0, [FromQuery] string ids = null) |
|
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest)] |
|
|
|
public async Task<IActionResult> ItemsAsync([FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0, string ids = null) |
|
|
|
{ |
|
|
|
if (!string.IsNullOrEmpty(ids)) |
|
|
|
{ |
|
|
|
return GetItemsByIds(ids); |
|
|
|
var items = await GetItemsByIdsAsync(ids); |
|
|
|
|
|
|
|
if (!items.Any()) |
|
|
|
{ |
|
|
|
return BadRequest("ids value invalid. Must be comma-separated list of numbers"); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(items); |
|
|
|
} |
|
|
|
|
|
|
|
var totalItems = await _catalogContext.CatalogItems |
|
|
@ -54,37 +62,36 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
|
|
|
|
itemsOnPage = ChangeUriPlaceholder(itemsOnPage); |
|
|
|
|
|
|
|
var model = new PaginatedItemsViewModel<CatalogItem>( |
|
|
|
pageIndex, pageSize, totalItems, itemsOnPage); |
|
|
|
var model = new PaginatedItemsViewModel<CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage); |
|
|
|
|
|
|
|
return Ok(model); |
|
|
|
} |
|
|
|
|
|
|
|
private IActionResult GetItemsByIds(string ids) |
|
|
|
private async Task<List<CatalogItem>> GetItemsByIdsAsync(string ids) |
|
|
|
{ |
|
|
|
var numIds = ids.Split(',') |
|
|
|
.Select(id => (Ok: int.TryParse(id, out int x), Value: x)); |
|
|
|
var numIds = ids.Split(',').Select(id => (Ok: int.TryParse(id, out int x), Value: x)); |
|
|
|
|
|
|
|
if (!numIds.All(nid => nid.Ok)) |
|
|
|
{ |
|
|
|
return BadRequest("ids value invalid. Must be comma-separated list of numbers"); |
|
|
|
return new List<CatalogItem>(); |
|
|
|
} |
|
|
|
|
|
|
|
var idsToSelect = numIds |
|
|
|
.Select(id => id.Value); |
|
|
|
|
|
|
|
var items = _catalogContext.CatalogItems.Where(ci => idsToSelect.Contains(ci.Id)).ToList(); |
|
|
|
var items = await _catalogContext.CatalogItems.Where(ci => idsToSelect.Contains(ci.Id)).ToListAsync(); |
|
|
|
|
|
|
|
items = ChangeUriPlaceholder(items); |
|
|
|
|
|
|
|
return Ok(items); |
|
|
|
return items; |
|
|
|
} |
|
|
|
|
|
|
|
[HttpGet] |
|
|
|
[Route("items/{id:int}")] |
|
|
|
[ProducesResponseType((int)HttpStatusCode.NotFound)] |
|
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest)] |
|
|
|
[ProducesResponseType(typeof(CatalogItem), (int)HttpStatusCode.OK)] |
|
|
|
public async Task<ActionResult<CatalogItem>> GetItemById(int id) |
|
|
|
public async Task<ActionResult<CatalogItem>> ItemByIdAsync(int id) |
|
|
|
{ |
|
|
|
if (id <= 0) |
|
|
|
{ |
|
|
@ -95,6 +102,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
|
|
|
|
var baseUri = _settings.PicBaseUrl; |
|
|
|
var azureStorageEnabled = _settings.AzureStorageEnabled; |
|
|
|
|
|
|
|
item.FillProductUrl(baseUri, azureStorageEnabled: azureStorageEnabled); |
|
|
|
|
|
|
|
if (item != null) |
|
|
@ -107,9 +115,9 @@ 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)}")]
|
|
|
|
[Route("items/withname/{name:minlength(1)}")] |
|
|
|
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)] |
|
|
|
public async Task<ActionResult<PaginatedItemsViewModel<CatalogItem>>> Items(string name, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) |
|
|
|
public async Task<ActionResult<PaginatedItemsViewModel<CatalogItem>>> ItemsWithNameAsync(string name, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) |
|
|
|
{ |
|
|
|
var totalItems = await _catalogContext.CatalogItems |
|
|
|
.Where(c => c.Name.StartsWith(name)) |
|
|
@ -123,14 +131,14 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
|
|
|
|
itemsOnPage = ChangeUriPlaceholder(itemsOnPage); |
|
|
|
|
|
|
|
return new PaginatedItemsViewModel<CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage); |
|
|
|
return new PaginatedItemsViewModel<CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage); |
|
|
|
} |
|
|
|
|
|
|
|
// GET api/v1/[controller]/items/type/1/brand[?pageSize=3&pageIndex=10]
|
|
|
|
[HttpGet] |
|
|
|
[Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId:int?}")]
|
|
|
|
[Route("items/type/{catalogTypeId}/brand/{catalogBrandId:int?}")] |
|
|
|
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)] |
|
|
|
public async Task<ActionResult<PaginatedItemsViewModel<CatalogItem>>> Items(int catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) |
|
|
|
public async Task<ActionResult<PaginatedItemsViewModel<CatalogItem>>> ItemsByTypeIdAndBrandIdAsync(int catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) |
|
|
|
{ |
|
|
|
var root = (IQueryable<CatalogItem>)_catalogContext.CatalogItems; |
|
|
|
|
|
|
@ -153,11 +161,12 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
|
|
|
|
return new PaginatedItemsViewModel<CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage); |
|
|
|
} |
|
|
|
|
|
|
|
// GET api/v1/[controller]/items/type/all/brand[?pageSize=3&pageIndex=10]
|
|
|
|
[HttpGet] |
|
|
|
[Route("[action]/type/all/brand/{catalogBrandId:int?}")]
|
|
|
|
[Route("items/type/all/brand/{catalogBrandId:int?}")] |
|
|
|
[ProducesResponseType(typeof(PaginatedItemsViewModel<CatalogItem>), (int)HttpStatusCode.OK)] |
|
|
|
public async Task<ActionResult<PaginatedItemsViewModel<CatalogItem>>> Items(int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) |
|
|
|
public async Task<ActionResult<PaginatedItemsViewModel<CatalogItem>>> ItemsByBrandIdAsync(int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) |
|
|
|
{ |
|
|
|
var root = (IQueryable<CatalogItem>)_catalogContext.CatalogItems; |
|
|
|
|
|
|
@ -181,18 +190,18 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
|
|
|
|
// GET api/v1/[controller]/CatalogTypes
|
|
|
|
[HttpGet] |
|
|
|
[Route("[action]")]
|
|
|
|
[Route("catalogtypes")] |
|
|
|
[ProducesResponseType(typeof(List<CatalogType>), (int)HttpStatusCode.OK)] |
|
|
|
public async Task<ActionResult<List<CatalogType>>> CatalogTypes() |
|
|
|
public async Task<ActionResult<List<CatalogType>>> CatalogTypesAsync() |
|
|
|
{ |
|
|
|
return await _catalogContext.CatalogTypes.ToListAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
// GET api/v1/[controller]/CatalogBrands
|
|
|
|
[HttpGet] |
|
|
|
[Route("[action]")]
|
|
|
|
[Route("catalogbrands")] |
|
|
|
[ProducesResponseType(typeof(List<CatalogBrand>), (int)HttpStatusCode.OK)] |
|
|
|
public async Task<ActionResult<List<CatalogBrand>>> CatalogBrands() |
|
|
|
public async Task<ActionResult<List<CatalogBrand>>> CatalogBrandsAsync() |
|
|
|
{ |
|
|
|
return await _catalogContext.CatalogBrands.ToListAsync(); |
|
|
|
} |
|
|
@ -202,10 +211,9 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
[HttpPut] |
|
|
|
[ProducesResponseType((int)HttpStatusCode.NotFound)] |
|
|
|
[ProducesResponseType((int)HttpStatusCode.Created)] |
|
|
|
public async Task<ActionResult> UpdateProduct([FromBody]CatalogItem productToUpdate) |
|
|
|
public async Task<ActionResult> UpdateProductAsync([FromBody]CatalogItem productToUpdate) |
|
|
|
{ |
|
|
|
var catalogItem = await _catalogContext.CatalogItems |
|
|
|
.SingleOrDefaultAsync(i => i.Id == productToUpdate.Id); |
|
|
|
var catalogItem = await _catalogContext.CatalogItems.SingleOrDefaultAsync(i => i.Id == productToUpdate.Id); |
|
|
|
|
|
|
|
if (catalogItem == null) |
|
|
|
{ |
|
|
@ -215,7 +223,6 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
var oldPrice = catalogItem.Price; |
|
|
|
var raiseProductPriceChangedEvent = oldPrice != productToUpdate.Price; |
|
|
|
|
|
|
|
|
|
|
|
// Update current product
|
|
|
|
catalogItem = productToUpdate; |
|
|
|
_catalogContext.CatalogItems.Update(catalogItem); |
|
|
@ -236,14 +243,14 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
await _catalogContext.SaveChangesAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
return CreatedAtAction(nameof(GetItemById), new { id = productToUpdate.Id }, null); |
|
|
|
return CreatedAtAction(nameof(ItemByIdAsync), new { id = productToUpdate.Id }, null); |
|
|
|
} |
|
|
|
|
|
|
|
//POST api/v1/[controller]/items
|
|
|
|
[Route("items")] |
|
|
|
[HttpPost] |
|
|
|
[ProducesResponseType((int)HttpStatusCode.Created)] |
|
|
|
public async Task<ActionResult> CreateProduct([FromBody]CatalogItem product) |
|
|
|
public async Task<ActionResult> CreateProductAsync([FromBody]CatalogItem product) |
|
|
|
{ |
|
|
|
var item = new CatalogItem |
|
|
|
{ |
|
|
@ -254,18 +261,20 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
PictureFileName = product.PictureFileName, |
|
|
|
Price = product.Price |
|
|
|
}; |
|
|
|
|
|
|
|
_catalogContext.CatalogItems.Add(item); |
|
|
|
|
|
|
|
await _catalogContext.SaveChangesAsync(); |
|
|
|
|
|
|
|
return CreatedAtAction(nameof(GetItemById), new { id = item.Id }, null); |
|
|
|
return CreatedAtAction(nameof(ItemByIdAsync), new { id = item.Id }, null); |
|
|
|
} |
|
|
|
|
|
|
|
//DELETE api/v1/[controller]/id
|
|
|
|
[Route("{id}")] |
|
|
|
[HttpDelete] |
|
|
|
[ProducesResponseType((int)HttpStatusCode.NoContent)] |
|
|
|
public async Task<ActionResult> DeleteProduct(int id) |
|
|
|
[ProducesResponseType((int)HttpStatusCode.NotFound)] |
|
|
|
public async Task<ActionResult> DeleteProductAsync(int id) |
|
|
|
{ |
|
|
|
var product = _catalogContext.CatalogItems.SingleOrDefault(x => x.Id == id); |
|
|
|
|
|
|
|