126 lines
3.4 KiB
C#
126 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 PositionRepository : BaseRepository<Position>, IDisposable
|
|
{
|
|
public PositionRepository(IOptions<DatabaseSettings> ConnectionString) : base(ConnectionString)
|
|
{
|
|
OpenConnection();
|
|
}
|
|
|
|
public override List<Position>? GetAll()
|
|
{
|
|
List<Position> result = new List<Position>();
|
|
string sql = "SELECT * FROM Positions";
|
|
using (var mc = new MySqlCommand(sql, connection))
|
|
{
|
|
using (var reader = mc.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
Position position = new Position()
|
|
{
|
|
Id = reader.GetInt32("Id"),
|
|
Name = reader.GetString("Name"),
|
|
};
|
|
result.Add(position);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public override Position? GetById(int id)
|
|
{
|
|
string sql = "SELECT * FROM Positions WHERE Id = @Id";
|
|
Position result = null;
|
|
using (var mc = new MySqlCommand(sql, connection))
|
|
{
|
|
mc.Parameters.AddWithValue("@Id", id);
|
|
using (var reader = mc.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
result = new Position()
|
|
{
|
|
Id = reader.GetInt32("Id"),
|
|
Name = reader.GetString("Name")
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public override bool Delete(int id)
|
|
{
|
|
string sql = "DELETE FROM Positions WHERE Id = @Id";
|
|
try
|
|
{
|
|
using (var mc = new MySqlCommand(sql, connection))
|
|
{
|
|
mc.Parameters.AddWithValue("@Id", id);
|
|
mc.ExecuteNonQuery();
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public override bool Update(Position item)
|
|
{
|
|
string sql = "UPDATE Positions SET Name = @Name WHERE Id = @Id";
|
|
try
|
|
{
|
|
using (var mc = new MySqlCommand(sql, connection))
|
|
{
|
|
mc.Parameters.AddWithValue("@Name", item.Name);
|
|
mc.Parameters.AddWithValue("@Id", item.Id);
|
|
mc.ExecuteNonQuery();
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public override bool Add(Position item)
|
|
{
|
|
string sql = "INSERT INTO Positions (Name) VALUES (@Name)";
|
|
try
|
|
{
|
|
using (var mc = new MySqlCommand(sql, connection))
|
|
{
|
|
mc.Parameters.AddWithValue("@Name", item.Name);
|
|
mc.ExecuteNonQuery();
|
|
}
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
base.Dispose();
|
|
}
|
|
} |