Exemplo n.º 1
0
 public static Stop Format(Models.Place place)
 {
     return(new Stop()
     {
         Location = place.Coordinate,
         Name = place.Name
     });
 }
        public ActionResult Create(SaltedButterViewModel submittedPlace)
        {
            if (ModelState.IsValid)
            {
                Models.Place place = new Models.Place();
                string _address = "http://demo.places.nlp.nokia.com/places/v1/places/" + submittedPlace.PlaceId + "?app_id=EOXbMEWYAllPhQnAQsmn&app_code=9TIppnJDB9PHy8-ckJLWXA";
                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Add("Accept", "application/json");

                // Send a request asynchronously continue when complete
                var taskGet = client.GetAsync(_address).ContinueWith(
                (requestTask) =>
                {
                    // Get HTTP response from completed task.
                    HttpResponseMessage response = requestTask.Result;

                    // Check that response was successful or throw exception
                    response.EnsureSuccessStatusCode();

                    response.Content.ReadAsAsync(typeof(NokiaPlace)).ContinueWith(
                    (readTask) =>
                    {
                        // Mapping information provided by API
                        NokiaPlace nokiaPlace = (NokiaPlace)readTask.Result;
                        place.Address1 = nokiaPlace.Location.Address["street"];
                        place.City = nokiaPlace.Location.Address["city"];
                        place.PostalCode = nokiaPlace.Location.Address["postalCode"];
                        place.Country = nokiaPlace.Location.Address["country"];
                        place.Latitude = nokiaPlace.Location.Position[0];
                        place.Longitude = nokiaPlace.Location.Position[1];
                    });

                });

                place.Name = submittedPlace.Name;

                Models.User user = new Models.User(){
                    Email = submittedPlace.Email,
                    Username = submittedPlace.UserName,
                    CreationDate = DateTime.Now
                };

                Models.SaltedButter saltedButter = new Models.SaltedButter()
                {
                    Salted = Convert.ToBoolean(submittedPlace.Status),
                    Comment = submittedPlace.Comment
                };

                taskGet.Wait();

                Models.Action action = new Models.Action()
                {
                    Place = place,
                    SaltedButter = saltedButter,
                    User = user,
                    CreationDate = DateTime.Now,
                    StatusId = 1,
                    CategoryId = 1
                };

                _dataContext.Actions.InsertOnSubmit(action);

                _dataContext.SubmitChanges();

                MapViewModel map = new MapViewModel()
                {
                    Latitude = place.Latitude,
                    Longitude = place.Longitude
                };

                //_dataContext.Places.InsertOnSubmit(place);
                TempData["success"] = "ok"; // Indicating to display ok notification after redirection
                return RedirectToAction("Index", "Map", map);

            }

            return PartialView("NoteAdd",submittedPlace);
        }