diff --git a/.idea/.idea.BathHouseManagmet/.idea/avalonia.xml b/.idea/.idea.BathHouseManagmet/.idea/avalonia.xml index 39eabcc..91c27ee 100644 --- a/.idea/.idea.BathHouseManagmet/.idea/avalonia.xml +++ b/.idea/.idea.BathHouseManagmet/.idea/avalonia.xml @@ -3,7 +3,9 @@ diff --git a/App.axaml b/App.axaml index e768f3f..09d64d7 100644 --- a/App.axaml +++ b/App.axaml @@ -11,5 +11,6 @@ + \ No newline at end of file diff --git a/Database/DatabaseConnection.cs b/Database/DatabaseConnection.cs deleted file mode 100644 index a029d0a..0000000 --- a/Database/DatabaseConnection.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace BathHouseManagmet.Database; - -public class DatabaseConnection -{ - public string ConnectionString { get; set; } -} \ No newline at end of file diff --git a/Database/OrderRepository.cs b/Database/OrderRepository.cs deleted file mode 100644 index 367f784..0000000 --- a/Database/OrderRepository.cs +++ /dev/null @@ -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 connect) - { - connection = new MySqlConnection(connect.Value.ConnectionString); - } - - public List GetOrders() - { - List 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; - } -} \ No newline at end of file diff --git a/Models/Client.cs b/Models/Classes/Client.cs similarity index 100% rename from Models/Client.cs rename to Models/Classes/Client.cs diff --git a/Models/Discount.cs b/Models/Classes/Discount.cs similarity index 100% rename from Models/Discount.cs rename to Models/Classes/Discount.cs diff --git a/Models/Employee.cs b/Models/Classes/Employee.cs similarity index 100% rename from Models/Employee.cs rename to Models/Classes/Employee.cs diff --git a/Models/Order.cs b/Models/Classes/Order.cs similarity index 100% rename from Models/Order.cs rename to Models/Classes/Order.cs diff --git a/Models/Position.cs b/Models/Classes/Position.cs similarity index 100% rename from Models/Position.cs rename to Models/Classes/Position.cs diff --git a/Models/Service.cs b/Models/Classes/Service.cs similarity index 100% rename from Models/Service.cs rename to Models/Classes/Service.cs diff --git a/Models/Zone.cs b/Models/Classes/Zone.cs similarity index 100% rename from Models/Zone.cs rename to Models/Classes/Zone.cs diff --git a/Models/Database/BaseRepository.cs b/Models/Database/BaseRepository.cs new file mode 100644 index 0000000..4b3b7d1 --- /dev/null +++ b/Models/Database/BaseRepository.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using BathHouseManagmet.Models.Interfaces; +using MySqlConnector; + +namespace BathHouseManagmet.Models.Database; + +public abstract class BaseRepository : IDisposable, IRepository where T : class +{ + public abstract List 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(); + } +} diff --git a/Database/ClientRepository.cs b/Models/Database/ClientRepository.cs similarity index 54% rename from Database/ClientRepository.cs rename to Models/Database/ClientRepository.cs index 62c4741..4078735 100644 --- a/Database/ClientRepository.cs +++ b/Models/Database/ClientRepository.cs @@ -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, IDisposable { - MySqlConnection connection; - - public ClientRepository(IOptions connect) + public ClientRepository() { - connection = new MySqlConnection(connect.Value.ConnectionString); + OpenConnection(); } - - public List GetClients() + + public override List GetPage(int pageNumber, int pageSize) { List 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(); + } } \ No newline at end of file diff --git a/Database/DiscountRepository.cs b/Models/Database/DiscountRepository.cs similarity index 59% rename from Database/DiscountRepository.cs rename to Models/Database/DiscountRepository.cs index 7cc9cd1..fa4c0c6 100644 --- a/Database/DiscountRepository.cs +++ b/Models/Database/DiscountRepository.cs @@ -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, IDisposable { - MySqlConnection connection; - - public DiscountRepository(IOptions connect) + public DiscountRepository() { - connection = new MySqlConnection(connect.Value.ConnectionString); + OpenConnection(); } - - public List GetDiscounts() + + public override List GetPage(int pageNumber, int pageSize) { List 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(); + } } \ No newline at end of file diff --git a/Database/EmployeeRepository.cs b/Models/Database/EmployeeRepository.cs similarity index 71% rename from Database/EmployeeRepository.cs rename to Models/Database/EmployeeRepository.cs index f00aeab..2349e4d 100644 --- a/Database/EmployeeRepository.cs +++ b/Models/Database/EmployeeRepository.cs @@ -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, IDisposable { - MySqlConnection connection; - - public EmployeeRepository(IOptions connect) + public EmployeeRepository() { - connection = new MySqlConnection(connect.Value.ConnectionString); + OpenConnection(); } - - public List GetEmployees() + + public override List GetPage(int pageNumber, int pageSize) { List 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(); + } } \ No newline at end of file diff --git a/Models/Database/OrderRepository.cs b/Models/Database/OrderRepository.cs new file mode 100644 index 0000000..e2b532c --- /dev/null +++ b/Models/Database/OrderRepository.cs @@ -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, IDisposable +{ + public OrderRepository() + { + OpenConnection(); + } + + public override List GetPage(int pageNumber, int pageSize) + { + List 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(); + } +} \ No newline at end of file diff --git a/Database/PositionRepository.cs b/Models/Database/PositionRepository.cs similarity index 53% rename from Database/PositionRepository.cs rename to Models/Database/PositionRepository.cs index d365887..e0cb6c3 100644 --- a/Database/PositionRepository.cs +++ b/Models/Database/PositionRepository.cs @@ -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, IDisposable { - MySqlConnection connection; - - public PositionRepository(IOptions connect) + public PositionRepository() { - connection = new MySqlConnection(connect.Value.ConnectionString); + OpenConnection(); } - - public List GetPositions() + + public override List GetPage(int pageNumber, int pageSize) { List 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(); + } } \ No newline at end of file diff --git a/Database/ServiceRepository.cs b/Models/Database/ServiceRepository.cs similarity index 56% rename from Database/ServiceRepository.cs rename to Models/Database/ServiceRepository.cs index 4f9eab6..31128ae 100644 --- a/Database/ServiceRepository.cs +++ b/Models/Database/ServiceRepository.cs @@ -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, IDisposable { - MySqlConnection connection; - - public ServiceRepository(IOptions connect) + public ServiceRepository() { - connection = new MySqlConnection(connect.Value.ConnectionString); + OpenConnection(); } - - public List GetServices() + + public override List GetPage(int pageNumber, int pageSize) { List 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; } -} \ No newline at end of file + + 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(); + } +} diff --git a/Database/ZoneRepository.cs b/Models/Database/ZoneRepository.cs similarity index 52% rename from Database/ZoneRepository.cs rename to Models/Database/ZoneRepository.cs index 2883894..5fb5655 100644 --- a/Database/ZoneRepository.cs +++ b/Models/Database/ZoneRepository.cs @@ -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, IDisposable { - MySqlConnection connection; - - public ZoneRepository(IOptions connect) + public ZoneRepository() { - connection = new MySqlConnection(connect.Value.ConnectionString); + OpenConnection(); } - - public List GetZones() + + public override List GetPage(int pageNumber, int pageSize) { List 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(); + } } \ No newline at end of file diff --git a/Models/Interfaces/IRepository.cs b/Models/Interfaces/IRepository.cs new file mode 100644 index 0000000..a5ddd3d --- /dev/null +++ b/Models/Interfaces/IRepository.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace BathHouseManagmet.Models.Interfaces; + +public interface IRepository where T : class +{ + List GetPage(int pageNumber, int pageSize); + bool Add(T entity); + bool Update(T entity); + bool Delete(T entity); +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 2047a6e..a3a8e24 100644 --- a/Program.cs +++ b/Program.cs @@ -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(c.Configuration.GetSection("DatabaseConnection")); s.AddTransient(); s.AddTransient(); + s.AddTransient(); + s.AddTransient(); + s.AddTransient(); s.AddTransient(); s.AddTransient(); s.AddTransient(); s.AddTransient(); s.AddTransient(); + s.AddTransient(); }).Build(); BuildAvaloniaApp(host.Services) .StartWithClassicDesktopLifetime(args); diff --git a/ViewModels/MainWindowViewModel.cs b/ViewModels/MainWindowViewModel.cs index 1456dcd..c10489e 100644 --- a/ViewModels/MainWindowViewModel.cs +++ b/ViewModels/MainWindowViewModel.cs @@ -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 orders; [ObservableProperty] List employees; @@ -21,7 +24,6 @@ public partial class MainWindowViewModel : ViewModelBase _employeeRepository = employeeRepository; _orderRepository = orderRepository; - Employees = employeeRepository.GetEmployees(); - Orders = orderRepository.GetOrders(); + OrderViewModel = _serviceProvider.GetRequiredService(); } } \ No newline at end of file diff --git a/ViewModels/OrderViewModel.cs b/ViewModels/OrderViewModel.cs new file mode 100644 index 0000000..42d768a --- /dev/null +++ b/ViewModels/OrderViewModel.cs @@ -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 _pageSizes; + [ObservableProperty] private string _pageInfo; + private int currentPage = 0; + private int totalPages; + + [ObservableProperty] List orders; + + public OrderViewModel(IServiceProvider serviceProvider, OrderRepository repository) + { + _serviceProvider = serviceProvider; + _repository = repository; + + PageSizes = new List([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); + } +} \ No newline at end of file diff --git a/Views/MainWindow.axaml b/Views/MainWindow.axaml index 4d7bc17..ba5129e 100644 --- a/Views/MainWindow.axaml +++ b/Views/MainWindow.axaml @@ -13,18 +13,7 @@ - - - - - - - - - - - - + diff --git a/Views/OrderView.axaml b/Views/OrderView.axaml new file mode 100644 index 0000000..a262fd6 --- /dev/null +++ b/Views/OrderView.axaml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + diff --git a/Views/OrderView.axaml.cs b/Views/OrderView.axaml.cs new file mode 100644 index 0000000..b1493fa --- /dev/null +++ b/Views/OrderView.axaml.cs @@ -0,0 +1,13 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace BathHouseManagmet.Views; + +public partial class OrderView : UserControl +{ + public OrderView() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/obj/Debug/net10.0/Avalonia/Resources.Inputs.cache b/obj/Debug/net10.0/Avalonia/Resources.Inputs.cache index 94e7c04..59fb040 100644 --- a/obj/Debug/net10.0/Avalonia/Resources.Inputs.cache +++ b/obj/Debug/net10.0/Avalonia/Resources.Inputs.cache @@ -1 +1 @@ -4ca19c68a0eed00c6c98dc17b5c4df148d4bff7244cb64327babf21594ee2313 +1192b891a192d7b26b551ab7706358bedb3321507bd7d8cf1bfa07c8d52271f9 diff --git a/obj/Debug/net10.0/Avalonia/resources b/obj/Debug/net10.0/Avalonia/resources index 316af9d..3718bf6 100644 Binary files a/obj/Debug/net10.0/Avalonia/resources and b/obj/Debug/net10.0/Avalonia/resources differ diff --git a/obj/Debug/net10.0/BathHouseManagmet.AssemblyInfo.cs b/obj/Debug/net10.0/BathHouseManagmet.AssemblyInfo.cs index a631c4c..fa780be 100644 --- a/obj/Debug/net10.0/BathHouseManagmet.AssemblyInfo.cs +++ b/obj/Debug/net10.0/BathHouseManagmet.AssemblyInfo.cs @@ -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")] diff --git a/obj/Debug/net10.0/BathHouseManagmet.AssemblyInfoInputs.cache b/obj/Debug/net10.0/BathHouseManagmet.AssemblyInfoInputs.cache index dcc5831..4516230 100644 --- a/obj/Debug/net10.0/BathHouseManagmet.AssemblyInfoInputs.cache +++ b/obj/Debug/net10.0/BathHouseManagmet.AssemblyInfoInputs.cache @@ -1 +1 @@ -f47cb2fe8a4221890c3e523f6ef8a05ece1d4ec0d2150086183c9d3e4a5ddd93 +eabbb29cd12761d77743c612010becd5857837b230f933faa13156f790e78108 diff --git a/obj/Debug/net10.0/BathHouseManagmet.GeneratedMSBuildEditorConfig.editorconfig b/obj/Debug/net10.0/BathHouseManagmet.GeneratedMSBuildEditorConfig.editorconfig index 466d04f..68a3705 100644 --- a/obj/Debug/net10.0/BathHouseManagmet.GeneratedMSBuildEditorConfig.editorconfig +++ b/obj/Debug/net10.0/BathHouseManagmet.GeneratedMSBuildEditorConfig.editorconfig @@ -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