// GET api/<controller> public IEnumerable<Location> GetAllLocations() { try { List<Location> locations = new List<Location>(); string connectionString = ConfigurationManager.ConnectionStrings["FreeFootieConnectionString"].ConnectionString; using (FreeFootieDataContext myData = new FreeFootieDataContext(connectionString)) { var results = (from c in myData.locations orderby c.id ascending select c).ToList(); foreach (var temp in results) { Location location = new Location(); location.id = temp.id; location.name = temp.name; location.latitude = temp.latitude; location.longitude = temp.longitude; locations.Add(location); } } return locations; } catch (Exception ex) { Common.ErrorCatch(ex, "LocationController.GetAllLocations"); return null; } }
// GET api/<controller>/5 public Location GetLocationById(int id) { try { string connectionString = Properties.Settings.Default.FreeFootieConnectionString; using (FreeFootieDataContext myData = new FreeFootieDataContext(connectionString)) { var temp = (from c in myData.locations where c.id == id orderby c.id ascending select c).SingleOrDefault(); Location location = new Location(); location.id = temp.id; location.name = temp.name; location.latitude = temp.latitude; location.longitude = temp.longitude; return location; } } catch (Exception ex) { Common.ErrorCatch(ex, "LocationController.GetLocationById"); return null; } }
// POST api/<controller> public void PostLocation(Location location) { try { string connectionString = Properties.Settings.Default.FreeFootieConnectionString; using (FreeFootieDataContext myData = new FreeFootieDataContext(connectionString)) { using (TransactionScope myScope = new TransactionScope()) { location newLocation = new location(); var newID = (from c in myData.locations select c.id).Max(); newLocation.id = newID + 1; newLocation.name = location.name; newLocation.latitude = location.latitude; newLocation.longitude = location.longitude; myData.locations.InsertOnSubmit(newLocation); myData.SubmitChanges(); myScope.Complete(); } } } catch (Exception ex) { Common.ErrorCatch(ex, "LocationController.PostLocation"); } }