Example #1
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new CodingTestDbContext(
                       serviceProvider.GetRequiredService <DbContextOptions <CodingTestDbContext> >()))
            {
                // Look for any Categories.
                if (context.Categories.Any())
                {
                    return;
                }

                context.Categories.AddRange(
                    new Category
                {
                    CategoryId   = 1,
                    CategoryName = "Government"
                },
                    new Category
                {
                    CategoryId   = 2,
                    CategoryName = "Corporate Customer"
                },
                    new Category
                {
                    CategoryId   = 3,
                    CategoryName = "Individual"
                });

                context.SaveChanges();
            }
        }
        public CustomerViewModel UpdateCustomer(CustomerViewModel model)
        {
            // Ensure the correct category is set
            model.Customer.Category = _dbContext.Categories.Find(model.Customer.Category?.CategoryId);

            try
            {
                // Either Update or Insert product
                if (model.PageMode == Constants.EDIT)
                {
                    _dbContext.Entry(model.Customer).State = EntityState.Modified;
                    _dbContext.SaveChanges();
                }
                else if (model.PageMode == Constants.ADD)
                {
                    _dbContext.Customers.Add(model.Customer);
                    _dbContext.SaveChanges();
                }

                // Get all the data again in case anything changed
                model.Customers        = _dbContext.Customers.OrderBy(p => p.CategoryId).ToList();
                model.SearchCategories = _dbContext.Categories.OrderBy(p => p.CategoryName).ToList();

                // Add category for 'Search All'
                Category category = new Category();
                category.CategoryId   = 0;
                category.CategoryName = "-- Search All Categories --";
                model.SearchCategories.Insert(0, category);
                model.SearchEntity = new CustomerSearch();
                model.IsValid      = true;
                SetUIState(model, Constants.LIST);
            }
            catch (ValidationException ex)
            {
                model.IsValid = false;
                // TODO: Validation errors

                // Set page state
                SetUIState(model, model.PageMode);
            }
            return(model);
        }