308 lines
12 KiB
C#
308 lines
12 KiB
C#
using Npgsql;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
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("@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("@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("@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();
|
|
}
|
|
}
|
|
}
|