Exemple #1
0
        public async Task <Package> UpdatePackageAsync(PackageServiceModel serviceModel)
        {
            var package = await GetPackages().SingleAsync(o => o.Id == serviceModel.Id);

            var newPackage = serviceModel.ToEntity();

            _context.Entry(package).CurrentValues.SetValues(newPackage);
            _context.Entry(package).Property(o => o.CreateByUserId).IsModified = false;
            _context.Entry(package).Property(o => o.CreateDate).IsModified     = false;

            foreach (var newDetail in newPackage.PackageDetails)
            {
                var oldDetail = package.PackageDetails.FirstOrDefault(o => o.Language == newDetail.Language);
                if (oldDetail.Language == newDetail.Language)
                {
                    newDetail.Id = oldDetail.Id;
                    _context.Entry(oldDetail).CurrentValues.SetValues(newDetail);
                }
            }

            _context.TryUpdateManyToMany(package.EnitityFiles, newPackage.EnitityFiles, o => o.FileEntityId);
            _context.TryUpdateManyToMany(package.EntityTaxonomies, newPackage.EntityTaxonomies, o => o.TaxonomyId);

            await _context.SaveChangesAsync();

            return(newPackage);
        }
        public async Task <Package> UpdatePackageAsync(PackageServiceModel serviceModel)
        {
            var package = await GetPackages().SingleAsync(o => o.Id == serviceModel.Id);

            var newPackage = serviceModel.ToEntity();

            _context.Entry(package).CurrentValues.SetValues(newPackage);
            _context.Entry(package).Property(o => o.Name).IsModified           = false;
            _context.Entry(package).Property(o => o.CreateByUserId).IsModified = false;
            _context.Entry(package).Property(o => o.CreateDate).IsModified     = false;

            foreach (var newDetail in newPackage.Details)
            {
                var oldDetail = package.Details.FirstOrDefault(o => o.Language == newDetail.Language);
                if (oldDetail.Language == newDetail.Language)
                {
                    newDetail.Id = oldDetail.Id;
                    _context.Entry(oldDetail).CurrentValues.SetValues(newDetail);
                }
            }

            _context.TryUpdateManyToMany(package.EntityFiles, newPackage.EntityFiles, o => o.FileEntityId);
            _context.TryUpdateManyToMany(package.EntityTaxonomies, newPackage.EntityTaxonomies, o => o.TaxonomyId);

            _context.TryUpdateManyToMany(package.EntityProducts, newPackage.EntityProducts, o => o.ProductId);
            foreach (var entityProduct in package.EntityProducts)
            {
                var pdEntry = _context.Entry(entityProduct);
                if (pdEntry.State == EntityState.Deleted)
                {
                    continue;
                }

                pdEntry.Property(o => o.Quantity).CurrentValue = newPackage.EntityProducts.FirstOrDefault(o => o.ProductId == entityProduct.ProductId).Quantity;
            }

            await _context.SaveChangesAsync();

            return(newPackage);
        }