Ejemplo n.º 1
0
        public async Task <InventoryServiceModel> UpdateAsync(UpdateInventoryServiceModel serviceModel)
        {
            var inventoryProduct = await this.context.Inventory.FirstOrDefaultAsync(x => x.Id == serviceModel.Id && x.SellerId == serviceModel.OrganisationId.Value && x.IsActive);

            if (inventoryProduct == null)
            {
                throw new CustomException(this.inventortLocalizer.GetString("InventoryNotFound"), (int)HttpStatusCode.NotFound);
            }

            inventoryProduct.WarehouseId       = serviceModel.WarehouseId.Value;
            inventoryProduct.ProductId         = serviceModel.ProductId.Value;
            inventoryProduct.ProductName       = serviceModel.ProductName;
            inventoryProduct.ProductSku        = serviceModel.ProductSku;
            inventoryProduct.Quantity          = serviceModel.Quantity;
            inventoryProduct.RestockableInDays = serviceModel.RestockableInDays;
            inventoryProduct.AvailableQuantity = serviceModel.AvailableQuantity;
            inventoryProduct.ExpectedDelivery  = serviceModel.ExpectedDelivery;
            inventoryProduct.LastModifiedDate  = DateTime.UtcNow;

            await this.context.SaveChangesAsync();

            return(await this.GetAsync(new GetInventoryServiceModel { Id = inventoryProduct.Id, Language = serviceModel.Language, OrganisationId = serviceModel.OrganisationId, Username = serviceModel.Username }));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Save(InventoryRequestModel request)
        {
            var sellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim);

            if (request.Id.HasValue && request.Id != null)
            {
                var serviceModel = new UpdateInventoryServiceModel
                {
                    Id                = request.Id.Value,
                    WarehouseId       = request.WarehouseId.Value,
                    ProductId         = request.ProductId.Value,
                    ProductName       = request.ProductName,
                    ProductSku        = request.ProductSku,
                    Quantity          = request.Quantity,
                    AvailableQuantity = request.AvailableQuantity,
                    RestockableInDays = request.RestockableInDays,
                    ExpectedDelivery  = request.ExpectedDelivery,
                    Language          = CultureInfo.CurrentCulture.Name,
                    OrganisationId    = GuidHelper.ParseNullable(sellerClaim?.Value)
                };

                var validator        = new UpdateInventoryModelValidator();
                var validationResult = await validator.ValidateAsync(serviceModel);

                if (validationResult.IsValid)
                {
                    var inventoryProduct = await this.inventoriesService.UpdateAsync(serviceModel);

                    return(this.StatusCode((int)HttpStatusCode.OK, new { inventoryProduct.Id }));
                }
                throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
            }
            else
            {
                var serviceModel = new CreateInventoryServiceModel
                {
                    WarehouseId       = request.WarehouseId.Value,
                    ProductId         = request.ProductId.Value,
                    ProductName       = request.ProductName,
                    ProductSku        = request.ProductSku,
                    Quantity          = request.Quantity,
                    AvailableQuantity = request.AvailableQuantity,
                    RestockableInDays = request.RestockableInDays,
                    ExpectedDelivery  = request.ExpectedDelivery,
                    Language          = CultureInfo.CurrentCulture.Name,
                    OrganisationId    = GuidHelper.ParseNullable(sellerClaim?.Value)
                };

                var validator        = new CreateInventoryModelValidator();
                var validationResult = await validator.ValidateAsync(serviceModel);

                if (validationResult.IsValid)
                {
                    var inventoryProduct = await this.inventoriesService.CreateAsync(serviceModel);

                    return(this.StatusCode((int)HttpStatusCode.Created, new { inventoryProduct.Id }));
                }

                throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
            }
        }