Example #1
0
        public async Task <IActionResult> Create(DocumentTemplateViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                await BindControlsAsync(model);

                Title     = DocumentResources.DocumentTemplate;
                ReturnUrl = returnUrl;
                return(View(model));
            }

            var template = new DocumentTemplate
            {
                Name      = model.Name,
                CreatorId = UserId,
                DocumentTemplateProducts = model.Products.Where(p => p.Checked).Select(m => new DocumentTemplateProduct
                {
                    ProductId = m.Id
                }).ToList()
            };

            Uow.DocumentTemplates.Add(template);
            await Uow.SaveAsync();

            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #2
0
        private async Task BindControlsAsync(DocumentTemplateViewModel model)
        {
            if (model == null)
            {
                model = new DocumentTemplateViewModel();
            }

            model.Categories = _cache.GetCategory(1, 1);

            if (model.Products == null)
            {
                model.Products = new List <DocumentTemplateProductViewModel>();
            }


            var products = await Uow.Products
                           .Where(p => !p.IsDeleted)
                           .Select(p => new DocumentTemplateProductViewModel
            {
                Id            = p.Id,
                ProductNumber = p.ProductNumber,
                Name          = p.Name,
                CategoryId    = p.CategoryId,
            }).ToDictionaryAsync(p => p.Id, p => p);

            if (model.Products.IsNullOrEmpty())
            {
                model.Products = products.Select(p => p.Value).ToList();
            }
            else
            {
                foreach (var product in model.Products)
                {
                    var tmp = products.TryGetValue(product.Id);
                    if (tmp == null)
                    {
                        continue;
                    }

                    product.ProductNumber = tmp.ProductNumber;
                    product.Name          = tmp.Name;
                    product.CategoryId    = tmp.CategoryId;

                    products.Remove(tmp.Id);
                }
                model.Products.AddRange(products.Values);
            }
        }
Example #3
0
        public async Task <IActionResult> Edit(DocumentTemplateViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                await BindControlsAsync(model);

                Title     = DocumentResources.DocumentTemplate;
                ReturnUrl = returnUrl;
                return(View(model));
            }


            var template = await Uow.DocumentTemplates.Where(t => t.Id == model.Id && !t.IsDeleted).SingleOrDefaultAsync();

            if (template == null)
            {
                return(NotFound());
            }

            template.Name         = model.Name;
            template.ModifierId   = UserId;
            template.ModifiedDate = DateTime.Now;


            var dbProducts = await Uow.DocumentTemplateProducts.Where(p => p.DocumentTemplateId == model.Id).ToListAsync();

            var dbIds               = new HashSet <int>(dbProducts.Select(t => t.ProductId));
            var checkedProducts     = model.Products.Where(t => t.Checked).ToList();
            var checkedIds          = new HashSet <int>(checkedProducts.Select(t => t.Id));
            var toBeDeletedProducts = dbProducts.Where(t => !checkedIds.Contains(t.ProductId)).ToList();

            Uow.DocumentTemplateProducts.RemoveRange(toBeDeletedProducts);

            Uow.DocumentTemplateProducts.AddRange(checkedProducts.Where(t => !dbIds.Contains(t.Id))
                                                  .Select(t => new DocumentTemplateProduct
            {
                DocumentTemplateId = template.Id,
                ProductId          = t.Id
            }));


            await Uow.SaveAsync();


            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #4
0
        public async Task <IActionResult> Create(string returnUrl = null)
        {
            var model = new DocumentTemplateViewModel
            {
                Categories = _cache.GetCategory(1, 1),
                Products   = await Uow.Products
                             .Where(p => !p.IsDeleted)
                             //.OrderBy(p => p.CategoryId)
                             //.ThenBy(p => p.Name)
                             .Select(p => new DocumentTemplateProductViewModel
                {
                    Id            = p.Id,
                    ProductNumber = p.ProductNumber,
                    Name          = p.Name,
                    CategoryId    = p.CategoryId,
                }).ToListAsync()
            };

            await BindControlsAsync(model);

            Title     = DocumentResources.DocumentTemplate;
            ReturnUrl = returnUrl;
            return(View(model));
        }