130 lines
4.5 KiB
C#
130 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using BathHouseManagmet.Models;
|
|
using Microsoft.Extensions.Options;
|
|
using MySqlConnector;
|
|
|
|
namespace BathHouseManagmet.Database;
|
|
|
|
public class OrderRepository
|
|
{
|
|
MySqlConnection connection;
|
|
|
|
public OrderRepository(IOptions<DatabaseConnection> connect)
|
|
{
|
|
connection = new MySqlConnection(connect.Value.ConnectionString);
|
|
}
|
|
|
|
public List<Order> GetOrders()
|
|
{
|
|
List<Order> result = new();
|
|
string sql = "select * from orders";
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
using (var reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
result.Add(new Order
|
|
{
|
|
Id = reader.GetInt32("id"),
|
|
ClientId = reader.GetInt32("client_id"),
|
|
ZoneId = reader.GetInt32("zone_id"),
|
|
OrderDate = reader.GetDateTime("order_date"),
|
|
WorkPrice = reader.GetInt32("work_price"),
|
|
EmployeeId = reader.GetInt32("employee_id"),
|
|
DiscountId = reader.GetInt32("discount_id"),
|
|
FinalPrice = reader.GetInt32("final_price"),
|
|
});
|
|
}
|
|
}
|
|
connection.Close();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public bool InsertOrder(Order order)
|
|
{
|
|
string sql =
|
|
"insert into `order` (`id`, `client_id`, `zone_id`, `order_date`, `work_price`, `employee_id`, `discount_id`, `final_price`) values (0, @client_id, @client_id, @zone_id, @order_date, @work_price, @employee_id, @discount_id, @final_price)";
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@client_id", order.ClientId);
|
|
command.Parameters.AddWithValue("@zone_id", order.ZoneId);
|
|
command.Parameters.AddWithValue("@order_date", order.OrderDate);
|
|
command.Parameters.AddWithValue("@work_price", order.WorkPrice);
|
|
command.Parameters.AddWithValue("@employee_id", order.EmployeeId);
|
|
command.Parameters.AddWithValue("@discount_id", order.DiscountId);
|
|
command.Parameters.AddWithValue("@final_price", order.FinalPrice);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
connection.Close();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
connection.Close();
|
|
return false;
|
|
}
|
|
connection.Close();
|
|
return true;
|
|
}
|
|
|
|
public bool UpdateOrder(Order order)
|
|
{
|
|
string sql =
|
|
"update `order` set client_id = @client_id, zone_id = @zone_id, order_date = @order_date, work_price = @work_price, employee_id = @employee_id, discount_id = @discount_id, final_price = @final_price";
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (var command = new MySqlCommand(sql, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@client_id", order.ClientId);
|
|
command.Parameters.AddWithValue("@zone_id", order.ZoneId);
|
|
command.Parameters.AddWithValue("@order_date", order.OrderDate);
|
|
command.Parameters.AddWithValue("@work_price", order.WorkPrice);
|
|
command.Parameters.AddWithValue("@employee_id", order.EmployeeId);
|
|
command.Parameters.AddWithValue("@discount_id", order.DiscountId);
|
|
command.Parameters.AddWithValue("@final_price", order.FinalPrice);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
connection.Close();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
connection.Close();
|
|
return false;
|
|
}
|
|
connection.Close();
|
|
return true;
|
|
}
|
|
|
|
public bool DeleteOrder(Order order)
|
|
{
|
|
string sql = "delete from `order` where `id` = " + order.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;
|
|
}
|
|
} |