//[HttpGet] //[ActionName("nearbyPlaces")] public int GetNearbyPlace(double latitude, double longitude) { var context = new NationalPlacesContext(); NationalPlaceModel nearbyPlace = new NationalPlaceModel(); double minDistance = double.MaxValue; foreach (var place in context.Places) { double distance = CheckDistance(place.Latitude, place.Longitude, latitude, longitude); if (minDistance > distance) { nearbyPlace.Id = place.Id; minDistance = distance; } } //var response = this.Request.CreateResponse(HttpStatusCode.OK, nearbyPlace.Id); return nearbyPlace.Id; }
public HttpResponseMessage GetPlaceDetails(int placeId) { try { var context = new NationalPlacesContext(); var place = context.Places.FirstOrDefault(p => p.Id == placeId); var placeDetails = new NationalPlaceModel() { Id = place.Id, Name = place.Name, Description = place.Description, PictureUrl = place.PictureUrl, Town = place.Town.Name }; var response = this.Request.CreateResponse(HttpStatusCode.OK, placeDetails); return response; } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); return response; } }
public HttpResponseMessage PostPlace(NationalPlaceModel place) { try { var context = new NationalPlacesContext(); using(context) { if(place.Name==null || place.Name=="") { throw new ArgumentNullException("Inavalid place name"); } if(place.Description==null || place.Description=="") { throw new ArgumentNullException("Invalid description"); } var town=context.Towns.FirstOrDefault(t => t.Name == place.Town); if (town==null) { town=context.Towns.Add(new Town() { Name = place.Town, }); context.SaveChanges(); } Place newPlace = new Place() { Name = place.Name, Description = place.Description, Town = town, PictureUrl = place.PictureUrl, Latitude=place.Latitude, Longitude = place.Longitude }; context.Places.Add(newPlace); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created); return response; } } catch(Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); return response; } }