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> LoadAll(IDBManager manager) { List loadResultList = await manager.Load(SetLoadQuery, LoadElems); List result = new(); foreach (Securable s in loadResultList.Cast()) 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 LoadElems(IDataReader reader) { List result = new List(); 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 } }