Пример #1
0
 public ActionResult Contacts(CrudCompanyVm companyContact)
 {
     UrlsContact();
     companyContact          = companyContact ?? new CrudCompanyVm();
     companyContact.Contacts = companyContact.Contacts ?? new List <Contact>();
     return(PartialView("_Contacts", companyContact.Contacts));
 }
Пример #2
0
        private void SetBiewBags(CrudCompanyVm model)
        {
            var cities = _db.Cities.OrderBy(o => o.Name);

            ViewBag.Cities = new SelectList(cities, "CityId", "Name", model?.Company?.Address?.CityId);

            var categories = _db.SubCategories
                             .Where(s => s.CategoryId == null)
                             .OrderBy(o => o.Name)
                             .AsNoTracking()
                             .ToList();

            ViewBag.Categories = new SelectList(categories, "SubCategoryId", "Name", model?.CategoryId);

            if (model == null || model.CategoryId == 0)
            {
                ViewBag.SubCategories = new SelectList(Enumerable.Empty <SelectListItem>());
            }
            else
            {
                var subCategories = _db.SubCategories
                                    .Where(s => model.CategoryId == s.CategoryId)
                                    .OrderBy(o => o.Name)
                                    .AsNoTracking()
                                    .ToList();
                ViewBag.SubCategories = new SelectList(subCategories, "SubCategoryId", "Name", model.SubCategoryId);
            }
        }
Пример #3
0
 public ActionResult AddContact(CrudCompanyVm companyContact)
 {
     companyContact.Contacts = companyContact.Contacts ?? new List <Contact>();
     companyContact.Contacts.Add(new Contact {
         Description = companyContact.Description, ContactType = companyContact.ContactType
     });
     companyContact.Description = string.Empty;
     companyContact.ContactType = 0;
     UrlsContact();
     return(PartialView("_Contacts", companyContact.Contacts));
 }
Пример #4
0
        private void SetBiewBags(CrudCompanyVm model)
        {
            ViewBag.Cities = new SelectList(EventController.GetCities(_db), "CityId", "Name", model?.Company?.Address?.CityId);

            var categories = _db.SubCategories.Where(s => s.CategoryId == null).OrderBy(o => o.Name);

            ViewBag.Categories = new SelectList(categories, "SubCategoryId", "Name", model?.CategoryId);

            if (model == null || model.CategoryId == 0)
            {
                ViewBag.SubCategories = new SelectList(Enumerable.Empty <SelectListItem>());
            }
            else
            {
                var subCategories = _db.SubCategories.Where(s => s.CategoryId != null).OrderBy(o => o.Name);
                ViewBag.SubCategories = new SelectList(subCategories, "SubCategoryId", "Name", model.SubCategoryId);
            }
        }
Пример #5
0
        public ActionResult Create(CrudCompanyVm model, Address address)
        {
            try
            {
                if (model.SubCategoryId.HasValue && model.SubCategoryId != 0)
                {
                    model.Company.SubCategoryId = model.SubCategoryId.Value;
                }
                else
                {
                    model.Company.SubCategoryId = model.CategoryId;
                }

                address.SetCoordinates(address.LatitudeString, address.LongitudeString);
                model.Company.Address  = address;
                model.Company.Contacts = model.Contacts;

                if (model.Company.Logo == null || model.LogoFile != null)
                {
                    model.Company.Logo = FileUpload.GetBytes(model.LogoFile, "Logo");
                }
                if (model.Company.Cover == null || model.CoverFile != null)
                {
                    model.Company.Cover = FileUpload.GetBytes(model.CoverFile, "Capa");
                }

                ModelState.Remove("Company.Logo");
                ModelState.Remove("Company.Cover");
                if (!ModelState.IsValid)
                {
                    SetBiewBags(model);
                    return(View(model).Error(ModelState));
                }

                _db.Companies.Add(model.Company);
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                SetBiewBags(model);
                return(View(model).Error(ex.Message));
            }
        }
Пример #6
0
        private static CrudCompanyVm GetCrudVm(Company company)
        {
            var model = new CrudCompanyVm {
                Company = company
            };

            if (company.Contacts != null)
            {
                model.Contacts = new List <Contact>(company.Contacts);
            }
            if (company.SubCategory?.CategoryId != null)
            {
                model.SubCategoryId = company.SubCategoryId;
                model.CategoryId    = company.SubCategory.CategoryId.Value;
            }
            else
            {
                model.CategoryId = company.SubCategoryId;
            }

            return(model);
        }
Пример #7
0
 public ActionResult RemoveContact(CrudCompanyVm companyContact, int index)
 {
     UrlsContact();
     companyContact.Contacts?.RemoveAt(index);
     return(PartialView("_Contacts", companyContact.Contacts));
 }
Пример #8
0
        public ActionResult Edit(CrudCompanyVm model, Address address)
        {
            try
            {
                if (model.SubCategoryId.HasValue && model.SubCategoryId != 0)
                {
                    model.Company.SubCategoryId = model.SubCategoryId.Value;
                }
                else
                {
                    model.Company.SubCategoryId = model.CategoryId;
                }

                address.SetCoordinates(address.LatitudeString, address.LongitudeString);
                model.Company.Address  = address;
                model.Company.Contacts = model.Contacts;

                if (model.Company.Logo == null || model.LogoFile != null)
                {
                    model.Company.Logo = FileUpload.GetBytes(model.LogoFile, "Logo");
                }
                if (model.Company.Cover == null || model.CoverFile != null)
                {
                    model.Company.Cover = FileUpload.GetBytes(model.CoverFile, "Capa");
                }

                ModelState.Remove("Company.Logo");
                ModelState.Remove("Company.Cover");
                if (!ModelState.IsValid)
                {
                    SetBiewBags(model);
                    return(View(model).Error(ModelState));
                }

                var oldCompany = _db.Companies
                                 .Include(c => c.Address)
                                 .Include(c => c.SubCategory)
                                 .Include(c => c.Contacts)
                                 .FirstOrDefault(x => x.CompanyId == model.Company.CompanyId);
                if (oldCompany == null)
                {
                    return(RedirectToAction("Index").Success("Empresa atualizada com sucesso"));
                }

                // Update parent
                _db.Entry(oldCompany).CurrentValues.SetValues(model.Company);
                oldCompany.Address.UpdateAddress(model.Company.Address);
                _db.Entry(oldCompany.Address).State = EntityState.Modified;

                // Delete children
                foreach (var existingContact in oldCompany.Contacts.ToList())
                {
                    if (model.Contacts.All(c => c.ContactId != existingContact.ContactId))
                    {
                        _db.Contacts.Remove(existingContact);
                    }
                }

                // Update and Insert children
                foreach (var childContact in model.Contacts)
                {
                    var existingContact = oldCompany.Contacts
                                          .FirstOrDefault(c => c.ContactId == childContact.ContactId);

                    childContact.CompanyId = model.Company.CompanyId;
                    if (existingContact != null)
                    {
                        // Update child
                        _db.Entry(existingContact).CurrentValues.SetValues(childContact);
                    }
                    else
                    {
                        // Insert child
                        oldCompany.Contacts.Add(childContact);
                        _db.Contacts.Add(childContact);
                    }
                }

                _db.SaveChanges();
                return(RedirectToAction("Index").Success("Empresa atualizada com sucesso"));
            }
            catch (Exception ex)
            {
                SetBiewBags(model);

                return(View(model).Error(ex.Message));
            }
        }