528 lines
21 KiB
C#
528 lines
21 KiB
C#
using Npgsql;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.Contracts;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DepartmentStaffDatabase
|
|
{
|
|
public class Implements: Abstracts
|
|
{
|
|
private NpgsqlConnection GetConnect()
|
|
{
|
|
return new NpgsqlConnection("Host=192.168.56.103;Username=postgres;Password=postgres;Database=TestLab3");
|
|
}
|
|
public override void CreateDepartment(Department department)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand
|
|
("INSERT INTO department VALUES (nextval('Department_seq'), @Name, @Head)", conn);
|
|
cmd.Parameters.AddWithValue("@name", department.DepartmentName);
|
|
cmd.Parameters.AddWithValue("@head", department.Head);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override void UpdateDepartment(Department department)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"UPDATE department SET name = @name, head = @head WHERE id = @Id", conn);
|
|
cmd.Parameters.AddWithValue("@Id", department.Id);
|
|
cmd.Parameters.AddWithValue("@name", department.DepartmentName);
|
|
cmd.Parameters.AddWithValue("@head", department.Head);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override void DeleteDepartment(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM department WHERE id = @Id", conn);
|
|
cmd.Parameters.AddWithValue("@Id", id);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override Department GetDepartment(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM department WHERE id = {id}", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Department
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
DepartmentName = reader.GetString(1),
|
|
Head = reader.GetString(2),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public override Department GetDepartment(string departmentName)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM department WHERE name = {departmentName}", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Department
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
DepartmentName = reader.GetString(1),
|
|
Head = reader.GetString(2),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public override List<Department> GetDepartments()
|
|
{
|
|
List<Department> departments = new List<Department>();
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM department", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
departments.Add(new Department
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
DepartmentName = reader.GetString(1),
|
|
Head = reader.GetString(2),
|
|
});
|
|
}
|
|
return departments;
|
|
}
|
|
public override void DeleteDepartments()
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM department", conn);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
//=======================================================================
|
|
|
|
public override void CreatePosition(Position position)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand
|
|
("INSERT INTO position VALUES (nextval('Position_seq'), @Name, @Salary, @DepartmentId)", conn);
|
|
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)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"UPDATE position SET name = @name, salary = @salary, departmentId = @departmentId WHERE id = @Id", 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 DeletePosition(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM position WHERE id = @Id", conn);
|
|
cmd.Parameters.AddWithValue("@Id", id);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override Position GetPosition(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM position WHERE id = {id}", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Position
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
PositionName = reader.GetString(1),
|
|
Salary = reader.GetDouble(2),
|
|
DepartmentId = reader.GetInt32(3),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public override Position GetPosition(string positionName)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM position WHERE id = {positionName}", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Position
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
PositionName = reader.GetString(1),
|
|
Salary = reader.GetDouble(2),
|
|
DepartmentId = reader.GetInt32(3),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public override List<Position> GetPositions()
|
|
{
|
|
List<Position> positions = new List<Position>();
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM position", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
positions.Add(new Position
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
PositionName = reader.GetString(1),
|
|
Salary = reader.GetDouble(2),
|
|
DepartmentId = reader.GetInt32(3),
|
|
});
|
|
}
|
|
return positions;
|
|
}
|
|
public override void Deletepositions()
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM position", conn);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
//=======================================================================
|
|
|
|
public override void CreateEmployee(Employee employee)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand
|
|
("INSERT INTO employee VALUES (nextval('Employee_seq'), @Name, @Birthdate, @Address, @PhoneNumber, @Email, @PositionId)", conn);
|
|
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)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"UPDATE employee SET name = @name, birthdate = @birthdate, " +
|
|
$"address = @address, phoneNumber = @phoneNumber, email = @email, positionId = @positionId WHERE id = @Id", 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 DeleteEmployee(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM employee WHERE id = @Id", conn);
|
|
cmd.Parameters.AddWithValue("@Id", id);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override Employee GetEmployee(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM employee WHERE id = {id}", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Employee
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
EmployeeName = reader.GetString(1),
|
|
Birthdate = reader.GetDateTime(2),
|
|
Address = reader.GetString(3),
|
|
PhoneNumber = reader.GetString(4),
|
|
Email = reader.GetString(5),
|
|
PositionId = reader.GetInt32(6),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public override Employee GetEmployee(string employeeName)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM employee WHERE id = {employeeName}", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Employee
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
EmployeeName = reader.GetString(1),
|
|
Birthdate = reader.GetDateTime(2),
|
|
Address = reader.GetString(3),
|
|
PhoneNumber = reader.GetString(4),
|
|
Email = reader.GetString(5),
|
|
PositionId = reader.GetInt32(6),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public override List<Employee> GetEmployees()
|
|
{
|
|
List<Employee> employees = new List<Employee>();
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM employee", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
employees.Add(new Employee
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
EmployeeName = reader.GetString(1),
|
|
Birthdate = reader.GetDateTime(2),
|
|
Address = reader.GetString(3),
|
|
PhoneNumber = reader.GetString(4),
|
|
Email = reader.GetString(5),
|
|
PositionId = reader.GetInt32(6),
|
|
});
|
|
}
|
|
return employees;
|
|
}
|
|
public override void DeleteEmployees()
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM employee", conn);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
//=======================================================================
|
|
|
|
public override void CreateContract(Contract contract)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand
|
|
("INSERT INTO contract VALUES (nextval('Contract_seq'), @DateOfConclusin, @Duration, @EmployeeId)", conn);
|
|
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)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"UPDATE contract SET dateOfConclusion = @dateOfConclusion, duration = @duration, employeeId = @employeeId WHERE id = @Id", conn);
|
|
cmd.Parameters.AddWithValue("@Id", contract.Id);
|
|
cmd.Parameters.AddWithValue("@dateOfConclusion", contract.DateOfConclusion);
|
|
cmd.Parameters.AddWithValue("@duration", contract.Duration);
|
|
cmd.Parameters.AddWithValue("@employeeId", contract.EmployeeId);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override void DeleteContract(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM contract WHERE id = @Id", conn);
|
|
cmd.Parameters.AddWithValue("@Id", id);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override Contract GetContract(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM contract WHERE id = {id}", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Contract
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
DateOfConclusion = reader.GetDateTime(1),
|
|
Duration = reader.GetInt32(2),
|
|
EmployeeId = reader.GetInt32(3),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public override List<Contract> GetContracts()
|
|
{
|
|
List<Contract> contracts = new List<Contract>();
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM contract", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
contracts.Add(new Contract
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
DateOfConclusion = reader.GetDateTime(1),
|
|
Duration = reader.GetInt32(2),
|
|
EmployeeId = reader.GetInt32(3),
|
|
});
|
|
}
|
|
return contracts;
|
|
}
|
|
public override void DeleteContracts()
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM contract", conn);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
//=======================================================================
|
|
|
|
public override void CreateCourse(Course course)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand
|
|
("INSERT INTO course VALUES (nextval('Course_seq'), @EventDateb, @HoursNumber, @Result)", conn);
|
|
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)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"UPDATE course SET eventDateb = @eventDateb, hoursNumber = @hoursNumber, result = @result WHERE id = @Id", 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 DeleteCourse(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM course WHERE id = @Id", conn);
|
|
cmd.Parameters.AddWithValue("@Id", id);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override Course GetCourse(int id)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM course WHERE id = {id}", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Course
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
DateOfEvent = reader.GetDateTime(1),
|
|
HoursNumber = reader.GetInt32(2),
|
|
Result = reader.GetString(3),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public override List<Course> GetCourses()
|
|
{
|
|
List<Course> contracts = new List<Course>();
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM course", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
contracts.Add(new Course
|
|
{
|
|
Id = reader.GetInt32(0),
|
|
DateOfEvent = reader.GetDateTime(1),
|
|
HoursNumber = reader.GetInt32(2),
|
|
Result = reader.GetString(3),
|
|
});
|
|
}
|
|
return contracts;
|
|
}
|
|
public override void DeleteCourses()
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM course", conn);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
//=======================================================================
|
|
|
|
public override void CreateEmployee_Course(Employee_Course employee_course)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand
|
|
("INSERT INTO employee_course VALUES (@EmployeeId, @CourseId)", conn);
|
|
cmd.Parameters.AddWithValue("@EmployeeId", employee_course.EmployeeId);
|
|
cmd.Parameters.AddWithValue("@CourseId", employee_course.CourseId);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override void DeleteEmployee_Course(int empId, int courseId)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM employee_course WHERE EmployeeId = @EmployeeId AND CourseId = @CourseId", conn);
|
|
cmd.Parameters.AddWithValue("@EmployeeId", empId);
|
|
cmd.Parameters.AddWithValue("@CourseId", courseId);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
public override Employee_Course GetEmployee_Course(int empId, int courseId)
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"SELECT * FROM employee_course WHERE EmployeeId = {empId} AND CourseId = {courseId}", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
if (reader.Read())
|
|
{
|
|
return new Employee_Course
|
|
{
|
|
EmployeeId = reader.GetInt32(0),
|
|
CourseId = reader.GetInt32(1),
|
|
};
|
|
}
|
|
return null;
|
|
}
|
|
public override List<Employee_Course> GetEmployee_Courses()
|
|
{
|
|
List<Employee_Course> employee_course = new List<Employee_Course>();
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM employee_course", conn);
|
|
using var reader = cmd.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
employee_course.Add(new Employee_Course
|
|
{
|
|
EmployeeId = reader.GetInt32(0),
|
|
CourseId = reader.GetInt32(1),
|
|
});
|
|
}
|
|
return employee_course;
|
|
}
|
|
public override void DeleteEmployee_Courses()
|
|
{
|
|
using var conn = GetConnect();
|
|
conn.Open();
|
|
using var cmd = new NpgsqlCommand($"DELETE FROM employee_course", conn);
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|