public LocationModel PostAddLocation(int itemId, LocationModel locationModel, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var user = GetAndValidateUser(sessionKey); var itemModel = this.Data.Items.All().Where(item => item.UserId == user.Id).SingleOrDefault(it => it.Id == itemId); if (itemModel == null) { throw new ArgumentException("Item Not Found!"); } var locationEntry = this.Data.Locations.All().SingleOrDefault(n => n.ItemId == itemId); if (locationEntry == null) { Location location = new Location(); location.ItemId = itemId; location.Latitude = locationModel.Latitude; location.Longitude = locationModel.Longitude; itemModel.Location = location; this.Data.Locations.Add(location); this.Data.SaveChanges(); } else { if (locationEntry.Latitude != locationModel.Latitude) { locationEntry.Latitude = locationModel.Latitude; } if (locationEntry.Longitude != locationModel.Longitude) { locationEntry.Longitude = locationModel.Longitude; } this.Data.SaveChanges(); } return locationModel; }); return responseMsg; }
public LocationModel PutUpdateLocation(LocationModel locationModel, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions(() => { var user = GetAndValidateUser(sessionKey); var locationEntry = this.Data.Locations.All().Where(n => n.Item.User.Id == user.Id).SingleOrDefault(n => n.Id == locationModel.Id); if (locationModel == null) { throw new ArgumentException("Note Not Found!"); } if (locationEntry.Latitude != locationModel.Latitude) { locationEntry.Latitude = locationModel.Latitude; } if (locationEntry.Longitude != locationModel.Longitude) { locationEntry.Longitude = locationModel.Longitude; } this.Data.SaveChanges(); return locationModel; }); return responseMsg; }