Example #1
0
        public async Task <IActionResult> AddToStock([FromRoute] long productId, [FromBody] AddToStockHttpRequest addToStockHttpRequest)
        {
            var queryProductServiceRequest = new QueryProductCommand(0, 1)
            {
                ProductId = productId
            };

            StockActionHttpResponse stockActionHttpResponse = null;
            await _distributedLockManager.LockAsync(StockOperationLockKey(productId),
                                                    async() =>
            {
                await _mediator.Send(queryProductServiceRequest);

                AddToStockCommand addToStockCommand = addToStockHttpRequest != null
                                                                                                  ? new AddToStockCommand(productId, addToStockHttpRequest.Count, addToStockHttpRequest.CorrelationId)
                                                                                                  : null;

                StockActionResponse stockActionResponse = await _mediator.Send(addToStockCommand);

                stockActionHttpResponse = stockActionResponse.ToStockActionHttpResponse();
            });


            return(StatusCode((int)HttpStatusCode.OK, stockActionHttpResponse));
        }
        public async Task Consume(ConsumeContext <StockInitializedIntegrationEvent> context)
        {
            StockInitializedIntegrationEvent stockInitializedIntegrationEvent = context.Message;

            var queryProductCommand = new QueryProductCommand(0, 1)
            {
                ProductId = stockInitializedIntegrationEvent.ProductId
            };
            ProductCollectionResponse productCollectionResponse = await _mediator.Send(queryProductCommand);

            ProductResponse productResponse = productCollectionResponse.Data.First();

            var createStockCommand = new CreateStockCommand(productResponse.ProductId, productResponse.ProductCode, stockInitializedIntegrationEvent.StockActionId, stockInitializedIntegrationEvent.StockCreatedOn);
            await _mediator.Send(createStockCommand);
        }
        public async Task <IActionResult> GetProducts([FromQuery] GetProductCollectionHttpRequest getProductCollectionHttpRequest)
        {
            QueryProductCommand queryProductCommand
                = getProductCollectionHttpRequest != null
                      ? new QueryProductCommand(getProductCollectionHttpRequest.Offset, getProductCollectionHttpRequest.Take)
                {
                ProductCode = getProductCollectionHttpRequest.ProductCode
                }
                      : null;

            ProductCollectionResponse productCollectionResponse = await _mediator.Send(queryProductCommand);

            var productCollectionHttpResponse = new ProductCollectionHttpResponse
            {
                TotalCount = productCollectionResponse.TotalCount,
                Data       = productCollectionResponse.Data
                             .Select(d => d.ToProductHttpResponse())
                             .ToList()
            };

            return(StatusCode((int)HttpStatusCode.OK, productCollectionHttpResponse));
        }
Example #4
0
        public async Task <ProductCollectionResponse> Handle(QueryProductCommand request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new RequestNullException();
            }

            IQueryable <ProductModel> productModels = _dataContext.ProductModels.AsQueryable();

            if (request.ProductCode != null)
            {
                productModels = productModels.Where(p => p.ProductCode == request.ProductCode);
            }

            if (request.ProductId.HasValue)
            {
                productModels = productModels.Where(p => p.Id == request.ProductId);
            }

            int totalCount = await productModels.CountAsync(cancellationToken : cancellationToken);

            List <ProductModel> productModelList = await productModels.Skip(request.Offset)
                                                   .Take(request.Take)
                                                   .ToListAsync(cancellationToken: cancellationToken);

            if (!productModelList.Any())
            {
                throw new ProductNotFoundException();
            }

            List <ProductResponse> productServiceResponseList = productModelList.Select(p => p.ToProductServiceResponse())
                                                                .ToList();
            var productCollectionServiceResponse = new ProductCollectionResponse(totalCount, productServiceResponseList);

            return(productCollectionServiceResponse);
        }