Exemplo n.º 1
0
        public ActionResult Create(NewEstablishmentViewModel viewModel)
        {
            viewModel.Establishment.CreatedDateTime = DateTime.Now;

            // Geocodes the address.
            try
            {
                var geocoder = new GoogleGeocodingService();
                var coords   = geocoder.GetCoordinate(viewModel.Establishment.Address);
                viewModel.Establishment.Latitude  = coords.Latitude;
                viewModel.Establishment.Longitude = coords.Longitude;
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Não foi possível localizar o endereço. Detalhes: " + ex.Message);
            }

            if (ModelState.IsValid)
            {
                if (ExistsEstablishment(viewModel.Establishment.Name))
                {
                    ModelState.AddModelError("", string.Format("O Estabelecimento \"{0}\" já está cadastrado", viewModel.Establishment.Name));
                    return(View(viewModel));
                }

                db.Establishments.Add(viewModel.Establishment);
                db.SaveChanges();

                viewModel.Review.EstablishmentId = viewModel.Establishment.Id;
                viewModel.Review.CreatedDateTime = DateTime.Now;

                db.Reviews.Add(viewModel.Review);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View(viewModel));
        }
 public NewEstablishmentPage()
 {
     InitializeComponent();
     BindingContext = viewModel = new NewEstablishmentViewModel();
 }