Deletion.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace InABox.Core
  9. {
  10. public class SetNullData
  11. {
  12. public Guid EntityID { get; set; }
  13. public Guid ParentID { get; set; }
  14. public string Property { get; set; }
  15. public SetNullData(Guid entityID, string property, Guid parentID)
  16. {
  17. EntityID = entityID;
  18. ParentID = parentID;
  19. Property = property;
  20. }
  21. }
  22. public class DeletionData
  23. {
  24. public Dictionary<string, List<SetNullData>> SetNulls { get; set; } = new Dictionary<string, List<SetNullData>>();
  25. public Dictionary<string, CoreTable> Cascades { get; set; } = new Dictionary<string, CoreTable>();
  26. public static IColumns DeletionColumns(Type T)
  27. {
  28. var columns = Columns.None(T);
  29. foreach(var property in DatabaseSchema.Properties(T).Where(x => x.IsDBColumn))
  30. {
  31. columns.Add(property);
  32. }
  33. return columns;
  34. }
  35. public static Columns<T> DeletionColumns<T>() => (DeletionColumns(typeof(T)) as Columns<T>)!;
  36. public void DeleteEntity<T>(T entity)
  37. where T : Entity, new()
  38. {
  39. var entityName = typeof(T).EntityName();
  40. if(!Cascades.TryGetValue(entityName, out var table))
  41. {
  42. table = new CoreTable();
  43. table.LoadColumns(DeletionColumns<T>());
  44. Cascades.Add(entityName, table);
  45. }
  46. table.LoadRow(entity);
  47. }
  48. public void DeleteEntity<T>(CoreRow row)
  49. where T : Entity, new()
  50. {
  51. var entityName = typeof(T).EntityName();
  52. if(!Cascades.TryGetValue(entityName, out var table))
  53. {
  54. table = new CoreTable();
  55. table.LoadColumns(DeletionColumns<T>());
  56. Cascades.Add(entityName, table);
  57. }
  58. table.LoadRows(new CoreRow[] { row });
  59. }
  60. public void SetNullEntity<T>(Guid entityID, string property, Guid parentID)
  61. where T : Entity, new()
  62. {
  63. var entityName = typeof(T).EntityName();
  64. if (!SetNulls.TryGetValue(entityName, out var list))
  65. {
  66. list = new List<SetNullData>();
  67. SetNulls.Add(entityName, list);
  68. }
  69. list.Add(new SetNullData(entityID, property, parentID));
  70. }
  71. }
  72. // A Deletion is not IRemotable; *no one* but the database should know about it.
  73. public class Deletion : Entity, ILicense<CoreLicense>, IPersistent
  74. {
  75. [DateTimeEditor(Editable = Editable.Disabled)]
  76. public DateTime DeletionDate { get; set; }
  77. [TextBoxEditor(Editable = Editable.Disabled)]
  78. public string DeletedBy { get; set; }
  79. [TextBoxEditor(Editable = Editable.Disabled)]
  80. public string HeadTable { get; set; }
  81. [TextBoxEditor(Editable = Editable.Disabled)]
  82. public string Description { get; set; }
  83. [NullEditor]
  84. public string Data { get; set; }
  85. }
  86. }