111 lines
3.0 KiB
C#
111 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BathHouseManagmet.Models;
|
|
using Microsoft.Extensions.Options;
|
|
using MySqlConnector;
|
|
|
|
namespace BathHouseManagmet.Database;
|
|
|
|
public class ClientRepository
|
|
{
|
|
MySqlConnection connection;
|
|
|
|
public ClientRepository(IOptions<DatabaseConnection> connect)
|
|
{
|
|
connection = new MySqlConnection(connect.Value.ConnectionString);
|
|
}
|
|
|
|
public List<Client> GetClients()
|
|
{
|
|
List<Client> result = new();
|
|
string sql = "select * from `client`";
|
|
try
|
|
{
|
|
connection.Open();
|
|
using(var command = new MySqlCommand(sql, connection))
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
result.Add(new Client
|
|
{
|
|
Id = reader.GetInt32("id"),
|
|
Name = reader.GetString("name"),
|
|
Surname = reader.GetString("surname"),
|
|
});
|
|
}
|
|
}
|
|
connection.Close();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public bool InsertClient(Client client)
|
|
{
|
|
string sql = "insert into `client` (`id`, `name`, `surname`) values (0, @name, @surname)";
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", client.Name);
|
|
command.Parameters.AddWithValue("@surname", client.Surname);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
connection.Close();
|
|
return false;
|
|
}
|
|
connection.Close();
|
|
return true;
|
|
}
|
|
|
|
public bool UpdateClient(Client client)
|
|
{
|
|
string sql = "update `client` set name = @name, surname = @surname";
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", client.Name);
|
|
command.Parameters.AddWithValue("@surname", client.Surname);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
connection.Close();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
connection.Close();
|
|
return false;
|
|
}
|
|
connection.Close();
|
|
return true;
|
|
}
|
|
|
|
public bool DeleteClient(Client client)
|
|
{
|
|
string sql = "delete from `client` where `id` = " + client.Id;
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
connection.Close();
|
|
return false;
|
|
}
|
|
connection.Close();
|
|
return true;
|
|
}
|
|
} |