public void BtnSaveEmployeeChanges() { using (var ctx = new DatabaseDir.Database()) { ctx.Employees.Attach(SelectedEmployee); ctx.Entry(SelectedEmployee).State = EntityState.Modified; ctx.SaveChanges(); new Notification(Notification.NotificationType.Edited, $"{SelectedEmployee.Fullname} er blevet opdateret i databasen."); } }
public void BtnDeleteSelectedRoute() { Task.Run(() => { using (var ctx = new DatabaseDir.Database()) { ctx.Routes.Attach(SelectedRoute); ctx.Entry(SelectedRoute).State = EntityState.Deleted; ctx.SaveChanges(); new Notification(Notification.NotificationType.Removed, "Den valgte rute er blevet fjernet fra databasen."); SelectedEmployee.Routes.Remove(SelectedRoute); SelectedRoute = null; NotifyOfPropertyChange(() => RouteCollection); } }); }
public void BtnFireSelectedEmployee() { Task.Run(() => { using (var ctx = new DatabaseDir.Database()) { // Flip the fired status of the employee and update the database SelectedEmployee.IsFired = !SelectedEmployee.IsFired; ctx.Employees.Attach(SelectedEmployee); ctx.Entry(SelectedEmployee).State = EntityState.Modified; ctx.SaveChanges(); NotifyOfPropertyChange(() => TitleInformation); new Notification(Notification.NotificationType.Edited, $"{SelectedEmployee.Fullname} er blevet opdateret i databasen."); } }); }
public async Task BtnAddNewEmployee() { DialogResult employeeIdCheck = MessageBox.Show($"Kan det passe at løn nummeret er {NewEmployee.Id}?", "Bekræft løn nummer", MessageBoxButtons.YesNo); if (employeeIdCheck == DialogResult.No) { return; } using (var ctx = new DatabaseDir.Database()) { CanBtnAddNewEmployee = false; Cursor.Current = Cursors.WaitCursor; bool success = await Task <bool> .Run(() => { try { ctx.Employees.Add(NewEmployee); ctx.SaveChanges(); return(true); } catch (DbUpdateException ex) { // Should we log exceptions? return(false); } }); if (success) { new Notification(Notification.NotificationType.Added, $"{NewEmployee.Fullname} er blevet tilføjet til databasen."); NewEmployee = new Employee(); AllEmployees = await GetEmployeesAsync(); EmployeeCollection = new BindableCollection <Employee>(AllEmployees); } else { new Notification(Notification.NotificationType.Error, "Der skete en fejl. Tjek de indtastede informationer og prøv igen.", 7.5f); } CanBtnAddNewEmployee = true; Cursor.Current = Cursors.Default; } }
public void BtnAddNewRoute() { if (NewRoute.Distance < 1) { new Notification(Notification.NotificationType.Error, "Distancen kan ikke være mindre end 1"); return; } Task.Run(() => { // Make sure the entered rate value is valid compared to the distance double calculatedRate = (NewRoute.LinkedWorkplace.MaxPayout / NewRoute.Distance); bool isValid = (calculatedRate <= StateRouteRate); if (!isValid) { new Notification(Notification.NotificationType.Error, $"Satsen {calculatedRate},- DKK/km overskrider statens takst {StateRouteRate},- DDK/km"); NewRoute.RateValue = StateRouteRate; } else { NewRoute.RateValue = calculatedRate; } using (var ctx = new DatabaseDir.Database()) { ctx.Routes.Add(NewRoute); ctx.Entry(NewRoute.LinkedWorkplace).State = EntityState.Detached; ctx.SaveChanges(); // Reload the virtual property again. ctx.Entry(NewRoute).Reference(c => c.LinkedWorkplace).Load(); SelectedEmployee.Routes.Add(NewRoute); NewRoute = new Route(); NewRoute.EmployeeID = SelectedEmployee.Id; new Notification(Notification.NotificationType.Added, $"Ruten er blevet tilføjet i databasen."); SelectedWorkplace = null; NotifyOfPropertyChange(() => RouteCollection); } }); }
public void BtnDeleteSelectedEntry() { //Check if found if (EntriesCollection.Where(x => x.ID == SelectedEntry.ID).Count() == 0) { return; } //Find the entryrow EntryRow entryRow = EntriesCollection.FirstOrDefault(x => x.ID == SelectedEntry.ID); //Delete from datagrid EntriesCollection.Remove(entryRow); NotifyOfPropertyChange(() => EntriesCollection); VismaEntry vismaEntry; //Delete vismaentry from database and its timesheetentry if it was the last vismaentry. using (var ctx = new DatabaseDir.Database()) { //Find the object in th database and delete vismaEntry = ctx.VismaEntries.Where(v => v.Id == SelectedEntry.ID).First(); ctx.VismaEntries.Remove(vismaEntry); ctx.SaveChanges(); } //Check and delete timesheetEntry if it no longer contains any vismaEntries using (var ctx = new DatabaseDir.Database()) { //Search for other vismaentries with the same timesheet id List <VismaEntry> list = new List <VismaEntry>(ctx.VismaEntries.Where(x => x.TimesheetEntryID == vismaEntry.TimesheetEntryID)); if (list.Count() == 0) { TimesheetEntry timesheetEntry = ctx.TimesheetEntries.FirstOrDefault(x => x.Id == vismaEntry.TimesheetEntryID); //If none, then delete ctx.TimesheetEntries.Remove(timesheetEntry); ctx.SaveChanges(); } } }
public async Task BtnAddNewWorkplace() { if (NewWorkplace.MaxPayout <= 0) { new Notification(Notification.NotificationType.Error, "Max beløb kan ikke være lig 0"); return; } using (var ctx = new DatabaseDir.Database()) { CanAddNewWorkplace = false; bool success = await Task <bool> .Run(() => { try { ctx.Workplaces.Add(NewWorkplace); ctx.SaveChanges(); return(true); } catch (Exception ex) { // Should we log exceptions? return(false); } }); if (success) { new Notification(Notification.NotificationType.Added, $"{NewWorkplace.Name} er blevet tilføjet til databasen."); NewWorkplace = new Workplace(); AllWorkplaces = await GetWorkplacesAsync(); WorkplaceCollection = new BindableCollection <Workplace>(AllWorkplaces); } CanAddNewWorkplace = true; } }
public void BtnDeleteSelectedWorkplace() { //If not found if (WorkplaceCollection.Where(x => x == SelectedWorkplace).Count() == 0) { return; } //Find the entryrow Workplace workplace = WorkplaceCollection.FirstOrDefault(x => x == SelectedWorkplace); //Delete from datagrid WorkplaceCollection.Remove(workplace); NotifyOfPropertyChange(() => WorkplaceCollection); // Archives the selected workplace using (var ctx = new DatabaseDir.Database()) { ctx.Workplaces.First(x => x.Id == workplace.Id).Archived = true; ctx.SaveChanges(); } }