private void BtnSave_OnClick(object sender, RoutedEventArgs e) { var assignedShop = (Shop)comboBoxShops.SelectedItem; // create inmate object from data entered var newInmate = CreateInmateObject(assignedShop); string statusMsg = ToolTrackerService.ValidateInmateData(newInmate); if (AlertUserIfNotValidated(statusMsg)) { return; } newInmate.FirstName = ToolTrackerService.MakeFirstLetterUppercase(newInmate.FirstName); newInmate.LastName = ToolTrackerService.MakeFirstLetterUppercase(newInmate.LastName); // update database and collection try { using (var db = new UnitOfWork()) { // Used for validation but not needed here. Avoids re-adding the shop. newInmate.AssignedShop = null; db.InmatesRepo.Add(newInmate); db.Commit(); } } catch (EntityException ex) { ExceptionHandler.LogAndNotifyOfException(ex, ex.Message); } catch (Exception ex) { ExceptionHandler.LogAndNotifyOfException(ex, ex.Message); } ToolTrackerService.MapInmateToViewModel(newInmate); ToolTrackerService.SortInmates(); // change text in cancel button to "I'm Finished" btnCancel.Content = "I'm Finished"; // update status textBlockStatus.Text = $"{newInmate.FirstName} {newInmate.LastName} has been added."; ResetForm(); }
private void BtnSaveUpdateInmate_OnClick(object sender, RoutedEventArgs e) { var btn = (Button)sender; // Tag attribute on button is bound to Id int inmateId = (int)btn.Tag; // Get Inmate object from collection var inmate = ToolTrackerService.Inmates.SingleOrDefault(i => i.Id == inmateId); var result = NotifyUser.AskToConfirm("Are you sure you want to save?", "Confirm Update"); if (result != System.Windows.Forms.DialogResult.Yes) { return; } if (inmate != null && inmate.AssignedShop == null) { inmate.AssignedShop = ToolTrackerService.Shops.FirstOrDefault(s => s.Id == inmate.ShopId); } string statusMsg = ToolTrackerService.ValidateInmateData(inmate); if (statusMsg != null) { NotifyUser.InvalidEntry(statusMsg, "Invalid Entry"); return; } // Ensure first letter is uppercase if (inmate != null) { inmate.FirstName = ToolTrackerService.MakeFirstLetterUppercase(inmate.FirstName); inmate.LastName = ToolTrackerService.MakeFirstLetterUppercase(inmate.LastName); } UpdateInmate(inmate); }