public ManageItem(ItemViewModel itemViewModel) { this.InitializeComponent(); this.tbTitle.Text = "Update Item"; InitializeFlyout(); this.itemViewModel = itemViewModel; this.txtTitle.Text = itemViewModel.Title; this.txtDescription.Text = itemViewModel.Description; this.txtCost.Text = itemViewModel.Cost.ToString(); this.cboCategories.SelectedItem = cboCategories.Items[((int)itemViewModel.Category) - 2]; }
async private void btnSave_Click(object sender, RoutedEventArgs e) { try { this.txtStatusMessage.Visibility = Windows.UI.Xaml.Visibility.Visible; this.txtStatusMessage.Text = "Please wait..."; if (AreFieldsValid()) { if (this.itemViewModel == null) { ItemViewModel item = new ItemViewModel() { Identifier = -1, Title = this.txtTitle.Text, Description = this.txtDescription.Text, Cost = Convert.ToInt64(this.txtCost.Text), Trip = MainViewModel.GetSelectedTrip(), Traveler = MainViewModel.GetTraveler(), Category = (short)((PairViewModel)cboCategories.SelectedItem).Identifier }; item.Identifier = await MainViewModel.SaveItem(item); this.txtStatusMessage.Text = "Trip has been created"; } else { itemViewModel.Title = this.txtTitle.Text; itemViewModel.Description = this.txtDescription.Text; itemViewModel.Trip = MainViewModel.GetSelectedTrip(); itemViewModel.Trip.ItemsCost -= itemViewModel.Cost; itemViewModel.Cost = Convert.ToInt64(this.txtCost.Text); itemViewModel.Traveler = MainViewModel.GetTraveler(); itemViewModel.Category = (short)((PairViewModel)cboCategories.SelectedItem).Identifier; await MainViewModel.SaveItem(itemViewModel); this.txtStatusMessage.Text = "Trip has been updated"; } this.backButton_Click(sender, e); } } catch (Exception) { this.txtStatusMessage.Text = "Something is wrong, please review the fields and try again"; } }
async public Task UpdateItem(ItemViewModel itemViewModel) { SQLite.SQLiteAsyncConnection context = new SQLite.SQLiteAsyncConnection(connectionString); List<ItemViewModel> result = new List<ItemViewModel>(); Item item = await context.Table<Item>().Where(p => p.Identifier == itemViewModel.Identifier).FirstOrDefaultAsync(); item.Title = itemViewModel.Title; item.Description = itemViewModel.Description; item.Cost = itemViewModel.Cost; item.Category = itemViewModel.Category; if (item != null) { await context.UpdateAsync(item); } }
async public Task<int> SaveItemAsync(ItemViewModel item) { SQLite.SQLiteAsyncConnection context = new SQLite.SQLiteAsyncConnection(connectionString); Item newItem = new Item() { Identifier = item.Identifier, Title = item.Title, Description = item.Description, Cost = item.Cost, Latitude = item.Latitude, Longitude = item.Longitude, Parent = item.Trip.Identifier, Traveler = item.Traveler.ToString(), Category = item.Category }; await context.InsertAsync(newItem); return newItem.Identifier; }
async public static Task<int> SaveItem(ItemViewModel item) { if (item.Identifier == -1) { item.Identifier = await model.SaveItemAsync(item); item.Trip.DeleteFakeItem(); } else { await model.UpdateItem(item); item.Group.Items.Remove(item); if (item.Group.Items.Count == 0) { item.Trip.ItemGroups.Remove(item.Group); } } item.Trip.ItemsCost += Convert.ToInt64(item.Cost); CategoryViewModel category = item.Trip.CreateCategory((Category)item.Category); item.Group = category; category.Items.Add(item); return item.Identifier; }
private static void CreateFakeItem(TripViewModel trip) { CategoryViewModel category = trip.CreateCategory(Category.Things); if (category.Items.Count == 0) { ItemViewModel item = new ItemViewModel(); item.Identifier = -1; item.Title = "Note"; item.Description = "Add items from the botton toolbar"; item.Group = category; item.Trip = trip; category.Items.Add(item); } }