119 lines
3.0 KiB
C#
119 lines
3.0 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 ZoneRepository : BaseRepository<Zone>, IDisposable
|
|
{
|
|
public ZoneRepository()
|
|
{
|
|
OpenConnection();
|
|
}
|
|
|
|
public override List<Zone> GetPage(int pageNumber, int pageSize)
|
|
{
|
|
List<Zone> result = new();
|
|
string sql = "select * from zone 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 Zone
|
|
{
|
|
Id = reader.GetInt32("id"),
|
|
Name = reader.GetString("name"),
|
|
});
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public override bool Add(Zone zone)
|
|
{
|
|
string sql = "insert into `zone` (`id`, `name`) values (0, @name)";
|
|
try
|
|
{
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", zone.Name);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override bool Update(Zone zone)
|
|
{
|
|
string sql = "update `zone` set name = @name";
|
|
try
|
|
{
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", zone.Name);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override bool Delete(Zone zone)
|
|
{
|
|
string sql = "delete from `zone` where id = " + zone.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 `zone`";
|
|
try
|
|
{
|
|
using var mc = new MySqlCommand(sql, connection);
|
|
return Convert.ToInt32(mc.ExecuteScalar());
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
CloseConnection();
|
|
base.Dispose();
|
|
}
|
|
} |