Пример #1
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));
            }
        }
Пример #2
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));
            }
        }