Переделал. Начало пагинации положено. Криво, но все таки

master
Святой Владислав Солоп 2026-05-25 10:32:46 +10:00
parent 41512c28ef
commit 924827d2dc
31 changed files with 637 additions and 402 deletions

View File

@ -3,7 +3,9 @@
<component name="AvaloniaProject">
<option name="projectPerEditor">
<map>
<entry key="App.axaml" value="BathHouseManagmet.csproj" />
<entry key="Views/MainWindow.axaml" value="BathHouseManagmet.csproj" />
<entry key="Views/OrderView.axaml" value="BathHouseManagmet.csproj" />
</map>
</option>
</component>

View File

@ -11,5 +11,6 @@
<Application.Styles>
<FluentTheme />
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml"/>
</Application.Styles>
</Application>

View File

@ -1,6 +0,0 @@
namespace BathHouseManagmet.Database;
public class DatabaseConnection
{
public string ConnectionString { get; set; }
}

View File

@ -1,167 +0,0 @@
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 o.id as oid, o.client_id as ocid, o.employee_id as eid, o.discount_id as did, o.service_id as sid, o.zone_id as zid, c.name as cname, c.surname as csurname, o.order_time as otime, e.name as ename, e.surname as esurname, d.name as dname, s.name as sname, z.name as zname from `order` o join client c on o.client_id = c.id join employee e on o.employee_id = e.id join discount d on o.discount_id = d.id join service s on o.service_id = s.id join zone z on o.zone_id = z.id \n";
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"),
ServiceId = reader.GetInt32("service_id"),
EmployeeId = reader.GetInt32("employee_id"),
DiscountId = reader.GetInt32("discount_id"),
FinalPrice = reader.GetInt32("final_price"),
*/
Client = new Client()
{
Id = reader.GetInt32("ocid"),
Name = reader.GetString("cname"),
Surname = reader.GetString("csurname"),
},
Employee = new Employee()
{
Id = reader.GetInt32("eid"),
Name = reader.GetString("ename"),
Surname = reader.GetString("esurname"),
},
Discount = new Discount()
{
Id = reader.GetInt32("did"),
Name = reader.GetString("dname"),
},
Zone = new Zone()
{
Id = reader.GetInt32("zid"),
Name = reader.GetString("zname"),
},
Service = new Service()
{
Id = reader.GetInt32("sid"),
Name = reader.GetString("sname"),
},
FinalPrice = reader.GetInt32("final_price"),
OrderDate = reader.GetDateTime("order_date"),
Id = reader.GetInt32("oid"),
});
}
}
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`, `service_id`, `employee_id`, `discount_id`, `final_price`) values (0, @client_id, @client_id, @zone_id, @order_date, @service_id, @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("@service_id", order.ServiceId);
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, service_id = @service_id, 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("@service_id", order.ServiceId);
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;
}
}

View File

@ -0,0 +1,61 @@
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();
}
}

View File

@ -1,41 +1,38 @@
using System;
using System.Collections.Generic;
using BathHouseManagmet.Models;
using BathHouseManagmet.Models.Database;
using Microsoft.Extensions.Options;
using MySqlConnector;
namespace BathHouseManagmet.Database;
public class ClientRepository
public class ClientRepository : BaseRepository<Client>, IDisposable
{
MySqlConnection connection;
public ClientRepository(IOptions<DatabaseConnection> connect)
public ClientRepository()
{
connection = new MySqlConnection(connect.Value.ConnectionString);
OpenConnection();
}
public List<Client> GetClients()
public override List<Client> GetPage(int pageNumber, int pageSize)
{
List<Client> result = new();
string sql = "select * from `client`";
string sql = "select * from `client` limit @limit offset @offset";
try
{
connection.Open();
using(var command = new MySqlCommand(sql, connection))
using (var reader = command.ExecuteReader())
using var command = new MySqlCommand(sql, connection);
command.Parameters.AddWithValue("@limit", pageNumber);
command.Parameters.AddWithValue("@offset", pageNumber * pageSize);
using var reader = command.ExecuteReader();
while (reader.Read())
{
while (reader.Read())
result.Add(new Client
{
result.Add(new Client
{
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
Surname = reader.GetString("surname"),
});
}
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
Surname = reader.GetString("surname"),
});
}
connection.Close();
}
catch (Exception e)
{
@ -44,12 +41,11 @@ public class ClientRepository
return result;
}
public bool InsertClient(Client client)
public override bool Add(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);
@ -59,42 +55,37 @@ public class ClientRepository
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool UpdateClient(Client client)
public override bool Update(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();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool DeleteClient(Client client)
public override bool Delete(Client client)
{
string sql = "delete from `client` where `id` = " + client.Id;
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.ExecuteNonQuery();
@ -102,10 +93,30 @@ public class ClientRepository
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public int GetRowsCount()
{
string sql = "select count(id) from `client`";
try
{
using var command = new MySqlCommand(sql, connection);
return Convert.ToInt32(command.ExecuteScalar());
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}
}
public void Dispose()
{
CloseConnection();
base.Dispose();
}
}

View File

@ -1,42 +1,38 @@
using System;
using System.Collections.Generic;
using BathHouseManagmet.Models;
using BathHouseManagmet.Models.Database;
using Microsoft.Extensions.Options;
using Microsoft.VisualBasic;
using MySqlConnector;
namespace BathHouseManagmet.Database;
public class DiscountRepository
public class DiscountRepository : BaseRepository<Discount>, IDisposable
{
MySqlConnection connection;
public DiscountRepository(IOptions<DatabaseConnection> connect)
public DiscountRepository()
{
connection = new MySqlConnection(connect.Value.ConnectionString);
OpenConnection();
}
public List<Discount> GetDiscounts()
public override List<Discount> GetPage(int pageNumber, int pageSize)
{
List<Discount> result = new();
string sql = "select * from `discount`";
string sql = "select * from `discount` limit @limit offset @offset";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
using (var reader = command.ExecuteReader())
using var command = new MySqlCommand(sql, connection);
using var reader = command.ExecuteReader();
while (reader.Read())
{
while (reader.Read())
result.Add(new Discount
{
result.Add(new Discount
{
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
Description = reader.GetString("description"),
DiscountPercent = reader.GetInt32("discount_percent")
});
}
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
Description = reader.GetString("description"),
DiscountPercent = reader.GetInt32("discount_percent")
});
}
connection.Close();
}
catch (Exception e)
{
@ -45,13 +41,12 @@ public class DiscountRepository
return result;
}
public bool InsertDiscount(Discount discount)
public override bool Add(Discount discount)
{
string sql =
"insert into `discount` (`id`, `name`, `description`, `discount_percent`) values (0, @name, @description, @discount_percent)";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@id", discount.Id);
@ -59,24 +54,21 @@ public class DiscountRepository
command.Parameters.AddWithValue("@description", discount.Description);
command.Parameters.AddWithValue("@discount_percent", discount.DiscountPercent);
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return false;
}
public bool UpdateDiscount(Discount discount)
public override bool Update(Discount discount)
{
string sql =
"update `discount` set name = @name, description = @description, discount_percent = @discount_percent";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@id", discount.Id);
@ -84,35 +76,51 @@ public class DiscountRepository
command.Parameters.AddWithValue("@description", discount.Description);
command.Parameters.AddWithValue("@discount_percent", discount.DiscountPercent);
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return false;
}
public bool DeleteDiscount(Discount discount)
public override bool Delete(Discount discount)
{
string sql = "delete from `discount` where `id` = " + discount.Id;
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public int GetRowsCount()
{
string sql = "select count(id) from `discount`";
try
{
using var command = new MySqlCommand(sql, connection);
return Convert.ToInt32(command.ExecuteScalar());
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}
}
public void Dispose()
{
CloseConnection();
base.Dispose();
}
}

View File

@ -1,27 +1,25 @@
using System;
using System.Collections.Generic;
using BathHouseManagmet.Models;
using BathHouseManagmet.Models.Database;
using Microsoft.Extensions.Options;
using MySqlConnector;
namespace BathHouseManagmet.Database;
public class EmployeeRepository
public class EmployeeRepository : BaseRepository<Employee>, IDisposable
{
MySqlConnection connection;
public EmployeeRepository(IOptions<DatabaseConnection> connect)
public EmployeeRepository()
{
connection = new MySqlConnection(connect.Value.ConnectionString);
OpenConnection();
}
public List<Employee> GetEmployees()
public override List<Employee> GetPage(int pageNumber, int pageSize)
{
List<Employee> result = new();
string sql = "select e.id as eid, e.position_id as pid, e.name as ename, e.surname as esurname, p.name as pname from employee e join position p on e.position_id = p.id";
string sql = "select e.id as eid, e.position_id as pid, e.name as ename, e.surname as esurname, p.name as pname from employee e join position p on e.position_id = p.id limit @limit offset @offset";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
using (var reader = command.ExecuteReader())
{
@ -29,12 +27,6 @@ public class EmployeeRepository
{
result.Add(new Employee
{
/*
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
Surname = reader.GetString("surname"),
PositionId = reader.GetInt32("position_id"),
*/
Id = reader.GetInt32("eid"),
Name = reader.GetString("ename"),
Surname = reader.GetString("esurname"),
@ -46,7 +38,6 @@ public class EmployeeRepository
});
}
}
connection.Close();
}
catch (Exception e)
{
@ -55,13 +46,12 @@ public class EmployeeRepository
return result;
}
public bool InsertEmployee(Employee employee)
public override bool Add(Employee employee)
{
string sql =
"insert into `employee` (`id`, `name`, `surname`, `on_work`, `position_id`) values (0, @name, @surname, @on_work, @position_id)";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@name", employee.Name);
@ -69,23 +59,20 @@ public class EmployeeRepository
command.Parameters.AddWithValue("@position_id", employee.PositionId);
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool UpdateEmployee(Employee employee)
public override bool Update(Employee employee)
{
string sql = "update `employee` set name = @name, surname = @surname, on_work = @on_work, position_id = @position_id";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@name", employee.Name);
@ -93,35 +80,51 @@ public class EmployeeRepository
command.Parameters.AddWithValue("@position_id", employee.PositionId);
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool DeleteEmployee(Employee employee)
public override bool Delete(Employee employee)
{
string sql = "delete from `employee` where `id` = " + employee.Id;
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public int GetRowsCount()
{
string sql = "select count(id) from `employee`";
try
{
using var command = new MySqlCommand(sql, connection);
return Convert.ToInt32(command.ExecuteScalar());
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}
}
public void Dispose()
{
CloseConnection();
base.Dispose();
}
}

View File

@ -0,0 +1,174 @@
using System;
using System.Collections.Generic;
using BathHouseManagmet.Models;
using BathHouseManagmet.Models.Database;
using BathHouseManagmet.Models.Interfaces;
using Microsoft.Extensions.Options;
using MySqlConnector;
namespace BathHouseManagmet.Database;
public class OrderRepository : BaseRepository<Order>, IDisposable
{
public OrderRepository()
{
OpenConnection();
}
public override List<Order> GetPage(int pageNumber, int pageSize)
{
List<Order> result = new();
string sql =
"select o.id as oid, o.client_id as ocid, o.employee_id as eid, o.discount_id as did, o.service_id as sid, o.zone_id as zid, c.name as cname, c.surname as csurname, o.order_time as otime, e.name as ename, e.surname as esurname, d.name as dname, s.name as sname, z.name as zname from `order` o join client c on o.client_id = c.id join employee e on o.employee_id = e.id join discount d on o.discount_id = d.id join service s on o.service_id = s.id join zone z on o.zone_id = z.id limit @limit offset @offset";
try
{
using var command = new MySqlCommand(sql, connection);
command.Parameters.AddWithValue("@limit", pageNumber);
command.Parameters.AddWithValue("@offset", pageNumber * pageSize);
using var reader = command.ExecuteReader();
while (reader.Read())
{
result.Add(new Order
{
Client = new Client()
{
Id = reader.GetInt32("ocid"),
Name = reader.GetString("cname"),
Surname = reader.GetString("csurname"),
},
Employee = new Employee()
{
Id = reader.GetInt32("eid"),
Name = reader.GetString("ename"),
Surname = reader.GetString("esurname"),
},
Discount = new Discount()
{
Id = reader.GetInt32("did"),
Name = reader.GetString("dname"),
},
Zone = new Zone()
{
Id = reader.GetInt32("zid"),
Name = reader.GetString("zname"),
},
Service = new Service()
{
Id = reader.GetInt32("sid"),
Name = reader.GetString("sname"),
},
FinalPrice = reader.GetInt32("final_price"),
OrderDate = reader.GetDateTime("order_date"),
Id = reader.GetInt32("oid"),
});
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return result;
}
public override bool Add(Order order)
{
string sql =
"insert into `order` (`id`, `client_id`, `zone_id`, `order_date`, `service_id`, `employee_id`, `discount_id`, `final_price`) values (0, @client_id, @client_id, @zone_id, @order_date, @service_id, @employee_id, @discount_id, @final_price)";
try
{
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("@service_id", order.ServiceId);
command.Parameters.AddWithValue("@employee_id", order.EmployeeId);
command.Parameters.AddWithValue("@discount_id", order.DiscountId);
command.Parameters.AddWithValue("@final_price", order.FinalPrice);
command.ExecuteNonQuery();
}
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
return true;
}
public override bool Update(Order order)
{
string sql =
"update `order` set client_id = @client_id, zone_id = @zone_id, order_date = @order_date, service_id = @service_id, 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("@service_id", order.ServiceId);
command.Parameters.AddWithValue("@employee_id", order.EmployeeId);
command.Parameters.AddWithValue("@discount_id", order.DiscountId);
command.Parameters.AddWithValue("@final_price", order.FinalPrice);
command.ExecuteNonQuery();
}
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
return true;
}
public override bool Delete(Order order)
{
string sql = "delete from `order` where `id` = " + order.Id;
try
{
using (var command = new MySqlCommand(sql, connection))
{
command.ExecuteNonQuery();
}
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
return true;
}
public int GetRowsCount()
{
string sql = "select count(id) from `order`";
try
{
using var mc = new MySqlCommand(sql, connection);
return Convert.ToInt32(mc.ExecuteScalar());
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}
}
public void Dispose()
{
CloseConnection();
base.Dispose();
}
}

View File

@ -1,40 +1,37 @@
using System;
using System.Collections.Generic;
using BathHouseManagmet.Models;
using BathHouseManagmet.Models.Database;
using Microsoft.Extensions.Options;
using MySqlConnector;
namespace BathHouseManagmet.Database;
public class PositionRepository
public class PositionRepository : BaseRepository<Position>, IDisposable
{
MySqlConnection connection;
public PositionRepository(IOptions<DatabaseConnection> connect)
public PositionRepository()
{
connection = new MySqlConnection(connect.Value.ConnectionString);
OpenConnection();
}
public List<Position> GetPositions()
public override List<Position> GetPage(int pageNumber, int pageSize)
{
List<Position> result = new();
string sql = "select * from `position`";
string sql = "select * from `position` limit @limit offset @offset";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
using (var reader = command.ExecuteReader())
using var command = new MySqlCommand(sql, connection);
command.Parameters.AddWithValue("@limit", pageNumber);
command.Parameters.AddWithValue("@offset", pageNumber * pageSize);
using var reader = command.ExecuteReader();
while (reader.Read())
{
while (reader.Read())
result.Add(new Position
{
result.Add(new Position
{
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
});
}
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
});
}
connection.Close();
}
catch (Exception e)
{
@ -43,69 +40,81 @@ public class PositionRepository
return result;
}
public bool InsertPositon(Position position)
public override bool Add(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();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool UpdatePositon(Position position)
public override bool Update(Position position)
{
string sql = "update `position` set name = @name";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@name", position.Name);
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool DeletePositon(Position position)
public override bool Delete(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();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public int GetRowsCount()
{
string sql = "select count(id) from `position`";
try
{
using var command = new MySqlCommand(sql, connection);
return Convert.ToInt32(command.ExecuteScalar());
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}
}
public void Dispose()
{
CloseConnection();
base.Dispose();
}
}

View File

@ -1,42 +1,39 @@
using System;
using System.Collections.Generic;
using BathHouseManagmet.Models;
using BathHouseManagmet.Models.Database;
using Microsoft.Extensions.Options;
using MySqlConnector;
namespace BathHouseManagmet.Database;
public class ServiceRepository
public class ServiceRepository : BaseRepository<Service>, IDisposable
{
MySqlConnection connection;
public ServiceRepository(IOptions<DatabaseConnection> connect)
public ServiceRepository()
{
connection = new MySqlConnection(connect.Value.ConnectionString);
OpenConnection();
}
public List<Service> GetServices()
public override List<Service> GetPage(int pageNumber, int pageSize)
{
List<Service> result = new();
string sql = "select * from `service`";
string sql = "select * from `service` limit @limit offset @offset";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
using (var reader = command.ExecuteReader())
using var command = new MySqlCommand(sql, connection);
command.Parameters.AddWithValue("@limit", pageNumber);
command.Parameters.AddWithValue("@offset", pageNumber * pageSize);
using var reader = command.ExecuteReader();
while (reader.Read())
{
while (reader.Read())
result.Add(new Service
{
result.Add(new Service
{
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
Description = reader.GetString("description"),
Price = reader.GetInt32("price")
});
}
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
Description = reader.GetString("description"),
Price = reader.GetInt32("price")
});
}
connection.Close();
}
catch (Exception e)
{
@ -45,12 +42,11 @@ public class ServiceRepository
return result;
}
public bool InsertService(Service service)
public override bool Add(Service service)
{
string sql = "insert into `service` (`id`, `name`, `description`, `price`) values (0, @name, @description, @price)";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@id", service.Id);
@ -59,23 +55,20 @@ public class ServiceRepository
command.Parameters.AddWithValue("@price", service.Price);
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool UpdateService(Service service)
public override bool Update(Service service)
{
string sql = "update `service` set name = @name, description = @description, price = @price";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.Parameters.AddWithValue("@name", service.Name);
@ -83,35 +76,51 @@ public class ServiceRepository
command.Parameters.AddWithValue("@price", service.Price);
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool DeleteService(Service service)
public override bool Delete(Service service)
{
string sql = "delete from `service` where `id` =" + service.Id;
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
{
command.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception e)
{
connection.Close();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
}
public int GetRowsCount()
{
string sql = "select count(id) from `service`";
try
{
using var mc = new MySqlCommand(sql, connection);
return Convert.ToInt32(mc.ExecuteScalar());
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}
}
public void Dispose()
{
CloseConnection();
base.Dispose();
}
}

View File

@ -1,40 +1,37 @@
using System;
using System.Collections.Generic;
using BathHouseManagmet.Models;
using BathHouseManagmet.Models.Database;
using Microsoft.Extensions.Options;
using MySqlConnector;
namespace BathHouseManagmet.Database;
public class ZoneRepository
public class ZoneRepository : BaseRepository<Zone>, IDisposable
{
MySqlConnection connection;
public ZoneRepository(IOptions<DatabaseConnection> connect)
public ZoneRepository()
{
connection = new MySqlConnection(connect.Value.ConnectionString);
OpenConnection();
}
public List<Zone> GetZones()
public override List<Zone> GetPage(int pageNumber, int pageSize)
{
List<Zone> result = new();
string sql = "select * from zone";
string sql = "select * from zone limit @limit offset @offset";
try
{
connection.Open();
using (var command = new MySqlCommand(sql, connection))
using (var reader = command.ExecuteReader())
using var command = new MySqlCommand(sql, connection);
command.Parameters.AddWithValue("@limit", pageNumber);
command.Parameters.AddWithValue("@offset", pageNumber * pageSize);
using var reader = command.ExecuteReader();
while (reader.Read())
{
while (reader.Read())
result.Add(new Zone
{
result.Add(new Zone
{
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
});
}
Id = reader.GetInt32("id"),
Name = reader.GetString("name"),
});
}
connection.Close();
}
catch (Exception e)
{
@ -43,68 +40,80 @@ public class ZoneRepository
return result;
}
public bool InsertZone(Zone zone)
public override bool Add(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();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool UpdateZone(Zone zone)
public override bool Update(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();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public bool DeleteZone(Zone zone)
public override bool Delete(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();
Console.WriteLine(e);
return false;
}
connection.Close();
return true;
}
public int GetRowsCount()
{
string sql = "select count(id) from `zone`";
try
{
using var mc = new MySqlCommand(sql, connection);
return Convert.ToInt32(mc.ExecuteScalar());
}
catch (Exception e)
{
Console.WriteLine(e);
return 0;
}
}
public void Dispose()
{
CloseConnection();
base.Dispose();
}
}

View File

@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace BathHouseManagmet.Models.Interfaces;
public interface IRepository<T> where T : class
{
List<T> GetPage(int pageNumber, int pageSize);
bool Add(T entity);
bool Update(T entity);
bool Delete(T entity);
}

View File

@ -17,20 +17,21 @@ sealed class Program
[STAThread]
public static void Main(string[] args)
{
var host = Host.CreateDefaultBuilder().ConfigureAppConfiguration((context, config) =>
var host = Host.CreateDefaultBuilder()
.ConfigureServices((c, s) =>
{
config.SetBasePath(AppContext.BaseDirectory).AddJsonFile("appsettings.json").AddEnvironmentVariables();
}).ConfigureServices((c, s) =>
{
s.Configure<DatabaseConnection>(c.Configuration.GetSection("DatabaseConnection"));
s.AddTransient<MainWindow>();
s.AddTransient<MainWindowViewModel>();
s.AddTransient<OrderViewModel>();
s.AddTransient<OrderView>();
s.AddTransient<DiscountRepository>();
s.AddTransient<ClientRepository>();
s.AddTransient<EmployeeRepository>();
s.AddTransient<OrderRepository>();
s.AddTransient<PositionRepository>();
s.AddTransient<ZoneRepository>();
s.AddTransient<ServiceRepository>();
}).Build();
BuildAvaloniaApp(host.Services)
.StartWithClassicDesktopLifetime(args);

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using BathHouseManagmet.Database;
using BathHouseManagmet.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
namespace BathHouseManagmet.ViewModels;
@ -11,7 +12,9 @@ public partial class MainWindowViewModel : ViewModelBase
private readonly IServiceProvider _serviceProvider;
private readonly EmployeeRepository _employeeRepository;
private readonly OrderRepository _orderRepository;
[ObservableProperty] OrderViewModel _orderViewModel;
[ObservableProperty] List<Order> orders;
[ObservableProperty] List<Employee> employees;
@ -21,7 +24,6 @@ public partial class MainWindowViewModel : ViewModelBase
_employeeRepository = employeeRepository;
_orderRepository = orderRepository;
Employees = employeeRepository.GetEmployees();
Orders = orderRepository.GetOrders();
OrderViewModel = _serviceProvider.GetRequiredService<OrderViewModel>();
}
}

View File

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BathHouseManagmet.Database;
using BathHouseManagmet.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace BathHouseManagmet.ViewModels;
public partial class OrderViewModel : ViewModelBase
{
private readonly IServiceProvider _serviceProvider;
private readonly OrderRepository _repository;
[ObservableProperty] private int _currentPageSize;
[ObservableProperty] private List<int> _pageSizes;
[ObservableProperty] private string _pageInfo;
private int currentPage = 0;
private int totalPages;
[ObservableProperty] List<Order> orders;
public OrderViewModel(IServiceProvider serviceProvider, OrderRepository repository)
{
_serviceProvider = serviceProvider;
_repository = repository;
PageSizes = new List<int>([5,10,20]);
CurrentPageSize = PageSizes.First();
}
partial void OnCurrentPageSizeChanged(int value)
{
CalculatePages();
}
void CalculatePages()
{
using var db = new OrderRepository();
var rowsCount = db.GetRowsCount();
totalPages = (int)Math.Ceiling(((double)rowsCount / CurrentPageSize));
ShowFirstPage();
}
void ShowPage(int pageIndex)
{
currentPage = pageIndex;
using var db = new OrderRepository();
Orders.Clear();
var rows = db.GetPage(pageIndex, CurrentPageSize);
rows.ForEach(o => Orders.Add(o));
PageInfo = $"Страница {currentPage + 1} из {totalPages}";
}
[RelayCommand]
private void ShowFirstPage()
{
ShowPage(0);
}
[RelayCommand]
private void ShowLastPage()
{
ShowPage(totalPages - 1);
}
[RelayCommand]
private void ShowNextPage()
{
if (currentPage < totalPages - 1)
ShowPage(currentPage + 1);
}
[RelayCommand]
private void ShowPreviousPage()
{
if (currentPage > 0)
ShowPage(currentPage - 1);
}
}

View File

@ -13,18 +13,7 @@
<TabItem Header="Основная информация">
<TabControl TabStripPlacement="Top">
<TabItem Header="Заказы">
<DataGrid ItemsSource="{Binding Orders}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Дата заказа" Binding="{Binding OrderDate}"/>
<DataGridTextColumn Header="Имя клиента" Binding="{Binding Client.Name}"/>
<DataGridTextColumn Header="Фамилия клиента" Binding="{Binding Client.Name}"/>
<DataGridTextColumn Header="Название услуги" Binding="{Binding Service.Name}"/>
<DataGridTextColumn Header="Имя сотрудника" Binding="{Binding Employee.Name}"/>
<DataGridTextColumn Header="Фамилия сотрудника" Binding="{Binding Employee.Surname}"/>
<DataGridTextColumn Header="Название зоны" Binding="{Binding Zone.Name}"/>
<DataGridTextColumn Header="Окончательная цена" Binding="{Binding FinalPrice}"/>
</DataGrid.Columns>
</DataGrid>
<ContentControl Content="{Binding OrderViewModel}"/>
</TabItem>
<TabItem Header="Сотрудники">
<DataGrid ItemsSource="{Binding Employees}" AutoGenerateColumns="False">

21
Views/OrderView.axaml Normal file
View File

@ -0,0 +1,21 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:BathHouseManagmet.ViewModels"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="BathHouseManagmet.Views.OrderView"
x:DataType="viewModels:OrderViewModel">
<DataGrid ItemsSource="{Binding Orders}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Дата заказа" Binding="{Binding OrderDate}"/>
<DataGridTextColumn Header="Имя клиента" Binding="{Binding Client.Name}"/>
<DataGridTextColumn Header="Фамилия клиента" Binding="{Binding Client.Name}"/>
<DataGridTextColumn Header="Название услуги" Binding="{Binding Service.Name}"/>
<DataGridTextColumn Header="Имя сотрудника" Binding="{Binding Employee.Name}"/>
<DataGridTextColumn Header="Фамилия сотрудника" Binding="{Binding Employee.Surname}"/>
<DataGridTextColumn Header="Название зоны" Binding="{Binding Zone.Name}"/>
<DataGridTextColumn Header="Окончательная цена" Binding="{Binding FinalPrice}"/>
</DataGrid.Columns>
</DataGrid>
</UserControl>

13
Views/OrderView.axaml.cs Normal file
View File

@ -0,0 +1,13 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace BathHouseManagmet.Views;
public partial class OrderView : UserControl
{
public OrderView()
{
InitializeComponent();
}
}

View File

@ -1 +1 @@
4ca19c68a0eed00c6c98dc17b5c4df148d4bff7244cb64327babf21594ee2313
1192b891a192d7b26b551ab7706358bedb3321507bd7d8cf1bfa07c8d52271f9

Binary file not shown.

View File

@ -13,7 +13,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("BathHouseManagmet")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4e1ab770f1b973c62387290bf8e7c9407bc2cd2a")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+41512c28ef3f00e1a5bf2e8fff2eb768a98ef1bf")]
[assembly: System.Reflection.AssemblyProductAttribute("BathHouseManagmet")]
[assembly: System.Reflection.AssemblyTitleAttribute("BathHouseManagmet")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

View File

@ -1 +1 @@
f47cb2fe8a4221890c3e523f6ef8a05ece1d4ec0d2150086183c9d3e4a5ddd93
eabbb29cd12761d77743c612010becd5857837b230f933faa13156f790e78108

View File

@ -28,3 +28,6 @@ build_metadata.AdditionalFiles.SourceItemGroup = AvaloniaXaml
[C:/Users/Mikhail/RiderProjects/BathHouseManagmet/Views/MainWindow.axaml]
build_metadata.AdditionalFiles.SourceItemGroup = AvaloniaXaml
[C:/Users/Mikhail/RiderProjects/BathHouseManagmet/Views/OrderView.axaml]
build_metadata.AdditionalFiles.SourceItemGroup = AvaloniaXaml