Inventory/AvaloniaApplication14_Inven.../Models/DataBase/EmployeeRepository.cs

90 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using AvaloniaApplication14_autoTest_190326.Models;
using AvaloniaApplication14_Di_test_1125.Models;
using AvaloniaApplication14_Inventory_300326.Models.Models;
using Microsoft.Extensions.Options;
using MySqlConnector;
namespace AvaloniaApplication14_Inventory_300326.Models.DataBase;
public class EmployeeRepository : BaseRepository<Employee>, IDisposable
{
public EmployeeRepository(IOptions<DatabaseSettings> ConnectionString) : base(ConnectionString)
{
OpenConnection();
}
public override List<Employee>? GetAll()
{
string sql = "SELECT * FROM `Employees`";
List<Employee> result = new List<Employee>();
try
{
using var mc = new MySqlCommand(sql, connection);
using var reader = mc.ExecuteReader();
while (reader.Read())
{
Employee employee = new Employee()
{
Id = reader.GetInt32("Id"),
FullName = reader.GetString("FullName"),
PositionId = reader.GetInt32("PositionId"),
};
result.Add(employee);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return result;
}
public override Employee? GetById(int id)
{
string sql = "SELECT * FROM `Employees` WHERE Id = @Id";
Employee result = null;
try
{
using var mc = new MySqlCommand(sql, connection);
mc.Parameters.AddWithValue("@Id", id);
using var reader = mc.ExecuteReader();
while (reader.Read())
{
result = new Employee()
{
Id = reader.GetInt32("Id"),
FullName = reader.GetString("FullName"),
PositionId = reader.GetInt32("PositionId"),
};
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return result;
}
public override bool Delete(int id)
{
throw new NotImplementedException();
}
public override bool Update(Employee item)
{
throw new NotImplementedException();
}
public override bool Add(Employee item)
{
throw new NotImplementedException();
}
public void Dispose()
{
base.Dispose();
}
}