Esempio n. 1
0
        public async Task <IResponse <Product> > AddAsync(ProductAddModel model)
        {
            var product = new Product().CopyFrom(model);

            if (model.TagIds != null && model.TagIds.Any())
            {
                product.ProductTags = new List <ProductTag>(model.TagIds.Select(x => new ProductTag {
                    TagId = x
                }));
            }
            if (model.Files != null && model.Files.Count != 0)
            {
                var getAssets = await _productAssetService.SaveRange(model);

                if (!getAssets.IsSuccessful)
                {
                    return new Response <Product> {
                               Message = getAssets.Message
                    }
                }
                ;
                product.ProductAssets = getAssets.Result;
                await _productRepo.AddAsync(product);

                var add = await _appUow.ElkSaveChangesAsync();

                if (!add.IsSuccessful)
                {
                    _productAssetService.DeleteRange(getAssets.Result);
                }
                return(new Response <Product> {
                    Result = product, IsSuccessful = add.IsSuccessful, Message = add.Message
                });
            }
            else
            {
                await _productRepo.AddAsync(product);

                var add = await _appUow.ElkSaveChangesAsync();

                return(new Response <Product> {
                    Result = product, IsSuccessful = add.IsSuccessful, Message = add.Message
                });
            }
        }