Esempio n. 1
0
    private void EmployeeCudMenu()
    {
        int? choice = MenuUtils.GetIntChoice(MenuUtils.CudMenu(), 1, 4);

        switch (choice)
        {
            case 1:
                using (var db = new EmployeeContext())
                {
                    db.Employees.Add(MenuUtils.BuildEmployeeWithService(db));
                    db.SaveChanges();
                }
                break;
            case 2:
                PrintFromDb<Employee>((EmployeeContext db) =>
                {
                    return db.Employees.AsNoTracking().ToList();
                });
                using (var db = new EmployeeContext())
                {
                    Employee empToUpdate = null;
                    do
                    {
                        empToUpdate = db.Employees.Find(MenuUtils.GetIntChoice("Select employee to update by id", 1, int.MaxValue));
                    } while (empToUpdate == null);

                    Employee newValues = MenuUtils.BuildEmployeeWithService(db);
                    newValues.EmployeeId = empToUpdate.EmployeeId;
                    db.Entry(empToUpdate).CurrentValues.SetValues(newValues);
                    db.SaveChanges();
                }
                break;
            case 3:
                PrintFromDb<Employee>((EmployeeContext db) =>
                {
                    return db.Employees.AsNoTracking().ToList();
                });
                using (var db = new EmployeeContext())
                {
                    Employee empToDelete = null;
                    do
                    {
                        empToDelete = db.Employees.Find(MenuUtils.GetIntChoice("Select employee to delete by id", 1, int.MaxValue));
                    } while (empToDelete == null);

                    db.Entry(empToDelete).State = EntityState.Deleted;
                    db.SaveChanges();
                }
                break;
            case 4:
                EmployeeMenu();
                break;
            default:
                break;
        }
    }
Esempio n. 2
0
    private void ServiceCudMenu()
    {
        int? choice = MenuUtils.GetIntChoice(MenuUtils.CudMenu(), 1, 4);

        switch (choice)
        {
            case 1:
                using (var db = new EmployeeContext())
                {
                    db.Services.Add(MenuUtils.BuildService());
                    db.SaveChanges();
                }
                break;
            case 2:
                PrintFromDb<Service>((EmployeeContext db) =>
                {
                    return db.Services.AsNoTracking().ToList();
                });
                using (var db = new EmployeeContext())
                {
                    Service srvToUpdate = null;
                    do
                    {
                        srvToUpdate = db.Services.Find(MenuUtils.GetIntChoice("Select service to update by id", 1, int.MaxValue));
                    } while (srvToUpdate == null);

                    Service newValues = MenuUtils.BuildService();
                    newValues.ServiceId = srvToUpdate.ServiceId;
                    db.Entry(srvToUpdate).CurrentValues.SetValues(newValues);
                    db.SaveChanges();
                }
                break;
            case 3:
                PrintFromDb<Service>((EmployeeContext db) =>
                {
                    return db.Services.AsNoTracking().ToList();
                });
                using (var db = new EmployeeContext())
                {
                    Service srvToDelete = null;
                    do
                    {
                        srvToDelete = db.Services.Find(MenuUtils.GetIntChoice("Select service to delete by id", 1, int.MaxValue));
                    } while (srvToDelete == null);

                    db.Entry(srvToDelete).State = EntityState.Deleted;

                    //Check if services remaining
                    if (db.Employees.AsNoTracking().Include(x => x.Department).Where(x => x.Department.ServiceId == srvToDelete.ServiceId).Count() > 0)
                    {
                        PrintFromDb<Employee>((EmployeeContext db1) =>
                        {
                            return db.Employees.AsNoTracking().Include(x => x.Department).Where(x => x.Department.ServiceId == srvToDelete.ServiceId).ToList();
                        });

                        Console.WriteLine("------------------------------------");

                        int? changeId;
                        Service newService;
                        do
                        {
                            PrintFromDb<Service>((EmployeeContext db1) =>
                            {
                                return db.Services.AsNoTracking().Where(x => x.ServiceId != srvToDelete.ServiceId).ToList();
                            });

                            changeId = MenuUtils.GetIntChoice("Select new service id for linked employees", 1, int.MaxValue);
                            newService = db.Services.Find(changeId);
                        } while (newService == null || changeId == srvToDelete.ServiceId);

                        foreach (var item in db.Employees.Include(x => x.Department).Where(x => x.Department.ServiceId == srvToDelete.ServiceId).ToList())
                        {
                            item.Department = newService;
                            db.Entry(item).State = EntityState.Modified;
                        }
                    }
                    else
                    {
                        //Delete linked employees
                        foreach (var item in db.Employees.Include(x => x.Department).Where(x => x.Department.ServiceId == srvToDelete.ServiceId).ToList())
                        {
                            db.Entry(item).State = EntityState.Deleted;
                        }
                    }
                    db.SaveChanges();
                }
                break;
            case 4:
                ServiceMenu();
                break;
            default:
                break;
        }
    }