public IHttpActionResult Post(FileDto file)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            fileRepo.Insert(Mapper.Map <File>(file));

            return(CreatedAtRoute("AddFile", new { id = file.FileId }, file));
        }
Пример #2
0
        public bool Add(ProductDetailDto dto)
        {
            using (var cxt = DbContext(DbOperation.Write))
            {
                if (dto.Id == 0)
                {
                    cxt.BeginTransaction();
                    var repo    = new ProductRepo(cxt);
                    var product = new Product
                    {
                        Code          = dto.Code,
                        Name          = dto.Name,
                        CoverUrl      = dto.CoverUrl,
                        ClassId       = dto.ClassId,
                        Summary       = dto.Summary,
                        Description   = dto.Description,
                        OriginalPrice = dto.OriginalPrice,
                        Price         = dto.Price,
                        StoreAmount   = dto.StoreAmount,
                        UpdateTime    = DateTime.Now,
                        CreateTime    = DateTime.Now,
                        IsDeleted     = 0,
                        Status        = 1
                                        //SaleAll = dto.SaleAll
                                        //CommentAll = dto.CommentAll
                    };
                    long id = repo.Insert(product);

                    if (id > 0)
                    {
                        var fileRepo = new FileRepo(cxt);
                        var fileList = new List <File>();

                        if (!string.IsNullOrEmpty(dto.DetailImages.FirstOrDefault()))
                        {
                            var detailImg = new File
                            {
                                ProductId  = (int)id,
                                Url        = dto.DetailImages.FirstOrDefault(),
                                Type       = (int)Common.Enums.FileType.Detail,
                                UpdateTime = DateTime.Now,
                                CreateTime = DateTime.Now,
                                IsDeleted  = 0,
                                Status     = 1
                            };
                            fileList.Add(detailImg);
                        }

                        if (dto.CoverImages.Any())
                        {
                            foreach (var img in dto.CoverImages)
                            {
                                var coverImg = new File
                                {
                                    ProductId  = (int)id,
                                    Url        = img,
                                    Type       = (int)Enums.FileType.DetailBanner,
                                    UpdateTime = DateTime.Now,
                                    CreateTime = DateTime.Now,
                                    IsDeleted  = 0,
                                    Status     = 1
                                };
                                fileList.Add(coverImg);
                            }
                        }

                        if (fileList.Any())
                        {
                            fileRepo.Insert(fileList);
                        }

                        var skuRepo = new ProductSkuRepo(cxt);
                        var skuList = new List <ProductSku>();
                        foreach (var featureValueId in dto.AttributeIdSet)
                        {
                            var sku = new ProductSku
                            {
                                ProductId      = (int)id,
                                FeatureValueId = featureValueId,
                                Num            = dto.StoreAmount,
                                Price          = dto.Price,
                                UpdateTime     = DateTime.Now,
                                CreateTime     = DateTime.Now,
                                IsDeleted      = 0,
                                Status         = 1
                            };
                            skuList.Add(sku);
                        }
                        skuRepo.Insert(skuList);
                        cxt.Commit();
                        return(true);
                    }
                    else
                    {
                        cxt.Rollback();
                        return(false);
                    }
                }

                return(false);
            }
        }
Пример #3
0
        public bool UpdateDetail(ProductDetailDto dto)
        {
            using (var cxt = DbContext(DbOperation.Write))
            {
                if (dto.Id > 0)
                {
                    cxt.BeginTransaction();
                    var repo    = new ProductRepo(cxt);
                    var product = new Product
                    {
                        Id            = dto.Id,
                        Code          = dto.Code,
                        Name          = dto.Name,
                        CoverUrl      = dto.CoverUrl,
                        ClassId       = dto.ClassId,
                        Summary       = dto.Summary,
                        Description   = dto.Description,
                        OriginalPrice = dto.OriginalPrice,
                        Price         = dto.Price,
                        StoreAmount   = dto.StoreAmount,
                        UpdateTime    = DateTime.Now,
                    };

                    //var propertyNames = product.GetType().GetProperties().Select(p => p.Name).ToList();
                    var propertyNames = "Code,Name,CoverUrl,ClassId,Summary,Description,OriginalPrice,Price,StoreAmount,UpdateTime".Split(',');
                    if (repo.Update(product, p => propertyNames.Contains(p.Name)))
                    {
                        var fileRepo = new FileRepo(cxt);
                        fileRepo.DeleteFilesOfProduct(dto.Id);
                        var fileList = new List <File>();
                        if (!string.IsNullOrEmpty(dto.DetailImages.FirstOrDefault()))
                        {
                            var detailImg = new File
                            {
                                ProductId  = dto.Id,
                                Url        = dto.DetailImages.FirstOrDefault(),
                                Type       = (int)Common.Enums.FileType.Detail,
                                UpdateTime = DateTime.Now,
                                CreateTime = DateTime.Now,
                                IsDeleted  = 0,
                                Status     = 1
                            };
                            fileList.Add(detailImg);
                        }

                        if (dto.CoverImages.Any())
                        {
                            foreach (var img in dto.CoverImages)
                            {
                                var coverImg = new File
                                {
                                    ProductId  = dto.Id,
                                    Url        = img,
                                    Type       = (int)Enums.FileType.DetailBanner,
                                    UpdateTime = DateTime.Now,
                                    CreateTime = DateTime.Now,
                                    IsDeleted  = 0,
                                    Status     = 1
                                };
                                fileList.Add(coverImg);
                            }
                        }

                        if (fileList.Any())
                        {
                            fileRepo.Insert(fileList);
                        }

                        var skuRepo = new ProductSkuRepo(cxt);
                        skuRepo.DeleteProductSku(dto.Id);
                        var skuList = new List <ProductSku>();
                        foreach (var featureValueId in dto.AttributeIdSet)
                        {
                            var sku = new ProductSku
                            {
                                ProductId      = dto.Id,
                                FeatureValueId = featureValueId,
                                Num            = dto.StoreAmount,
                                Price          = dto.Price,
                                UpdateTime     = DateTime.Now,
                                CreateTime     = DateTime.Now,
                                IsDeleted      = 0,
                                Status         = 1
                            };
                            skuList.Add(sku);
                        }
                        skuRepo.Insert(skuList);
                        cxt.Commit();

                        return(true);
                    }
                    else
                    {
                        cxt.Rollback();
                        return(false);
                    }
                }

                return(false);
            }
        }