117 lines
3.4 KiB
C#
117 lines
3.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)
|
|
{
|
|
string sql = "UPDATE TechInventory.Employees SET FullName=@FullName, PositionId=@PositionId WHERE Id=@Id;";
|
|
try
|
|
{
|
|
using var mc = new MySqlCommand(sql, connection);
|
|
mc.Parameters.AddWithValue("@Id", item.Id);
|
|
mc.Parameters.AddWithValue("@FullName", item.FullName);
|
|
mc.Parameters.AddWithValue("@PositionId", item.PositionId);
|
|
mc.ExecuteNonQuery();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public override bool Add(Employee item)
|
|
{
|
|
string sql = "INSERT INTO TechInventory.Employees (FullName, PositionId) VALUES(@FullName, @PositionId);";
|
|
try
|
|
{
|
|
using var mc = new MySqlCommand(sql, connection);
|
|
mc.Parameters.AddWithValue("@FullName", item.FullName);
|
|
mc.Parameters.AddWithValue("@PositionId", item.PositionId);
|
|
mc.ExecuteNonQuery();
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
base.Dispose();
|
|
}
|
|
} |