Ejemplo n.º 1
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;
        }
    }
Ejemplo n.º 2
0
        public void MainMenu()
        {
            int?subChoice = null;

            do
            {
                int?choice = MenuUtils.GetIntChoice(MenuUtils.BaseChoiceMenu(), 1, 2);
                switch (choice)
                {
                case 1:
                    subChoice = MenuUtils.GetIntChoice(MenuUtils.CrudMenuFor("employee"), 1, 5);
                    switch (subChoice)
                    {
                    case 1:
                        using (var db = new EmployeeContext())
                        {
                            Employee employee = MenuUtils.BuildEmployeeWithService();

                            db.Employees.Add(employee);

                            db.SaveChanges();
                        }
                        break;

                    case 2:
                        using (var db = new EmployeeContext())
                        {
                            foreach (var item in db.Employees.Include(x => x.Department).ToList())
                            {
                                Console.WriteLine(item);
                            }
                        }
                        break;

                    case 3:
                        using (var db = new EmployeeContext())
                        {
                            string firstname = MenuUtils.GetString("Firstname");
                            string lastname  = MenuUtils.GetString("Lastname");
                            foreach (var item in db.Employees.Where(x => x.Firstname.Contains(firstname) && x.Lastname.Contains(lastname)).Include(x => x.Department).ToList())
                            {
                                Console.WriteLine(item);
                            }
                        }
                        break;

                    case 4:
                        int id = MenuUtils.GetIntChoice("Choose an id", 0, int.MaxValue).Value;
                        using (var db = new EmployeeContext())
                        {
                            Employee employee = new Employee()
                            {
                                EmployeeId = id
                            };

                            db.Employees.Attach(employee);
                            db.Employees.Remove(employee);

                            db.SaveChanges();
                        }
                        break;

                    case 5:
                        int      idUpdate    = MenuUtils.GetIntChoice("Choose an id", 0, int.MaxValue).Value;
                        Employee empToUpdate = MenuUtils.BuildEmployee();
                        using (var db = new EmployeeContext())
                        {
                            Employee empUpdate = db.Employees.Find(idUpdate);

                            db.Employees.Attach(empUpdate);
                            empUpdate.City        = empToUpdate.City;
                            empUpdate.DateOfBirth = empToUpdate.DateOfBirth;
                            empUpdate.Firstname   = empToUpdate.Firstname;
                            empUpdate.Lastname    = empToUpdate.Lastname;
                            empUpdate.Salary      = empToUpdate.Salary;
                            empUpdate.Function    = empToUpdate.Function;

                            db.Entry(empUpdate).State = EntityState.Modified;

                            db.SaveChanges();
                        }
                        break;

                    default:
                        break;
                    }
                    break;

                case 2:
                    subChoice = MenuUtils.GetIntChoice(MenuUtils.CrudMenuFor("service"), 1, 5);
                    switch (subChoice)
                    {
                    case 1:
                        using (var db = new EmployeeContext())
                        {
                            db.Services.Add(MenuUtils.BuildService());
                            db.SaveChanges();
                        }
                        break;

                    case 2:
                        using (var db = new EmployeeContext())
                        {
                            foreach (var item in db.Services.ToList())
                            {
                                Console.WriteLine(item);
                            }
                        }
                        break;

                    case 3:
                        string name = MenuUtils.GetString("Name");
                        using (var db = new EmployeeContext())
                        {
                            foreach (var item in db.Services.Where(x => x.Name.Contains(name)).ToList())
                            {
                                Console.WriteLine(item);
                            }
                        }
                        break;

                    case 4:
                        int id = MenuUtils.GetIntChoice("Choose an id", 0, int.MaxValue).Value;
                        using (var db = new EmployeeContext())
                        {
                            Service service = new Service()
                            {
                                ServiceId = id
                            };

                            db.Services.Attach(service);
                            db.Services.Remove(service);

                            foreach (var item in db.Employees.Include(x => x.Department).Where(x => x.Department.ServiceId == id))
                            {
                                db.Employees.Attach(item);
                                item.Department = new Service()
                                {
                                    Name = "changed"
                                };
                                db.Entry(item).State = EntityState.Modified;
                            }

                            try
                            {
                                db.SaveChanges();
                            }
                            catch (DbEntityValidationException e)
                            {
                                Console.WriteLine(e);
                            }
                        }
                        break;

                    case 5:
                        int     idUpdate     = MenuUtils.GetIntChoice("Choose an id", 0, int.MaxValue).Value;
                        Service servToUpdate = MenuUtils.BuildService();
                        using (var db = new EmployeeContext())
                        {
                            Service servUpdate = db.Services.Find(idUpdate);

                            db.Services.Attach(servUpdate);
                            servUpdate.Name = servToUpdate.Name;

                            db.Entry(servUpdate).State = EntityState.Modified;

                            db.SaveChanges();
                        }
                        break;

                    default:
                        break;
                    }
                    break;

                default:
                    break;
                }
            } while (!MenuUtils.GetStringChoice("y to quit else n", "y", "n").Equals("y"));
        }