Пример #1
0
        public IActionResult Create([FromBody] StorageExport request)
        {
            var actionResult = new CustomActionResult
            {
                Successful = true,
                Message    = "Storage Export was successfull created!"
            };

            try
            {
                request.SetAudit(CurrentLoggedUserId);
                var storageExport = _storageExportService.Create(request);
                actionResult.EntityId = storageExport.Id;
            }
            catch
            {
                actionResult.Successful = false;
                actionResult.Message    = "Create storage export was unsuccessfully, please try again!";

                return(Ok(actionResult));
            }

            try
            {
                var product = _productService.GetById(request.ProductId);
                product.StorageQuantity -= request.Quantity;
                _productService.Update(product);
            }
            catch
            {
                actionResult.Successful = false;
                actionResult.Message    = "Create storage export was successfully, but Storage Quantity of the product was not updated properly, please contact Office worker!";

                return(Ok(actionResult));
            }

            var fieldWorkerProducts = _fieldWorkerProductService.GetByCriteria(new FieldWorkerProductSearchRequest
            {
                FieldWorkerId = request.FieldWorkerId,
                ProductId     = request.ProductId
            });

            try
            {
                if (fieldWorkerProducts.Count() == 0)
                {
                    var fieldWorkerProduct = new FieldWorkerProduct
                    {
                        FieldWorkerId = request.FieldWorkerId,
                        ProductId     = request.ProductId,
                        Quantity      = request.Quantity
                    };
                    fieldWorkerProduct.SetAudit(CurrentLoggedUserId);

                    _fieldWorkerProductService.Create(fieldWorkerProduct);
                }
                else
                {
                    var fieldWorkerProduct = fieldWorkerProducts.First();
                    fieldWorkerProduct.Quantity += request.Quantity;
                    fieldWorkerProduct.SetAudit(CurrentLoggedUserId);

                    _fieldWorkerProductService.Update(fieldWorkerProduct);
                }
            }
            catch
            {
                actionResult.Successful = false;
                actionResult.Message    = "Create storage export was successfully, but Field Worker Quantity of the product was not updated properly, please contact Office worker!";

                return(Ok(actionResult));
            }

            return(Ok(actionResult));
        }
Пример #2
0
        public IActionResult GetByCriteria([FromQuery] FieldWorkerProductSearchRequest request)
        {
            var fieldWorkerProduct = _fieldWorkerProductService.GetByCriteria(request);

            return(Ok(fieldWorkerProduct));
        }
        public IActionResult Create([FromBody] VendingMachineProduct request)
        {
            var actionResult = new CustomActionResult
            {
                Successful = true,
                Message    = "Product was successfull added for Vending Machine!"
            };
            var userId = CurrentLoggedUserId;

            try
            {
                request.SetAudit(userId);
                var vendingMachineProduct = _vendingMachineProductService.Create(request);
            }
            catch
            {
                actionResult.Successful = false;
                actionResult.Message    = "Product was unsuccessfull added for Vending Machine, please try again!";

                return(Ok(actionResult));
            }

            if (CurrentLoggedUserRole == Role.FieldWorker)
            {
                var product             = _productService.GetById(request.ProductId);
                var fieldWorkerProducts = _fieldWorkerProductService.GetByCriteria(new FieldWorkerProductSearchRequest
                {
                    FieldWorkerId = CurrentLoggedUserId,
                    ProductId     = request.ProductId
                });
                var fieldWorkerProduct = fieldWorkerProducts.FirstOrDefault();
                try
                {
                    product.Quantity -= request.Quantity;
                    product.SetAudit(userId);
                    _productService.Update(product);
                }
                catch
                {
                    actionResult.Successful = false;
                    actionResult.Message    = "Product was successfully added for Vending Machine, but Quantity for product was not updated, contact your superior!";

                    return(Ok(actionResult));
                }

                if (fieldWorkerProduct == null)
                {
                    actionResult.Successful = false;
                    actionResult.Message    = "Product was successfully added for Vending Machine, but field worker should not possess these product, contact your superior!";

                    return(Ok(actionResult));
                }
                else
                {
                    try
                    {
                        fieldWorkerProduct.Quantity -= request.Quantity;
                        fieldWorkerProduct.SetAudit(userId);
                        _fieldWorkerProductService.Update(fieldWorkerProduct);
                    }
                    catch
                    {
                        actionResult.Successful = false;
                        actionResult.Message    = "Product was successfully added for Vending Machine, but Quantity for field worker product was not updated, contact your superior!";

                        return(Ok(actionResult));
                    }
                }
            }

            return(Ok(actionResult));
        }