Compare commits

..

No commits in common. "MongoDB" and "main" have entirely different histories.

9 changed files with 49 additions and 610 deletions

View File

@ -8,7 +8,7 @@ namespace DepartmentStaffDatabase
{ {
public abstract class Abstracts public abstract class Abstracts
{ {
public abstract void CreateDepartment(Department department, bool isChangedBD = false); public abstract void CreateDepartment(Department department);
public abstract void UpdateDepartment(Department department); public abstract void UpdateDepartment(Department department);
public abstract void DeleteDepartment(int id); public abstract void DeleteDepartment(int id);
public abstract Department GetDepartment(int id); public abstract Department GetDepartment(int id);
@ -16,15 +16,15 @@ namespace DepartmentStaffDatabase
public abstract List<Department> GetDepartments(); public abstract List<Department> GetDepartments();
public abstract void DeleteDepartments(); public abstract void DeleteDepartments();
public abstract void CreatePosition(Position position, bool isChangedBD = false); public abstract void CreatePosition(Position position);
public abstract void UpdatePosition(Position position); public abstract void UpdatePosition(Position position);
public abstract void DeletePosition(int id); public abstract void DeletePosition(int id);
public abstract Position GetPosition(int id); public abstract Position GetPosition(int id);
public abstract Position GetPosition(string positionName); public abstract Position GetPosition(string positionName);
public abstract List<Position> GetPositions(); public abstract List<Position> GetPositions();
public abstract void DeletePositions(); public abstract void Deletepositions();
public abstract void CreateEmployee(Employee employee, bool isChangedBD = false); public abstract void CreateEmployee(Employee employee);
public abstract void UpdateEmployee(Employee employee); public abstract void UpdateEmployee(Employee employee);
public abstract void DeleteEmployee(int id); public abstract void DeleteEmployee(int id);
public abstract Employee GetEmployee(int id); public abstract Employee GetEmployee(int id);
@ -32,14 +32,14 @@ namespace DepartmentStaffDatabase
public abstract List<Employee> GetEmployees(); public abstract List<Employee> GetEmployees();
public abstract void DeleteEmployees(); public abstract void DeleteEmployees();
public abstract void CreateContract(Contract contract, bool isChangedBD = false); public abstract void CreateContract(Contract contract);
public abstract void UpdateContract(Contract contract); public abstract void UpdateContract(Contract contract);
public abstract void DeleteContract(int id); public abstract void DeleteContract(int id);
public abstract Contract GetContract(int id); public abstract Contract GetContract(int id);
public abstract List<Contract> GetContracts(); public abstract List<Contract> GetContracts();
public abstract void DeleteContracts(); public abstract void DeleteContracts();
public abstract void CreateCourse(Course course, bool isChangedBD = false); public abstract void CreateCourse(Course course);
public abstract void UpdateCourse(Course course); public abstract void UpdateCourse(Course course);
public abstract void DeleteCourse(int id); public abstract void DeleteCourse(int id);
public abstract Course GetCourse(int id); public abstract Course GetCourse(int id);
@ -47,7 +47,7 @@ namespace DepartmentStaffDatabase
public abstract void DeleteCourses(); public abstract void DeleteCourses();
public abstract void CreateEmployee_Course(Employee_Course employee_course, bool isChangedBD = false); public abstract void CreateEmployee_Course(Employee_Course employee_course);
public abstract void DeleteEmployee_Course(int empId, int courseId); public abstract void DeleteEmployee_Course(int empId, int courseId);
public abstract Employee_Course GetEmployee_Course(int empId, int courseId); public abstract Employee_Course GetEmployee_Course(int empId, int courseId);
public abstract List<Employee_Course> GetEmployee_Courses(); public abstract List<Employee_Course> GetEmployee_Courses();

View File

@ -7,7 +7,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
</ItemGroup> </ItemGroup>

View File

@ -14,28 +14,16 @@ namespace DepartmentStaffDatabase
{ {
return new NpgsqlConnection("Host=192.168.56.103;Username=postgres;Password=postgres;Database=TestLab3"); return new NpgsqlConnection("Host=192.168.56.103;Username=postgres;Password=postgres;Database=TestLab3");
} }
public override void CreateDepartment(Department department, bool isChangedBD = false) public override void CreateDepartment(Department department)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
conn.Open(); conn.Open();
if (!isChangedBD)
{
using var cmd = new NpgsqlCommand using var cmd = new NpgsqlCommand
("INSERT INTO department VALUES (nextval('Department_seq'), @Name, @Head)", conn); ("INSERT INTO department VALUES (nextval('Department_seq'), @Name, @Head)", conn);
cmd.Parameters.AddWithValue("@name", department.DepartmentName); cmd.Parameters.AddWithValue("@name", department.DepartmentName);
cmd.Parameters.AddWithValue("@head", department.Head); cmd.Parameters.AddWithValue("@head", department.Head);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
else
{
using var cmd = new NpgsqlCommand
("INSERT INTO department VALUES (@Id, @Name, @Head)", conn);
cmd.Parameters.AddWithValue("@id", department.Id);
cmd.Parameters.AddWithValue("@name", department.DepartmentName);
cmd.Parameters.AddWithValue("@head", department.Head);
cmd.ExecuteNonQuery();
}
}
public override void UpdateDepartment(Department department) public override void UpdateDepartment(Department department)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
@ -116,12 +104,10 @@ namespace DepartmentStaffDatabase
//======================================================================= //=======================================================================
public override void CreatePosition(Position position, bool isChangedBD = false) public override void CreatePosition(Position position)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
conn.Open(); conn.Open();
if (!isChangedBD)
{
using var cmd = new NpgsqlCommand using var cmd = new NpgsqlCommand
("INSERT INTO position VALUES (nextval('Position_seq'), @Name, @Salary, @DepartmentId)", conn); ("INSERT INTO position VALUES (nextval('Position_seq'), @Name, @Salary, @DepartmentId)", conn);
cmd.Parameters.AddWithValue("@name", position.PositionName); cmd.Parameters.AddWithValue("@name", position.PositionName);
@ -129,17 +115,6 @@ namespace DepartmentStaffDatabase
cmd.Parameters.AddWithValue("@departmentId", position.DepartmentId); cmd.Parameters.AddWithValue("@departmentId", position.DepartmentId);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
else
{
using var cmd = new NpgsqlCommand
("INSERT INTO position VALUES (@Id, @Name, @Salary, @DepartmentId)", conn);
cmd.Parameters.AddWithValue("@id", position.Id);
cmd.Parameters.AddWithValue("@name", position.PositionName);
cmd.Parameters.AddWithValue("@salary", position.Salary);
cmd.Parameters.AddWithValue("@departmentId", position.DepartmentId);
cmd.ExecuteNonQuery();
}
}
public override void UpdatePosition(Position position) public override void UpdatePosition(Position position)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
@ -214,7 +189,7 @@ namespace DepartmentStaffDatabase
} }
return positions; return positions;
} }
public override void DeletePositions() public override void Deletepositions()
{ {
using var conn = GetConnect(); using var conn = GetConnect();
conn.Open(); conn.Open();
@ -224,12 +199,10 @@ namespace DepartmentStaffDatabase
//======================================================================= //=======================================================================
public override void CreateEmployee(Employee employee, bool isChangedBD = false) public override void CreateEmployee(Employee employee)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
conn.Open(); conn.Open();
if (!isChangedBD)
{
using var cmd = new NpgsqlCommand using var cmd = new NpgsqlCommand
("INSERT INTO employee VALUES (nextval('Employee_seq'), @Name, @Birthdate, @Address, @PhoneNumber, @Email, @PositionId)", conn); ("INSERT INTO employee VALUES (nextval('Employee_seq'), @Name, @Birthdate, @Address, @PhoneNumber, @Email, @PositionId)", conn);
cmd.Parameters.AddWithValue("@name", employee.EmployeeName); cmd.Parameters.AddWithValue("@name", employee.EmployeeName);
@ -240,20 +213,6 @@ namespace DepartmentStaffDatabase
cmd.Parameters.AddWithValue("@PositionId", employee.PositionId); cmd.Parameters.AddWithValue("@PositionId", employee.PositionId);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
else
{
using var cmd = new NpgsqlCommand
("INSERT INTO employee VALUES (@Id, @Name, @Birthdate, @Address, @PhoneNumber, @Email, @PositionId)", conn);
cmd.Parameters.AddWithValue("@id", employee.Id);
cmd.Parameters.AddWithValue("@name", employee.EmployeeName);
cmd.Parameters.AddWithValue("@Birthdate", employee.Birthdate);
cmd.Parameters.AddWithValue("@Address", employee.Address);
cmd.Parameters.AddWithValue("@PhoneNumber", employee.PhoneNumber);
cmd.Parameters.AddWithValue("@Email", employee.Email);
cmd.Parameters.AddWithValue("@PositionId", employee.PositionId);
cmd.ExecuteNonQuery();
}
}
public override void UpdateEmployee(Employee employee) public override void UpdateEmployee(Employee employee)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
@ -351,12 +310,10 @@ namespace DepartmentStaffDatabase
//======================================================================= //=======================================================================
public override void CreateContract(Contract contract, bool isChangedBD = false) public override void CreateContract(Contract contract)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
conn.Open(); conn.Open();
if (!isChangedBD)
{
using var cmd = new NpgsqlCommand using var cmd = new NpgsqlCommand
("INSERT INTO contract VALUES (nextval('Contract_seq'), @DateOfConclusin, @Duration, @EmployeeId)", conn); ("INSERT INTO contract VALUES (nextval('Contract_seq'), @DateOfConclusin, @Duration, @EmployeeId)", conn);
cmd.Parameters.AddWithValue("@dateOfConclusin", contract.DateOfConclusion); cmd.Parameters.AddWithValue("@dateOfConclusin", contract.DateOfConclusion);
@ -364,17 +321,6 @@ namespace DepartmentStaffDatabase
cmd.Parameters.AddWithValue("@employeeId", contract.EmployeeId); cmd.Parameters.AddWithValue("@employeeId", contract.EmployeeId);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
else
{
using var cmd = new NpgsqlCommand
("INSERT INTO contract VALUES (@Id, @DateOfConclusin, @Duration, @EmployeeId)", conn);
cmd.Parameters.AddWithValue("@id", contract.Id);
cmd.Parameters.AddWithValue("@dateOfConclusin", contract.DateOfConclusion);
cmd.Parameters.AddWithValue("@duration", contract.Duration);
cmd.Parameters.AddWithValue("@employeeId", contract.EmployeeId);
cmd.ExecuteNonQuery();
}
}
public override void UpdateContract(Contract contract) public override void UpdateContract(Contract contract)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
@ -441,12 +387,10 @@ namespace DepartmentStaffDatabase
//======================================================================= //=======================================================================
public override void CreateCourse(Course course, bool isChangedBD = false) public override void CreateCourse(Course course)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
conn.Open(); conn.Open();
if (!isChangedBD)
{
using var cmd = new NpgsqlCommand using var cmd = new NpgsqlCommand
("INSERT INTO course VALUES (nextval('Course_seq'), @EventDateb, @HoursNumber, @Result)", conn); ("INSERT INTO course VALUES (nextval('Course_seq'), @EventDateb, @HoursNumber, @Result)", conn);
cmd.Parameters.AddWithValue("@EventDateb", course.DateOfEvent); cmd.Parameters.AddWithValue("@EventDateb", course.DateOfEvent);
@ -454,17 +398,6 @@ namespace DepartmentStaffDatabase
cmd.Parameters.AddWithValue("@Result", course.Result); cmd.Parameters.AddWithValue("@Result", course.Result);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
else
{
using var cmd = new NpgsqlCommand
("INSERT INTO course VALUES (@Id, @EventDateb, @HoursNumber, @Result)", conn);
cmd.Parameters.AddWithValue("@Id", course.Id);
cmd.Parameters.AddWithValue("@EventDateb", course.DateOfEvent);
cmd.Parameters.AddWithValue("@HoursNumber", course.HoursNumber);
cmd.Parameters.AddWithValue("@Result", course.Result);
cmd.ExecuteNonQuery();
}
}
public override void UpdateCourse(Course course) public override void UpdateCourse(Course course)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
@ -531,7 +464,7 @@ namespace DepartmentStaffDatabase
//======================================================================= //=======================================================================
public override void CreateEmployee_Course(Employee_Course employee_course, bool isChangedBD = false) public override void CreateEmployee_Course(Employee_Course employee_course)
{ {
using var conn = GetConnect(); using var conn = GetConnect();
conn.Open(); conn.Open();

View File

@ -1,6 +1,4 @@
using MongoDB.Bson.Serialization.Attributes; using System;
using MongoDB.Bson;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -10,7 +8,6 @@ namespace DepartmentStaffDatabase
{ {
public class Department public class Department
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public string DepartmentName { get; set; } public string DepartmentName { get; set; }
public string Head { get; set; } public string Head { get; set; }
@ -18,7 +15,6 @@ namespace DepartmentStaffDatabase
public class Position public class Position
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public string PositionName { get; set; } public string PositionName { get; set; }
public double Salary { get; set; } public double Salary { get; set; }
@ -26,7 +22,6 @@ namespace DepartmentStaffDatabase
} }
public class Employee public class Employee
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public string EmployeeName { get; set; } public string EmployeeName { get; set; }
public DateTime Birthdate { get; set; } public DateTime Birthdate { get; set; }
@ -37,7 +32,6 @@ namespace DepartmentStaffDatabase
} }
public class Contract public class Contract
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public DateTime DateOfConclusion { get; set; } public DateTime DateOfConclusion { get; set; }
public int Duration { get; set; } public int Duration { get; set; }
@ -45,7 +39,6 @@ namespace DepartmentStaffDatabase
} }
public class Course public class Course
{ {
[BsonRepresentation(BsonType.Int32)]
public int Id { get; set; } public int Id { get; set; }
public DateTime DateOfEvent { get; set; } public DateTime DateOfEvent { get; set; }
public int HoursNumber { get; set; } public int HoursNumber { get; set; }
@ -53,10 +46,7 @@ namespace DepartmentStaffDatabase
} }
public class Employee_Course public class Employee_Course
{ {
public int? Id { get; set; }
[BsonRepresentation(BsonType.Int32)]
public int EmployeeId { get; set; } public int EmployeeId { get; set; }
[BsonRepresentation(BsonType.Int32)]
public int CourseId { get; set; } public int CourseId { get; set; }
} }
} }

View File

@ -1,296 +0,0 @@
using MongoDB.Bson;
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;
using System.Threading.Tasks;
namespace DepartmentStaffDatabase
{
public class MongoImplements: Abstracts
{
private IMongoDatabase _database;
private IMongoCollection<Department> _departmentCollection;
private IMongoCollection<Position> _positionCollection;
private IMongoCollection<Employee> _employeeCollection;
private IMongoCollection<Contract> _contractCollection;
private IMongoCollection<Course> _courseCollection;
private IMongoCollection<Employee_Course> _employee_CourseCollection;
public MongoImplements()
{
var client = new MongoClient("mongodb://localhost:27017");
_database = client.GetDatabase("StaffDepartment");
_departmentCollection = _database.GetCollection<Department>("departments");
_positionCollection = _database.GetCollection<Position>("positions");
_employeeCollection = _database.GetCollection<Employee>("employees");
_contractCollection = _database.GetCollection<Contract>("contracts");
_courseCollection = _database.GetCollection<Course>("courses");
_employee_CourseCollection = _database.GetCollection<Employee_Course>("employee_courses");
}
public override void CreateDepartment(Department department, bool isChangedBD = false)
{
if (!isChangedBD)
{
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)
{
var filter = Builders<Department>.Filter.Eq(pos => pos.Id, department.Id);
var update = Builders<Department>.Update
.Set(pos => pos.DepartmentName, department.DepartmentName)
.Set(pos => pos.Head, department.Head);
_departmentCollection.UpdateOne(filter, update);
}
public override void DeleteDepartment(int id)
{
_departmentCollection.DeleteOne(dep => dep.Id == id);
}
public override Department GetDepartment(int id)
{
return _departmentCollection.Find(dep => dep.Id == id).FirstOrDefault();
}
public override Department GetDepartment(string departmentName)
{
return _departmentCollection.Find(dep => dep.DepartmentName == departmentName).FirstOrDefault();
}
public override List<Department> GetDepartments()
{
return _departmentCollection.Find(_ => true).ToList();
}
public override void DeleteDepartments()
{
_departmentCollection.DeleteMany(_ => true);
}
//=======================================================================
public override void CreatePosition(Position position, bool isChangedBD = false)
{
if (!isChangedBD)
{
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<Position>.Filter.Eq(pos => pos.Id, position.Id);
var update = Builders<Position>.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<Position> GetPositions()
{
return _positionCollection.Find(_ => true).ToList();
}
public override void DeletePositions()
{
_positionCollection.DeleteMany(_ => true);
}
//=======================================================================
public override void CreateEmployee(Employee employee, bool isChangedBD = false)
{
if (!isChangedBD)
{
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<Employee>.Filter.Eq(emp => emp.Id, employee.Id);
var update = Builders<Employee>.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<Employee> GetEmployees()
{
return _employeeCollection.Find(_ => true).ToList();
}
public override void DeleteEmployees()
{
_employeeCollection.DeleteMany(_ => true);
}
//=======================================================================
public override void CreateContract(Contract contract, bool isChangedBD = false)
{
if (!isChangedBD)
{
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<Contract>.Filter.Eq(con => con.Id, contract.Id);
var update = Builders<Contract>.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<Contract> GetContracts()
{
return _contractCollection.Find(_ => true).ToList();
}
public override void DeleteContracts()
{
_contractCollection.DeleteMany(_ => true);
}
//=======================================================================
public override void CreateCourse(Course course, bool isChangedBD = false)
{
if (!isChangedBD)
{
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<Course>.Filter.Eq(crs => crs.Id, course.Id);
var update = Builders<Course>.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<Course> GetCourses()
{
return _courseCollection.Find(_ => true).ToList();
}
public override void DeleteCourses()
{
_courseCollection.DeleteMany(_ => true);
}
//=======================================================================
public override void CreateEmployee_Course(Employee_Course employee_course, bool isChangedBD = false)
{
var maxId = 0;
if (GetEmployee_Courses().Count > 0)
{
maxId = _employee_CourseCollection.AsQueryable().Max(crs => crs.Id).Value;
}
employee_course.Id = maxId + 1;
_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<Employee_Course> GetEmployee_Courses()
{
var employee_CourseList = _employee_CourseCollection.Find(_ => true).ToList();
var resultList = new List<Employee_Course>();
foreach (var employee_Course in employee_CourseList)
{
resultList.Add(new Employee_Course
{
Id = employee_Course.Id,
EmployeeId = employee_Course.EmployeeId,
CourseId = employee_Course.CourseId
});
}
return resultList;
}
public override void DeleteEmployee_Courses()
{
_employee_CourseCollection.DeleteMany(_ => true);
}
}
}

View File

@ -1,5 +1,4 @@
using DepartmentStaffDatabase; using DepartmentStaffDatabase;
using MongoDB.Bson;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -24,20 +23,8 @@ namespace DepartmentStaffView
{ {
try try
{ {
if (dataGridView1.ColumnCount == 0)
{
dataGridView1.Columns.Add("EmployeeId", "EmployeeId");
dataGridView1.Columns.Add("CourseId", "CourseId");
}
var list = db.GetEmployee_Courses(); var list = db.GetEmployee_Courses();
if (list != null) dataGridView1.DataSource = list;
{
dataGridView1.Rows.Clear();
foreach (var el in list)
{
dataGridView1.Rows.Add(el.EmployeeId, el.CourseId);
}
}
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -43,8 +43,6 @@
buttonEmployeesDelete = new Button(); buttonEmployeesDelete = new Button();
buttonEmployeesUpdate = new Button(); buttonEmployeesUpdate = new Button();
buttonEmployeesCreate = new Button(); buttonEmployeesCreate = new Button();
checkBox1 = new CheckBox();
label1 = new Label();
SuspendLayout(); SuspendLayout();
// //
// buttonDepartments // buttonDepartments
@ -197,34 +195,11 @@
buttonEmployeesCreate.UseVisualStyleBackColor = true; buttonEmployeesCreate.UseVisualStyleBackColor = true;
buttonEmployeesCreate.Click += buttonEmployeesCreate_Click; buttonEmployeesCreate.Click += buttonEmployeesCreate_Click;
// //
// checkBox1
//
checkBox1.AutoSize = true;
checkBox1.Location = new Point(557, 343);
checkBox1.Name = "checkBox1";
checkBox1.Size = new Size(80, 19);
checkBox1.TabIndex = 17;
checkBox1.Text = "MongoDB";
checkBox1.UseVisualStyleBackColor = true;
checkBox1.CheckedChanged += radioButtonMongo_CheckedChanged;
//
// label1
//
label1.AutoSize = true;
label1.Font = new Font("Segoe UI", 12F, FontStyle.Regular, GraphicsUnit.Point);
label1.Location = new Point(349, 339);
label1.Name = "label1";
label1.Size = new Size(202, 21);
label1.TabIndex = 18;
label1.Text = "Тущая СУБД: PostgreSQL";
//
// FormMain // FormMain
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(649, 371); ClientSize = new Size(649, 371);
Controls.Add(label1);
Controls.Add(checkBox1);
Controls.Add(buttonEmployeesDelete); Controls.Add(buttonEmployeesDelete);
Controls.Add(buttonEmployeesUpdate); Controls.Add(buttonEmployeesUpdate);
Controls.Add(buttonEmployeesCreate); Controls.Add(buttonEmployeesCreate);
@ -243,7 +218,6 @@
Name = "FormMain"; Name = "FormMain";
Text = "FormMain"; Text = "FormMain";
ResumeLayout(false); ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
@ -263,7 +237,5 @@
private Button buttonEmployeesDelete; private Button buttonEmployeesDelete;
private Button buttonEmployeesUpdate; private Button buttonEmployeesUpdate;
private Button buttonEmployeesCreate; private Button buttonEmployeesCreate;
private CheckBox checkBox1;
private Label label1;
} }
} }

View File

@ -15,7 +15,7 @@ namespace DepartmentStaffView
{ {
public partial class FormMain : Form public partial class FormMain : Form
{ {
private Abstracts db; private readonly Abstracts db;
public FormMain(Abstracts abstracts) public FormMain(Abstracts abstracts)
{ {
InitializeComponent(); InitializeComponent();
@ -247,124 +247,5 @@ namespace DepartmentStaffView
DateTime end = DateTime.Now; DateTime end = DateTime.Now;
MessageBox.Show("Удалено 200 работников за " + (end - start).Milliseconds.ToString() + " миллисекунд"); MessageBox.Show("Удалено 200 работников за " + (end - start).Milliseconds.ToString() + " миллисекунд");
} }
//перевод из постгреса в монго
private void changeToMongoDB()
{
MongoImplements mongoImplements = new();
Implements postreImplements = new();
//все удалил
mongoImplements.DeleteEmployee_Courses();
mongoImplements.DeleteContracts();
mongoImplements.DeleteCourses();
mongoImplements.DeleteEmployees();
mongoImplements.DeletePositions();
mongoImplements.DeleteDepartments();
//взял данные из постгреса
var listDepartments = postreImplements.GetDepartments();
var listPositions = postreImplements.GetPositions();
var listEmployees = postreImplements.GetEmployees();
var listCourses = postreImplements.GetCourses();
var listContracts = postreImplements.GetContracts();
var listEmployee_Courses = postreImplements.GetEmployee_Courses();
//заношу в монго
foreach (var element in listDepartments)
{
mongoImplements.CreateDepartment(element, true);
}
foreach (var element in listPositions)
{
mongoImplements.CreatePosition(element, true);
}
foreach (var element in listEmployees)
{
mongoImplements.CreateEmployee(element, true);
}
foreach (var element in listCourses)
{
mongoImplements.CreateCourse(element, true);
}
foreach (var element in listContracts)
{
mongoImplements.CreateContract(element, true);
}
foreach (var element in listEmployee_Courses)
{
mongoImplements.CreateEmployee_Course(element, true);
}
}
private void changeToPostgreSQL()
{
MongoImplements mongoImplements = new();
Implements postreImplements = new();
//все удалил
postreImplements.DeleteEmployee_Courses();
postreImplements.DeleteContracts();
postreImplements.DeleteCourses();
postreImplements.DeleteEmployees();
postreImplements.DeletePositions();
postreImplements.DeleteDepartments();
//взял данные из монго
var listDepartments = mongoImplements.GetDepartments();
var listPositions = mongoImplements.GetPositions();
var listEmployees = mongoImplements.GetEmployees();
var listCourses = mongoImplements.GetCourses();
var listContracts = mongoImplements.GetContracts();
var listEmployee_Courses = mongoImplements.GetEmployee_Courses();
//заношу в постгрес
foreach (var element in listDepartments)
{
postreImplements.CreateDepartment(element, true);
}
foreach (var element in listPositions)
{
postreImplements.CreatePosition(element, true);
}
foreach (var element in listEmployees)
{
postreImplements.CreateEmployee(element, true);
}
foreach (var element in listCourses)
{
postreImplements.CreateCourse(element, true);
}
foreach (var element in listContracts)
{
postreImplements.CreateContract(element, true);
}
foreach (var element in listEmployee_Courses)
{
postreImplements.CreateEmployee_Course(element, true);
}
}
private void radioButtonMongo_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Program.ChangeDB(false);
MessageBox.Show("База данных сменилась на MongoDB");
changeToMongoDB();
MessageBox.Show("Данные были перемещены в БД на MongoDB");
label1.Text = "Тущая СУБД: MongoDB";
db = (Abstracts)(Program.ServiceProvider?.GetService(typeof(Abstracts)));
}
else
{
Program.ChangeDB(true);
MessageBox.Show("База данных сменилась на PostgreSQL");
changeToPostgreSQL();
MessageBox.Show("Данные были перемещены в БД на PostgreSQL");
label1.Text = "Тущая СУБД: PostgreSQL";
db = (Abstracts)(Program.ServiceProvider?.GetService(typeof(Abstracts)));
}
}
} }
} }

View File

@ -7,7 +7,6 @@ namespace DepartmentStaffView
{ {
private static ServiceProvider? _serviceProvider; private static ServiceProvider? _serviceProvider;
public static ServiceProvider? ServiceProvider => _serviceProvider; public static ServiceProvider? ServiceProvider => _serviceProvider;
private static bool isPostgreSQL = true;
[STAThread] [STAThread]
static void Main() static void Main()
{ {
@ -18,16 +17,8 @@ namespace DepartmentStaffView
Application.Run(_serviceProvider.GetRequiredService<FormMain>()); Application.Run(_serviceProvider.GetRequiredService<FormMain>());
} }
private static void ConfigureServices(ServiceCollection services) private static void ConfigureServices(ServiceCollection services)
{
if (isPostgreSQL)
{ {
services.AddTransient<Abstracts, Implements>(); services.AddTransient<Abstracts, Implements>();
}
else
{
services.AddTransient<Abstracts, MongoImplements>();
}
services.AddTransient<FormMain>(); services.AddTransient<FormMain>();
services.AddTransient<FormDepartments>(); services.AddTransient<FormDepartments>();
services.AddTransient<FormDepartment>(); services.AddTransient<FormDepartment>();
@ -42,23 +33,5 @@ namespace DepartmentStaffView
services.AddTransient<FormEmployee_Courses>(); services.AddTransient<FormEmployee_Courses>();
services.AddTransient<FormEmployee_Course>(); services.AddTransient<FormEmployee_Course>();
} }
public static void ChangeDB()
{
isPostgreSQL = !isPostgreSQL;
var services = new ServiceCollection();
ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
}
public static void ChangeDB(bool newIsPostrgeSQL)
{
if (newIsPostrgeSQL == isPostgreSQL)
{
return;
}
isPostgreSQL = newIsPostrgeSQL;
var services = new ServiceCollection();
ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
}
} }
} }