|
|
@ -1,7 +1,9 @@ |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Catalog.API.IntegrationEvents; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.EntityFrameworkCore.Storage; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events.IntegrationEventLog; |
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events; |
|
|
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Model; |
|
|
@ -16,28 +18,31 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
[Route("api/v1/[controller]")]
|
|
|
|
public class CatalogController : ControllerBase |
|
|
|
{ |
|
|
|
private readonly CatalogContext _context; |
|
|
|
private readonly CatalogContext _catalogContext; |
|
|
|
private readonly IOptionsSnapshot<Settings> _settings; |
|
|
|
private readonly IEventBus _eventBus; |
|
|
|
private readonly IIntegrationEventLogService _integrationEventLogService; |
|
|
|
|
|
|
|
public CatalogController(CatalogContext context, IOptionsSnapshot<Settings> settings, IEventBus eventBus) |
|
|
|
public CatalogController(CatalogContext Context, IOptionsSnapshot<Settings> settings, IEventBus eventBus, IIntegrationEventLogService integrationEventLogService) |
|
|
|
{ |
|
|
|
_context = context; |
|
|
|
_catalogContext = Context; |
|
|
|
_settings = settings; |
|
|
|
_eventBus = eventBus; |
|
|
|
_integrationEventLogService = integrationEventLogService; |
|
|
|
|
|
|
|
((DbContext)context).ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; |
|
|
|
((DbContext)Context).ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; |
|
|
|
} |
|
|
|
|
|
|
|
// GET api/v1/[controller]/items[?pageSize=3&pageIndex=10]
|
|
|
|
[HttpGet] |
|
|
|
[Route("[action]")]
|
|
|
|
public async Task<IActionResult> Items([FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) |
|
|
|
|
|
|
|
{ |
|
|
|
var totalItems = await _context.CatalogItems |
|
|
|
var totalItems = await _catalogContext.CatalogItems |
|
|
|
.LongCountAsync(); |
|
|
|
|
|
|
|
var itemsOnPage = await _context.CatalogItems |
|
|
|
var itemsOnPage = await _catalogContext.CatalogItems |
|
|
|
.OrderBy(c=>c.Name) |
|
|
|
.Skip(pageSize * pageIndex) |
|
|
|
.Take(pageSize) |
|
|
@ -57,11 +62,11 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
public async Task<IActionResult> Items(string name, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) |
|
|
|
{ |
|
|
|
|
|
|
|
var totalItems = await _context.CatalogItems |
|
|
|
var totalItems = await _catalogContext.CatalogItems |
|
|
|
.Where(c => c.Name.StartsWith(name)) |
|
|
|
.LongCountAsync(); |
|
|
|
|
|
|
|
var itemsOnPage = await _context.CatalogItems |
|
|
|
var itemsOnPage = await _catalogContext.CatalogItems |
|
|
|
.Where(c => c.Name.StartsWith(name)) |
|
|
|
.Skip(pageSize * pageIndex) |
|
|
|
.Take(pageSize) |
|
|
@ -80,7 +85,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
[Route("[action]/type/{catalogTypeId}/brand/{catalogBrandId}")]
|
|
|
|
public async Task<IActionResult> Items(int? catalogTypeId, int? catalogBrandId, [FromQuery]int pageSize = 10, [FromQuery]int pageIndex = 0) |
|
|
|
{ |
|
|
|
var root = (IQueryable<CatalogItem>)_context.CatalogItems; |
|
|
|
var root = (IQueryable<CatalogItem>)_catalogContext.CatalogItems; |
|
|
|
|
|
|
|
if (catalogTypeId.HasValue) |
|
|
|
{ |
|
|
@ -113,7 +118,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
[Route("[action]")]
|
|
|
|
public async Task<IActionResult> CatalogTypes() |
|
|
|
{ |
|
|
|
var items = await _context.CatalogTypes |
|
|
|
var items = await _catalogContext.CatalogTypes |
|
|
|
.ToListAsync(); |
|
|
|
|
|
|
|
return Ok(items); |
|
|
@ -124,7 +129,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
[Route("[action]")]
|
|
|
|
public async Task<IActionResult> CatalogBrands() |
|
|
|
{ |
|
|
|
var items = await _context.CatalogBrands |
|
|
|
var items = await _catalogContext.CatalogBrands |
|
|
|
.ToListAsync(); |
|
|
|
|
|
|
|
return Ok(items); |
|
|
@ -135,7 +140,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
[HttpPost] |
|
|
|
public async Task<IActionResult> EditProduct([FromBody]CatalogItem product) |
|
|
|
{ |
|
|
|
var item = await _context.CatalogItems.SingleOrDefaultAsync(i => i.Id == product.Id); |
|
|
|
var item = await _catalogContext.CatalogItems.SingleOrDefaultAsync(i => i.Id == product.Id); |
|
|
|
|
|
|
|
if (item == null) |
|
|
|
{ |
|
|
@ -146,20 +151,21 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
{ |
|
|
|
var oldPrice = item.Price; |
|
|
|
item.Price = product.Price; |
|
|
|
_context.CatalogItems.Update(item); |
|
|
|
|
|
|
|
var @event = new ProductPriceChangedIntegrationEvent(item.Id, item.Price, oldPrice); |
|
|
|
var eventLogEntry = new IntegrationEventLogEntry(@event); |
|
|
|
_context.IntegrationEventLog.Add(eventLogEntry); |
|
|
|
|
|
|
|
await _context.SaveChangesAsync(); |
|
|
|
|
|
|
|
using (var transaction = _catalogContext.Database.BeginTransaction()) |
|
|
|
{ |
|
|
|
_catalogContext.CatalogItems.Update(item); |
|
|
|
await _catalogContext.SaveChangesAsync(); |
|
|
|
|
|
|
|
await _integrationEventLogService.SaveEventAsync(@event); |
|
|
|
|
|
|
|
transaction.Commit(); |
|
|
|
} |
|
|
|
|
|
|
|
_eventBus.Publish(@event); |
|
|
|
|
|
|
|
eventLogEntry.TimesSent++; |
|
|
|
eventLogEntry.State = EventStateEnum.Published; |
|
|
|
_context.IntegrationEventLog.Update(eventLogEntry); |
|
|
|
await _context.SaveChangesAsync(); |
|
|
|
|
|
|
|
await _integrationEventLogService.MarkEventAsPublishedAsync(@event); |
|
|
|
} |
|
|
|
|
|
|
|
return Ok(); |
|
|
@ -170,7 +176,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
[HttpPost] |
|
|
|
public async Task<IActionResult> CreateProduct([FromBody]CatalogItem product) |
|
|
|
{ |
|
|
|
_context.CatalogItems.Add( |
|
|
|
_catalogContext.CatalogItems.Add( |
|
|
|
new CatalogItem |
|
|
|
{ |
|
|
|
CatalogBrandId = product.CatalogBrandId, |
|
|
@ -181,7 +187,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
Price = product.Price |
|
|
|
}); |
|
|
|
|
|
|
|
await _context.SaveChangesAsync(); |
|
|
|
await _catalogContext.SaveChangesAsync(); |
|
|
|
|
|
|
|
return Ok(); |
|
|
|
} |
|
|
@ -191,15 +197,15 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers |
|
|
|
[HttpDelete] |
|
|
|
public async Task<IActionResult> DeleteProduct(int id) |
|
|
|
{ |
|
|
|
var product = _context.CatalogItems.SingleOrDefault(x => x.Id == id); |
|
|
|
var product = _catalogContext.CatalogItems.SingleOrDefault(x => x.Id == id); |
|
|
|
|
|
|
|
if (product == null) |
|
|
|
{ |
|
|
|
return NotFound(); |
|
|
|
} |
|
|
|
|
|
|
|
_context.CatalogItems.Remove(product); |
|
|
|
await _context.SaveChangesAsync(); |
|
|
|
_catalogContext.CatalogItems.Remove(product); |
|
|
|
await _catalogContext.SaveChangesAsync(); |
|
|
|
|
|
|
|
return Ok(); |
|
|
|
} |
|
|
|