public static StockSnapshotResponse ToStockServiceResponse(this StockSnapshotModel stockSnapshotModel) { StockSnapshotResponse stockSnapshotResponse = null; if (stockSnapshotModel != null) { stockSnapshotResponse = new StockSnapshotResponse(stockSnapshotModel.Id, stockSnapshotModel.ProductId, stockSnapshotModel.AvailableStock, stockSnapshotModel.LastStockActionDate); } return(stockSnapshotResponse); }
public async Task Handle(StockInitializedEvent notification, CancellationToken cancellationToken) { var stockSnapshotModel = new StockSnapshotModel(notification.ProductId, 0, notification.StockActionId, notification.StockActionDate); await _dataContext.StockSnapshotModels.AddAsync(stockSnapshotModel, cancellationToken); await _dataContext.SaveChangesAsync(cancellationToken); var stockSnapshotCreatedIntegrationEvent = new StockInitializedIntegrationEvent(stockSnapshotModel.ProductId, stockSnapshotModel.StockActionId, stockSnapshotModel.LastStockActionDate); _integrationEventPublisher.AddEvent(stockSnapshotCreatedIntegrationEvent); }
private async Task IncreaseStock(long productId, int count, long stockActionId, DateTime stockActionDate, CancellationToken cancellationToken) { StockSnapshotModel stockSnapshotModel = await _dataContext.StockSnapshotModels .FirstOrDefaultAsync(s => s.ProductId == productId, cancellationToken); if (stockSnapshotModel == null) { throw new StockSnapshotNotFoundException(productId); } stockSnapshotModel.IncreaseStock(count, stockActionId, stockActionDate); await _dataContext.SaveChangesAsync(cancellationToken); var stockCountIncreasedIntegrationEvent = new StockCountIncreasedIntegrationEvent(stockSnapshotModel.ProductId, stockSnapshotModel.StockActionId, count, stockSnapshotModel.AvailableStock, stockActionDate); _integrationEventPublisher.AddEvent(stockCountIncreasedIntegrationEvent); }