using System; using System.Collections.Generic; using BathHouseManagmet.Models; using Microsoft.Extensions.Options; using MySqlConnector; namespace BathHouseManagmet.Database; public class EmployeeRepository { MySqlConnection connection; public EmployeeRepository(IOptions connect) { connection = new MySqlConnection(connect.Value.ConnectionString); } public List GetEmployees(Position position) { List result = new(); string sql = "select * from `employee` where position_id = " + position.Id; try { connection.Open(); using (var command = new MySqlCommand(sql, connection)) using (var reader = command.ExecuteReader()) { while (reader.Read()) { result.Add(new Employee { Id = reader.GetInt32("id"), Name = reader.GetString("name"), Surname = reader.GetString("surname"), OnWork = reader.GetBoolean("on_work"), PositionId = reader.GetInt32("position_id"), }); } } connection.Close(); } catch (Exception e) { Console.WriteLine(e); } return result; } public bool InsertEmployee(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); command.Parameters.AddWithValue("@surname", employee.Surname); command.Parameters.AddWithValue("@on_work", employee.OnWork); command.Parameters.AddWithValue("@position_id", employee.PositionId); command.ExecuteNonQuery(); } connection.Close(); } catch (Exception e) { connection.Close(); return false; } connection.Close(); return true; } public bool UpdateEmployee(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); command.Parameters.AddWithValue("@surname", employee.Surname); command.Parameters.AddWithValue("@on_work", employee.OnWork); command.Parameters.AddWithValue("@position_id", employee.PositionId); command.ExecuteNonQuery(); } connection.Close(); } catch (Exception e) { connection.Close(); return false; } connection.Close(); return true; } public bool DeleteEmployee(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(); return false; } connection.Close(); return true; } }