62 lines
1.4 KiB
C#
62 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BathHouseManagmet.Models.Interfaces;
|
|
using MySqlConnector;
|
|
|
|
namespace BathHouseManagmet.Models.Database;
|
|
|
|
public abstract class BaseRepository<T> : IDisposable, IRepository<T> where T : class
|
|
{
|
|
public abstract List<T> GetPage(int pageNumber, int pageSize);
|
|
public abstract bool Add(T entity);
|
|
public abstract bool Update(T entity);
|
|
public abstract bool Delete(T entity);
|
|
|
|
protected MySqlConnection connection;
|
|
|
|
public BaseRepository()
|
|
{
|
|
string connectionString = "server=localhost;user=root;password=root;database=bathhouse";
|
|
connection = new MySqlConnection(connectionString);
|
|
}
|
|
|
|
public bool OpenConnection()
|
|
{
|
|
try
|
|
{
|
|
connection.Open();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool CloseConnection()
|
|
{
|
|
try
|
|
{
|
|
connection.Close();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public bool ExecuteNonQuery(string query)
|
|
{
|
|
using var cmd = new MySqlCommand(query, connection);
|
|
return cmd.ExecuteNonQuery() > 0;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
connection.Dispose();
|
|
}
|
|
}
|