This repository has been archived on 2025-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
BreCal/src/brecal.model/Securable.cs

98 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model
{
public class Securable : DbEntity
{
#region Properties
public string? Name { get; set; }
#endregion
#region public static methods
public static async Task<List<Securable>> LoadAll(IDBManager manager)
{
List<DbEntity> loadResultList = await manager.Load(SetLoadQuery, LoadElems);
List<Securable> result = new();
foreach (Securable s in loadResultList.Cast<Securable>())
result.Add(s);
return result;
}
public static void SetLoadQuery(IDbCommand cmd, params object[] list)
{
cmd.CommandText = "SELECT id, name, created, modified FROM securable";
}
public static List<DbEntity> LoadElems(IDataReader reader)
{
List<DbEntity> result = new List<DbEntity>();
while (reader.Read())
{
Securable s = new();
s.Id = (uint)reader.GetInt32(0);
if (!reader.IsDBNull(1)) s.Name = reader.GetString(1);
if (!reader.IsDBNull(2)) s.Created = reader.GetDateTime(2);
if (!reader.IsDBNull(3)) s.Modified = reader.GetDateTime(3);
result.Add(s);
}
return result;
}
#endregion
#region overrides
public override void SetUpdate(IDbCommand cmd)
{
cmd.CommandText = "UPDATE securable set name = @NAME WHERE id = @ID";
this.SetParameters(cmd);
}
public override void SetCreate(IDbCommand cmd)
{
cmd.CommandText = "INSERT INTO securable (name) VALUES ( @NAME)";
this.SetParameters(cmd);
}
public override void SetDelete(IDbCommand cmd)
{
cmd.CommandText = "DELETE FROM securable WHERE id = @ID";
this.SetParameters(cmd);
}
public override string ToString()
{
return this.Name ?? $"{base.Id} - {this.GetType().Name}";
}
#endregion
#region private methods
private void SetParameters(IDbCommand cmd)
{
IDbDataParameter name = cmd.CreateParameter();
name.ParameterName = "NAME";
name.Value = this.Name;
cmd.Parameters.Add(name);
IDataParameter idParam = cmd.CreateParameter();
idParam.ParameterName = "ID";
idParam.Value = this.Id;
cmd.Parameters.Add(idParam);
}
#endregion
}
}