Ejemplo n.º 1
0
  public void MainMenu()
 {
     int? choice = null;
     do
     {
         choice = MenuUtils.GetIntChoice(MenuUtils.BaseChoiceMenu(), 1, 4);
         switch (choice)
         {
             case 1:
                 EmployeeMenu();
                 break;
             case 2:
                 ServiceMenu();
                 break;
             case 3:
                 using (var db = new EmployeeContext())
                 {
                     Console.WriteLine(db.Employees.AsNoTracking().Sum(x => x.Salary));
                 }
                 break;
             case 4:
                 Environment.Exit(0);
                 break;
             default:
                 break;
         }
     } while (true);
 }
Ejemplo n.º 2
0
        public async void MainMenu()
        {
            int?choice = null;

            do
            {
                choice = MenuUtils.GetIntChoice(MenuUtils.BaseChoiceMenu(), 1, 4);
                switch (choice)
                {
                case 1:
                    EmployeeMenu();
                    break;

                case 2:
                    ServiceMenu();
                    break;

                case 3:
                    List <Employee> employees = await EmployeesWebService.Get();

                    Console.WriteLine(employees.Sum(x => x.Salary));
                    break;

                case 4:
                    Environment.Exit(0);
                    break;

                default:
                    break;
                }
            } while (true);
        }
Ejemplo n.º 3
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"));
        }