コード例 #1
0
        public ActionResult Create(SubCategory model, HttpPostedFileBase imageFile)
        {
            try
            {
                if (imageFile != null)
                {
                    model.SetImage(FileUpload.GetBytes(imageFile, "Imagem"));
                }

                else
                {
                    return(View(model).Error("A imagem da categoria é obrigatória"));
                }

                if (!ModelState.IsValid)
                {
                    return(View(model).Error(ModelState));
                }

                _db.SubCategories.Add(model);
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View(model).Error(ex.Message));
            }
        }
コード例 #2
0
        public ActionResult Create(CrudEventVm model, AddressForEventVm address)
        {
            try
            {
                model.Address       = address;
                model.Event.Address = model.GetAddress(address);
                model.Event.Address.SetCoordinates(address.LatitudeString, address.LongitudeString);

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

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

                _db.Events.Add(model.Event);
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                SetBiewBags(model);
                return(View(model).Error(ex.Message));
            }
        }
コード例 #3
0
        public ActionResult Edit(CrudHistoricalSightVm model, Address address)
        {
            try
            {
                model.HistoricalSight.Address = address;
                model.HistoricalSight.Address.SetCoordinates(address.LatitudeString, address.LongitudeString);

                if (model.HistoricalSight.Logo == null || model.LogoFile != null)
                {
                    model.HistoricalSight.Logo = FileUpload.GetBytes(model.LogoFile, "Logo");
                }

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

                ModelState.Remove("HistoricalSight.Logo");
                ModelState.Remove("HistoricalSight.Cover");

                if (!ModelState.IsValid)
                {
                    SetBiewBags(model);
                    return(View(model).Error(ModelState));
                }

                var oldHistoricalSight = _db.HistoricalSights
                                         .Include(c => c.Address)
                                         .FirstOrDefault(x => x.HistoricalSightId == model.HistoricalSight.HistoricalSightId);

                if (oldHistoricalSight == null)
                {
                    return(RedirectToAction("Index").Success("Memorial atualizado com sucesso"));
                }

                _db.Entry(oldHistoricalSight).CurrentValues.SetValues(model.HistoricalSight);

                oldHistoricalSight.Address.UpdateAddress(model.HistoricalSight.Address);
                _db.Entry(oldHistoricalSight.Address).State = EntityState.Modified;
                _db.SaveChanges();

                return(RedirectToAction("Index").Success("Memorial atualizado com sucesso"));
            }
            catch (Exception ex)
            {
                SetBiewBags(model);

                return(View(model).Error(ex.Message));
            }
        }
コード例 #4
0
        public ActionResult Edit(CrudEventVm model, AddressForEventVm address)
        {
            try
            {
                model.Address       = address;
                model.Event.Address = model.GetAddress(address);
                model.Event.Address.SetCoordinates(address.LatitudeString, address.LongitudeString);

                if (model.Event.Logo == null || model.LogoFile != null)
                {
                    model.Event.Logo = FileUpload.GetBytes(model.LogoFile, "Logo");
                }

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

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

                var oldCompany = _db.Events
                                 .Include(c => c.Address)
                                 //.Include(c => c.Contacts)
                                 .FirstOrDefault(x => x.EventId == model.Event.EventId);
                if (oldCompany == null)
                {
                    return(RedirectToAction("Index").Success("Empresa atualizada com sucesso"));
                }

                // Update parent
                _db.Entry(oldCompany).CurrentValues.SetValues(model.Event);
                oldCompany.Address.UpdateAddress(model.Event.Address);
                _db.Entry(oldCompany.Address).State = EntityState.Modified;
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                SetBiewBags(model);

                return(View(model).Error(ex.Message));
            }
        }
コード例 #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
        public ActionResult Edit(SubCategory model, HttpPostedFileBase imageFile)
        {
            try
            {
                if (imageFile == null)
                {
                    return(View(model).Error("A imagem da categoria é obrigatória"));
                }

                if (!ModelState.IsValid)
                {
                    return(View(model));
                }

                model.SetImage(FileUpload.GetBytes(imageFile, "Imagem"));
                _db.Entry(model).State = EntityState.Modified;
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(View(model).Error(ex.Message));
            }
        }
コード例 #7
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));
            }
        }