130 lines
3.8 KiB
C#
130 lines
3.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BathHouseManagmet.Models;
|
|
using BathHouseManagmet.Models.Database;
|
|
using Microsoft.Extensions.Options;
|
|
using MySqlConnector;
|
|
|
|
namespace BathHouseManagmet.Database;
|
|
|
|
public class EmployeeRepository : BaseRepository<Employee>, IDisposable
|
|
{
|
|
public EmployeeRepository()
|
|
{
|
|
OpenConnection();
|
|
}
|
|
|
|
public override List<Employee> GetPage(int pageNumber, int pageSize)
|
|
{
|
|
List<Employee> result = new();
|
|
string sql = "select e.id as eid, e.position_id as pid, e.name as ename, e.surname as esurname, p.name as pname from employee e join position p on e.position_id = p.id limit @limit offset @offset";
|
|
try
|
|
{
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
result.Add(new Employee
|
|
{
|
|
Id = reader.GetInt32("eid"),
|
|
Name = reader.GetString("ename"),
|
|
Surname = reader.GetString("esurname"),
|
|
Position = new Position()
|
|
{
|
|
Id = reader.GetInt32("pid"),
|
|
Name = reader.GetString("name"),
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public override bool Add(Employee employee)
|
|
{
|
|
string sql =
|
|
"insert into `employee` (`id`, `name`, `surname`, `on_work`, `position_id`) values (0, @name, @surname, @on_work, @position_id)";
|
|
try
|
|
{
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", employee.Name);
|
|
command.Parameters.AddWithValue("@surname", employee.Surname);
|
|
command.Parameters.AddWithValue("@position_id", employee.PositionId);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override bool Update(Employee employee)
|
|
{
|
|
string sql = "update `employee` set name = @name, surname = @surname, on_work = @on_work, position_id = @position_id";
|
|
try
|
|
{
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", employee.Name);
|
|
command.Parameters.AddWithValue("@surname", employee.Surname);
|
|
command.Parameters.AddWithValue("@position_id", employee.PositionId);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override bool Delete(Employee employee)
|
|
{
|
|
string sql = "delete from `employee` where `id` = " + employee.Id;
|
|
try
|
|
{
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public int GetRowsCount()
|
|
{
|
|
string sql = "select count(id) from `employee`";
|
|
try
|
|
{
|
|
using var command = new MySqlCommand(sql, connection);
|
|
return Convert.ToInt32(command.ExecuteScalar());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
CloseConnection();
|
|
base.Dispose();
|
|
}
|
|
} |