110 lines
2.7 KiB
C#
110 lines
2.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BathHouseManagmet.Models;
|
|
using Microsoft.Extensions.Options;
|
|
using MySqlConnector;
|
|
|
|
namespace BathHouseManagmet.Database;
|
|
|
|
public class ZoneRepository
|
|
{
|
|
MySqlConnection connection;
|
|
|
|
public ZoneRepository(IOptions<DatabaseConnection> connect)
|
|
{
|
|
connection = new MySqlConnection(connect.Value.ConnectionString);
|
|
}
|
|
|
|
public List<Zone> GetZones()
|
|
{
|
|
List<Zone> result = new();
|
|
string sql = "select * from zone";
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
result.Add(new Zone
|
|
{
|
|
Id = reader.GetInt32("id"),
|
|
Name = reader.GetString("name"),
|
|
});
|
|
}
|
|
}
|
|
connection.Close();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public bool InsertZone(Zone zone)
|
|
{
|
|
string sql = "insert into `zone` (`id`, `name`) values (0, @name)";
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", zone.Name);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
connection.Close();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
connection.Close();
|
|
return false;
|
|
}
|
|
connection.Close();
|
|
return true;
|
|
}
|
|
|
|
public bool UpdateZone(Zone zone)
|
|
{
|
|
string sql = "update `zone` set name = @name";
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@name", zone.Name);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
connection.Close();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
connection.Close();
|
|
return false;
|
|
}
|
|
connection.Close();
|
|
return true;
|
|
}
|
|
|
|
public bool DeleteZone(Zone zone)
|
|
{
|
|
string sql = "delete from `zone` where id = " + zone.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;
|
|
}
|
|
} |