Example #1
0
        private void dataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                using (MySqlApplicationContext db = new MySqlApplicationContext())
                {
                    foreach (DataGridViewRow item in dataGridView.SelectedRows)
                    {
                        switch (tabControl.SelectedIndex)
                        {
                        case 0:
                            db.Projects.Remove(_projectList.Find(search =>
                                                                 search.Id == int.Parse(item.Cells[0].Value.ToString())));
                            break;

                        case 1:
                            db.Employees.Remove(_employeeList.Find(search =>
                                                                   search.Id == int.Parse(item.Cells[0].Value.ToString())));
                            break;

                        case 2:
                            db.Customers.Remove(_customerList.Find(search =>
                                                                   search.Id == int.Parse(item.Cells[0].Value.ToString())));
                            break;
                        }

                        dataGridView.Rows.Remove(item);
                    }

                    db.SaveChanges();
                    UpdateData();
                }
            }
        }
Example #2
0
        private void EditButton(object sender, EventArgs e)
        {
            using (MySqlApplicationContext db = new MySqlApplicationContext())
            {
                switch (createTypeComboBox.SelectedIndex)
                {
                case 0:
                    if (!Validate(0))
                    {
                        return;
                    }
                    Project project = db.Projects.First(item => item.Id == _id);
                    project.Team          = _teams[projectTeamComboBox.SelectedIndex];
                    project.ProjectStatus = _projectStatuses[projectStatusComboBox.SelectedIndex];
                    project.ProjectType   = _projectTypes[projectTypeComboBox.SelectedIndex];
                    project.Customer      = _customers[projectCustomerComboBox.SelectedIndex];
                    project.OrderDate     = projectOrderDatePicker.Value;
                    project.FinishDate    =
                        project.OrderDate.AddMonths(_projectTypes[projectTypeComboBox.SelectedIndex].Term);
                    db.SaveChanges();
                    project = db.Projects
                              .Include(item => item.Team)
                              .ThenInclude(item => item.Employees)
                              .Include(item => item.ProjectStatus)
                              .Include(item => item.ProjectType)
                              .Include(item => item.Customer)
                              .ThenInclude(item => item.Projects)
                              .First(item => item.Id == project.Id);
                    ReturnValues = new object[]
                    {
                        project.Id,
                        project.Team.Name,
                        project.OrderDate.ToString("dd.MM.yyyy"),
                        project.FinishDate.ToString("dd.MM.yyyy"),
                        project.ProjectStatus.Name,
                        project.ProjectType.Name,
                        project.Customer.FirstName + " " + project.Customer.LastName,
                        project.Team.Employees.Count + project.Customer.Projects.Count + project.ProjectType.Price
                    };
                    break;

                case 1:
                    if (!Validate(1))
                    {
                        return;
                    }
                    Employee employee = db.Employees.First(item => item.Id == _id);
                    employee.Team        = _teams[employeeTeamComboBox.SelectedIndex];
                    employee.Position    = _positions[employeePositionComboBox.SelectedIndex];
                    employee.FirstName   = employeeFirstNameTextBox.Text;
                    employee.LastName    = employeeLastNameTextBox.Text;
                    employee.BirthDate   = employeeBirthDatePicker.Value;
                    employee.HomeAddress = employeeHomeAddressTextBox.Text;
                    employee.Phone       = employeePhoneTextBox.Text;
                    ReturnValues         = new object[]
                    {
                        employee.Id,
                        employee.Team.Name,
                        employee.Position.Name,
                        employee.FirstName + " " + employee.LastName,
                        employee.BirthDate.ToString("dd.MM.yyyy"),
                        employee.HomeAddress,
                        employee.Phone,
                        employee.ApplyDate.ToString("dd.MM.yyyy"),
                        (DateTime.Today - employee.ApplyDate).Days
                    };
                    break;

                case 2:
                    if (!Validate(2))
                    {
                        return;
                    }
                    Customer customer = db.Customers.First(item => item.Id == _id);
                    customer.FirstName = customerFirstNameTextBox.Text;
                    customer.LastName  = customerLastNameTextBox.Text;
                    customer.Email     = customerEmailTextBox.Text;
                    customer.Phone     = customerPhoneTextBox.Text;
                    db.SaveChanges();
                    customer = db.Customers
                               .Include(item => item.Projects)
                               .First(item => item.Id == customer.Id);
                    ReturnValues = new object[]
                    {
                        customer.Id,
                        customer.FirstName + " " + customer.LastName,
                        customer.Email,
                        customer.Phone,
                        customer.Projects.Count
                    };
                    break;
                }

                db.SaveChanges();
            }

            Close();
        }
Example #3
0
        static void Main()
        {
            Console.Write("Инициализация базы данных... ");
            using (MySqlApplicationContext db = new MySqlApplicationContext())
            {
                Console.Write("OK\nУдаление старых данных... ");
                db.Projects.RemoveRange(db.Projects);
                db.SaveChanges();
                db.Customers.RemoveRange(db.Customers);
                db.SaveChanges();
                db.Employees.RemoveRange(db.Employees);
                db.SaveChanges();
                db.Teams.RemoveRange(db.Teams);
                db.SaveChanges();
                db.Positions.RemoveRange(db.Positions);
                db.SaveChanges();
                db.ProjectTypes.RemoveRange(db.ProjectTypes);
                db.SaveChanges();
                db.ProjectStatuses.RemoveRange(db.ProjectStatuses);
                db.SaveChanges();
                db.Database.ExecuteSqlRaw(
                    "ALTER TABLE customers AUTO_INCREMENT = 1;" +
                    "ALTER TABLE employees AUTO_INCREMENT = 1;" +
                    "ALTER TABLE teams AUTO_INCREMENT = 1;" +
                    "ALTER TABLE positions AUTO_INCREMENT = 1;" +
                    "ALTER TABLE projecttypes AUTO_INCREMENT = 1;" +
                    "ALTER TABLE projects AUTO_INCREMENT = 1;" +
                    "ALTER TABLE projectstatuses AUTO_INCREMENT = 1;");
                db.SaveChanges();
                Console.Write("OK\nДобавление типов проектов... ");

                db.ProjectTypes.AddRange(
                    new ProjectType()
                {
                    Name = "Архитектура", Price = 1000, Term = 24
                },
                    new ProjectType()
                {
                    Name = "Дизайн интерьеров", Price = 100, Term = 1
                },
                    new ProjectType()
                {
                    Name = "Разработка концепции", Price = 50, Term = 1
                },
                    new ProjectType()
                {
                    Name = "Проектирование", Price = 100, Term = 12
                },
                    new ProjectType()
                {
                    Name = "Авторский надзор", Price = 100, Term = 1
                },
                    new ProjectType()
                {
                    Name = "Согласование в службах", Price = 50, Term = 1
                }
                    );
                db.SaveChanges();
                Console.Write("OK\nДобавление статусов проектов... ");

                db.ProjectStatuses.AddRange(
                    new ProjectStatus()
                {
                    Name = "Готово"
                },
                    new ProjectStatus()
                {
                    Name = "Отменено"
                },
                    new ProjectStatus()
                {
                    Name = "В процессе"
                },
                    new ProjectStatus()
                {
                    Name = "Не начато"
                }
                    );
                db.SaveChanges();
                Console.Write("OK\nДобавление должностей... ");

                db.Positions.AddRange(
                    new Position()
                {
                    Name = "Архитектор"
                },
                    new Position()
                {
                    Name = "Директор"
                },
                    new Position()
                {
                    Name = "Строитель"
                },
                    new Position()
                {
                    Name = "Бухгалтер"
                },
                    new Position()
                {
                    Name = "Представитель банка"
                }
                    );
                db.SaveChanges();
                Console.Write("OK\nГенерация команд... ");

                for (int i = 0; i < 5; i++)
                {
                    Faker faker = new Faker("ru");
                    db.Teams.Add(new Team()
                    {
                        Name = faker.Address.City()
                    });
                }

                db.SaveChanges();
                Console.Write("OK\nГенерация сотрудников... ");

                for (int i = 0; i < 50; i++)
                {
                    Faker faker = new Faker("ru");
                    db.Employees.Add(new Employee()
                    {
                        FirstName   = faker.Person.FirstName,
                        LastName    = faker.Person.LastName,
                        ApplyDate   = faker.Date.Past(),
                        BirthDate   = faker.Date.Past(30, new DateTime(DateTime.Now.Year - 18, 1, 1)),
                        HomeAddress = faker.Address.FullAddress(),
                        Phone       = faker.Phone.PhoneNumber(),
                        PositionId  = faker.Random.Number(1, db.Positions.Count()),
                        TeamId      = faker.Random.Number(1, db.Teams.Count())
                    });
                }

                db.SaveChanges();
                Console.Write("OK\nГенерация клиентов... ");

                for (int i = 0; i < 20; i++)
                {
                    Faker faker = new Faker("ru");
                    db.Customers.Add(new Customer()
                    {
                        FirstName = faker.Person.FirstName,
                        LastName  = faker.Person.LastName,
                        Email     = faker.Person.Email,
                        Phone     = faker.Phone.PhoneNumber()
                    });
                }

                db.SaveChanges();
                Console.Write("OK\nГенерация проектов... ");

                for (int i = 0; i < 100; i++)
                {
                    Faker    faker         = new Faker("ru");
                    int      teamId        = faker.Random.Number(1, db.Teams.Count());
                    int      projectTypeId = faker.Random.Number(1, db.ProjectTypes.Count());
                    DateTime orderDate     = faker.Date.Past(1, new DateTime(DateTime.Now.Year, 1, 1));
                    db.Projects.Add(new Project()
                    {
                        CustomerId = faker.Random.Number(1, db.Customers.Count()),
                        TeamId     = teamId,
                        OrderDate  = orderDate,
                        FinishDate = faker.Date.Future(1,
                                                       orderDate.AddMonths(db.ProjectTypes.ToList()[projectTypeId - 1].Term)),
                        ProjectStatusId = faker.Random.Number(1, db.ProjectStatuses.Count()),
                        ProjectTypeId   = projectTypeId
                    });
                }

                db.SaveChanges();
                Console.Write("OK\nГенерация прошла успешно!");
                Thread.Sleep(3000);
            }
        }