static void Main(string[] args) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<OrganizerContext, Configuration>()); using (var context = new OrganizerContext()) { var user = context.Users.FirstOrDefault(u => u.Username == "bayIvan5"); if (user == null) { user = new User(); user.AuthCode = "0123456789012345678901234567890123456789"; user.DisplayName = "Ivan Ivanov2"; user.Username = "******"; context.Users.Add(user); context.SaveChanges(); } Item rootUserItem = new Item(); rootUserItem.ItemType = ItemType.Type; rootUserItem.Title = "Root"; rootUserItem.User = user; context.Items.Add(rootUserItem); Location loc = new Location(); loc.Latitude = 40; loc.Longitude = 20; loc.ItemId = 4; context.Locations.Add(loc); context.SaveChanges(); } }
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; }