using System; using System.Collections.Generic; using BathHouseManagmet.Models; using Microsoft.Extensions.Options; using MySqlConnector; namespace BathHouseManagmet.Database; public class PositionRepository { MySqlConnection connection; public PositionRepository(IOptions connect) { connection = new MySqlConnection(connect.Value.ConnectionString); } public List GetPositions() { List result = new(); string sql = "select * from `position`"; try { connection.Open(); using (var command = new MySqlCommand(sql, connection)) using (var reader = command.ExecuteReader()) { while (reader.Read()) { result.Add(new Position { Id = reader.GetInt32("id"), Name = reader.GetString("name"), }); } } connection.Close(); } catch (Exception e) { Console.WriteLine(e); } return result; } public bool InsertPositon(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(); return false; } connection.Close(); return true; } public bool UpdatePositon(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(); return false; } connection.Close(); return true; } public bool DeletePositon(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(); return false; } connection.Close(); return true; } }