diff --git a/Database/ClientRepository.cs b/Database/ClientRepository.cs index d5deeec..62c4741 100644 --- a/Database/ClientRepository.cs +++ b/Database/ClientRepository.cs @@ -44,5 +44,68 @@ public class ClientRepository 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; + } } \ No newline at end of file diff --git a/Database/PositionRepository.cs b/Database/PositionRepository.cs new file mode 100644 index 0000000..7b888bb --- /dev/null +++ b/Database/PositionRepository.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using BathHouseManagmet.Models; +using Microsoft.Extensions.Options; +using MySqlConnector; + +namespace BathHouseManagmet.Database; + +public class PositionRepository +{ + MySqlConnection connection; + + public PositionRepository(IOptions connect) + { + connection = new MySqlConnection(connect.Value.ConnectionString); + } + + public List GetPositions() + { + List result = new(); + string sql = "select * from position"; + try + { + connection.Open(); + using (var command = new MySqlCommand(sql, connection)) + using (var reader = command.ExecuteReader()) + { + while (reader.Read()) + { + result.Add(new Position + { + Id = reader.GetInt32("id"), + Name = reader.GetString("name"), + }); + } + } + connection.Close(); + } + catch (Exception e) + { + Console.WriteLine(e); + } + return result; + } + + public bool InsertPositon(Position position) + { + string sql = "insert into `position` (`id`, `name`) values (0, @name)"; + try + { + connection.Open(); + using (var command = new MySqlCommand(sql, connection)) + { + command.Parameters.AddWithValue("@id", position.Id); + command.Parameters.AddWithValue("@name", position.Name); + command.ExecuteNonQuery(); + } + connection.Close(); + } + catch (Exception e) + { + connection.Close(); + return false; + } + connection.Close(); + return true; + } + + public bool UpdatePositon(Position position) + { + string sql = "insert into `position` (`id`, `name`) values (0, @name)"; + try + { + connection.Open(); + using (var command = new MySqlCommand(sql, connection)) + { + command.Parameters.AddWithValue("@id", position.Id); + command.Parameters.AddWithValue("@name", position.Name); + command.ExecuteNonQuery(); + } + connection.Close(); + } + catch (Exception e) + { + connection.Close(); + return false; + } + connection.Close(); + return true; + } + + public bool DeletePositon(Position position) + { + string sql = "delete from `position` where `id` = " + position.Id; + try + { + connection.Open(); + using (var command = new MySqlCommand(sql, connection)) + { + command.ExecuteNonQuery(); + } + connection.Close(); + } + catch (Exception e) + { + connection.Close(); + return false; + } + connection.Close(); + return true; + } +} \ No newline at end of file