120 lines
3.1 KiB
C#
120 lines
3.1 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 PositionRepository : BaseRepository<Position>, IDisposable
|
|
{
|
|
public PositionRepository()
|
|
{
|
|
OpenConnection();
|
|
}
|
|
|
|
public override List<Position> GetPage(int pageNumber, int pageSize)
|
|
{
|
|
List<Position> result = new();
|
|
string sql = "select * from `position` limit @limit offset @offset";
|
|
try
|
|
{
|
|
using var command = new MySqlCommand(sql, connection);
|
|
command.Parameters.AddWithValue("@limit", pageNumber);
|
|
command.Parameters.AddWithValue("@offset", pageNumber * pageSize);
|
|
using var reader = command.ExecuteReader();
|
|
while (reader.Read())
|
|
{
|
|
result.Add(new Position
|
|
{
|
|
Id = reader.GetInt32("id"),
|
|
Name = reader.GetString("name"),
|
|
});
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public override bool Add(Position position)
|
|
{
|
|
string sql = "insert into `position` (`id`, `name`) values (0, @name)";
|
|
try
|
|
{
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@id", position.Id);
|
|
command.Parameters.AddWithValue("@name", position.Name);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override bool Update(Position position)
|
|
{
|
|
string sql = "update `position` set name = @name";
|
|
try
|
|
{
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", position.Name);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override bool Delete(Position position)
|
|
{
|
|
string sql = "delete from `position` where `id` = " + position.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 `position`";
|
|
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();
|
|
}
|
|
} |