From ee5464553a84efa9abb656431830575ee0a97db4 Mon Sep 17 00:00:00 2001 From: frog24 Date: Mon, 20 May 2024 04:10:00 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=81=D0=B5=20=D0=BE=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BD=D0=B0=20=D0=B2=D1=81=D0=B5=20?= =?UTF-8?q?=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20=D0=BA=D1=80?= =?UTF-8?q?=D0=BE=D0=BC=D0=B5=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D1=81=D0=BC=D0=B6=D0=BD=D0=BE=D0=B9=20=D1=82=D0=B0=D0=B1=D0=BB?= =?UTF-8?q?=D0=B8=D1=86=D1=8B=20=D0=B8=D0=B7-=D0=B7=D0=B0=20=D0=B8=D0=B4?= =?UTF-8?q?=D0=B8=D0=BE=D1=82=D1=81=D0=BA=D0=BE=D0=B9=20=D0=BC=D0=BE=D0=BD?= =?UTF-8?q?=D0=B3=D0=BE.=20=D1=8F=20=D0=BD=D0=B5=D0=BD=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=B6=D1=83=20=D1=8D=D1=82=D1=83=20=D1=81=D1=83=D0=B1=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MongoImplements.cs | 228 +++++++++++++++--- .../FormEmployee_Courses.cs | 2 +- 2 files changed, 189 insertions(+), 41 deletions(-) diff --git a/DepartmentStaffView/DepartmentStaffDatabase/MongoImplements.cs b/DepartmentStaffView/DepartmentStaffDatabase/MongoImplements.cs index 53c2b3c..864ab5e 100644 --- a/DepartmentStaffView/DepartmentStaffDatabase/MongoImplements.cs +++ b/DepartmentStaffView/DepartmentStaffDatabase/MongoImplements.cs @@ -3,6 +3,7 @@ using MongoDB.Driver; using Npgsql; using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; using System.Linq; using System.Runtime.ConstrainedExecution; using System.Text; @@ -19,12 +20,6 @@ namespace DepartmentStaffDatabase private IMongoCollection _contractCollection; private IMongoCollection _courseCollection; private IMongoCollection _employee_CourseCollection; - - private int currentDepartmentId = 0; - private int currentPositionId = 0; - private int currentEmployeeId = 0; - private int currentContractId = 0; - private int currentCourseId = 0; public MongoImplements() { var client = new MongoClient("mongodb://localhost:27017"); @@ -40,8 +35,12 @@ namespace DepartmentStaffDatabase public override void CreateDepartment(Department department) { - currentDepartmentId++; - department.Id = currentDepartmentId; + var maxId = 0; + if (GetDepartments().Count > 0) + { + maxId = _departmentCollection.AsQueryable().Max(dep => dep.Id); + } + department.Id = maxId + 1; _departmentCollection.InsertOne(department); } public override void UpdateDepartment(Department department) @@ -69,41 +68,190 @@ namespace DepartmentStaffDatabase _departmentCollection.DeleteMany(_ => true); } - public override void CreatePosition(Position position) { } - public override void UpdatePosition(Position position) { } - public override void DeletePosition(int id) { } - public override Position GetPosition(int id) { return new Position(); } - public override Position GetPosition(string positionName) { return new Position(); } - public override List GetPositions() { return new List(); } - public override void Deletepositions() { } + //======================================================================= - public override void CreateEmployee(Employee employee) { } - public override void UpdateEmployee(Employee employee) { } - public override void DeleteEmployee(int id) { } - public override Employee GetEmployee(int id) { return new Employee(); } - public override Employee GetEmployee(string employeeName) { return new Employee(); } - public override List GetEmployees() { return new List(); } - public override void DeleteEmployees() { } + public override void CreatePosition(Position position) + { + var maxId = 0; + if (GetPositions().Count > 0) + { + maxId = _positionCollection.AsQueryable().Max(pos => pos.Id); + } + position.Id = maxId + 1; + _positionCollection.InsertOne(position); + } + public override void UpdatePosition(Position position) + { + var filter = Builders.Filter.Eq(pos => pos.Id, position.Id); + var update = Builders.Update + .Set(pos => pos.PositionName, position.PositionName) + .Set(pos => pos.Salary, position.Salary) + .Set(pos => pos.DepartmentId, position.DepartmentId); + _positionCollection.UpdateOne(filter, update); + } + public override void DeletePosition(int id) + { + _positionCollection.DeleteOne(pos => pos.Id == id); + } + public override Position GetPosition(int id) + { + return _positionCollection.Find(pos => pos.Id == id).FirstOrDefault(); + } + public override Position GetPosition(string positionName) + { + return _positionCollection.Find(pos => pos.PositionName == positionName).FirstOrDefault(); + } + public override List GetPositions() + { + return _positionCollection.Find(_ => true).ToList(); + } + public override void Deletepositions() + { + _positionCollection.DeleteMany(_ => true); + } - public override void CreateContract(Contract contract) { } - public override void UpdateContract(Contract contract) { } - public override void DeleteContract(int id) { } - public override Contract GetContract(int id) { return new Contract(); } - public override List GetContracts() { return new List(); } - public override void DeleteContracts() { } - - public override void CreateCourse(Course course) { } - public override void UpdateCourse(Course course) { } - public override void DeleteCourse(int id) { } - public override Course GetCourse(int id) { return new Course(); } - public override List GetCourses() { return new List(); } - public override void DeleteCourses() { } + //======================================================================= + public override void CreateEmployee(Employee employee) + { + var maxId = 0; + if (GetEmployees().Count > 0) + { + maxId = _employeeCollection.AsQueryable().Max(emp => emp.Id); + } + employee.Id = maxId + 1; + _employeeCollection.InsertOne(employee); + } + public override void UpdateEmployee(Employee employee) + { + var filter = Builders.Filter.Eq(emp => emp.Id, employee.Id); + var update = Builders.Update + .Set(emp => emp.EmployeeName, employee.EmployeeName) + .Set(emp => emp.Birthdate, employee.Birthdate) + .Set(emp => emp.Address, employee.Address) + .Set(emp => emp.PhoneNumber, employee.PhoneNumber) + .Set(emp => emp.Email, employee.Email) + .Set(emp => emp.PositionId, employee.PositionId); + _employeeCollection.UpdateOne(filter, update); + } + public override void DeleteEmployee(int id) + { + _employeeCollection.DeleteOne(emp => emp.Id == id); + } + public override Employee GetEmployee(int id) + { + return _employeeCollection.Find(emp => emp.Id == id).FirstOrDefault(); + } + public override Employee GetEmployee(string employeeName) + { + return _employeeCollection.Find(emp => emp.EmployeeName == employeeName).FirstOrDefault(); + } + public override List GetEmployees() + { + return _employeeCollection.Find(_ => true).ToList(); + } + public override void DeleteEmployees() + { + _positionCollection.DeleteMany(_ => true); + } - public override void CreateEmployee_Course(Employee_Course employee_course) { } - public override void DeleteEmployee_Course(int empId, int courseId) { } - public override Employee_Course GetEmployee_Course(int empId, int courseId) { return new Employee_Course(); } - public override List GetEmployee_Courses() { return new List(); } - public override void DeleteEmployee_Courses() { } + //======================================================================= + + public override void CreateContract(Contract contract) + { + var maxId = 0; + if (GetContracts().Count > 0) + { + maxId = _contractCollection.AsQueryable().Max(con => con.Id); + } + contract.Id = maxId + 1; + _contractCollection.InsertOne(contract); + } + public override void UpdateContract(Contract contract) + { + var filter = Builders.Filter.Eq(con => con.Id, contract.Id); + var update = Builders.Update + .Set(con => con.DateOfConclusion, contract.DateOfConclusion) + .Set(con => con.Duration, contract.Duration) + .Set(con => con.EmployeeId, contract.EmployeeId); + _contractCollection.UpdateOne(filter, update); + } + public override void DeleteContract(int id) + { + _contractCollection.DeleteOne(con => con.Id == id); + } + public override Contract GetContract(int id) + { + return _contractCollection.Find(con => con.Id == id).FirstOrDefault(); + } + public override List GetContracts() + { + return _contractCollection.Find(_ => true).ToList(); + } + public override void DeleteContracts() + { + _positionCollection.DeleteMany(_ => true); + } + + //======================================================================= + + public override void CreateCourse(Course course) + { + var maxId = 0; + if (GetCourses().Count > 0) + { + maxId = _courseCollection.AsQueryable().Max(crs => crs.Id); + } + course.Id = maxId + 1; + _courseCollection.InsertOne(course); + } + public override void UpdateCourse(Course course) + { + var filter = Builders.Filter.Eq(crs => crs.Id, course.Id); + var update = Builders.Update + .Set(crs => crs.DateOfEvent, course.DateOfEvent) + .Set(crs => crs.HoursNumber, course.HoursNumber) + .Set(crs => crs.Result, course.Result); + _courseCollection.UpdateOne(filter, update); + } + public override void DeleteCourse(int id) + { + _courseCollection.DeleteOne(crs => crs.Id == id); + } + public override Course GetCourse(int id) + { + return _courseCollection.Find(crs => crs.Id == id).FirstOrDefault(); + } + public override List GetCourses() + { + return _courseCollection.Find(_ => true).ToList(); + } + public override void DeleteCourses() + { + _courseCollection.DeleteMany(_ => true); + } + + //======================================================================= + + public override void CreateEmployee_Course(Employee_Course employee_course) + { + _employee_CourseCollection.InsertOne(employee_course); + } + public override void DeleteEmployee_Course(int empId, int courseId) + { + _employee_CourseCollection.DeleteOne(e => e.EmployeeId == empId && e.CourseId == courseId); + } + public override Employee_Course GetEmployee_Course(int empId, int courseId) + { + return _employee_CourseCollection.Find(e => e.EmployeeId == empId && e.CourseId == courseId).FirstOrDefault(); + } + public override List GetEmployee_Courses() + { + return _employee_CourseCollection.Find(_ => true).ToList(); + } + public override void DeleteEmployee_Courses() + { + _employee_CourseCollection.DeleteMany(_ => true); + } } } diff --git a/DepartmentStaffView/DepartmentStaffView/FormEmployee_Courses.cs b/DepartmentStaffView/DepartmentStaffView/FormEmployee_Courses.cs index d48501b..d95cc36 100644 --- a/DepartmentStaffView/DepartmentStaffView/FormEmployee_Courses.cs +++ b/DepartmentStaffView/DepartmentStaffView/FormEmployee_Courses.cs @@ -1,4 +1,5 @@ using DepartmentStaffDatabase; +using MongoDB.Bson; using System; using System.Collections.Generic; using System.ComponentModel; @@ -24,7 +25,6 @@ namespace DepartmentStaffView try { var list = db.GetEmployee_Courses(); - dataGridView1.DataSource = list; } catch (Exception ex) {