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