Ejemplo n.º 1
0
        public IActionResult InsertPackage([FromForm] InsertPackageDto input, [FromForm] FormFileCollection fileList)
        {
            try
            {
                var validator = new ParamValidator();
                validator.ValidateNull(input.Name, General.Messages_.NullInputMessages_.GeneralNullMessage("عنوان"))
                .ValidateNull(input.Price, General.Messages_.NullInputMessages_.GeneralNullMessage("قیمت"))
                .Throw(General.Results_.FieldNullErrorCode());

                if (input.ProductWithPriceList.Count == 0)
                {
                    throw new BusinessException("محصولی برای پکیج انتخاب نشده است.", 4001);
                }


                var counter = (_repository.Product
                               .FindByCondition(c => c.Coding.ToString().Substring(0, 8) == "11223344")
                               .Count() + 1).ToString();
                counter = counter.PadLeft(4, '0');

                var newproduct = new Product
                {
                    Name            = input.Name,
                    Price           = input.Price,
                    MetaDescription = input.MetaDesc,
                    KeyWords        = input.KeyWord,
                    MetaTitle       = input.MetaTitle,
                    Coding          = long.Parse("11223344" + counter),
                    IsPackage       = true,
                    Cdate           = DateTime.Now.Ticks,
                    CuserId         = ClaimPrincipalFactory.GetUserId(User)
                };

                input.ProductWithPriceList.ForEach(c =>
                {
                    var newProductPackage = new ProductPackage()
                    {
                        Cdate        = DateTime.Now.Ticks,
                        CuserId      = ClaimPrincipalFactory.GetUserId(User),
                        DepProductId = c.ProductId,
                        Price        = c.Price
                    };

                    newproduct.ProductPackageDepProduct.Add(newProductPackage);
                });

                fileList.ForEach(c =>
                {
                    short fileType = 1;
                    if (FileManeger.IsVideo(c))
                    {
                        fileType = 2;
                    }
                    var uploadFileStatus = FileManeger.FileUploader(c, fileType, "ProductPackage");
                    if (uploadFileStatus.Status == 200)
                    {
                        var newProductImage = new ProductImage
                        {
                            Cdate    = DateTime.Now.Ticks,
                            CuserId  = ClaimPrincipalFactory.GetUserId(User),
                            ImageUrl = uploadFileStatus.Path,
                            FileType = fileType
                        };
                        newproduct.ProductImage.Add(newProductImage);
                    }
                    else
                    {
                        throw new BusinessException(uploadFileStatus.Path, 4009);
                    }
                });

                _repository.Product.Create(newproduct);
                _repository.Save();
                _logger.LogData(MethodBase.GetCurrentMethod(), newproduct.Id, null, input, "****");
                return(Ok(newproduct.Id));
            }
            catch (Exception e)
            {
                _logger.LogError(e, MethodBase.GetCurrentMethod(), input, "****");
                return(BadRequest(e.Message));
            }
        }