ArticleModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Threading.Tasks;
  6. using Console = HyperCube.Utils.AdvConsole;
  7. namespace HyperCube.Models
  8. {
  9. public enum ArticleStatus {
  10. [Display(Name = "Новая")]
  11. New = 0,
  12. [Display(Name = "Добавлена")]
  13. Added,
  14. [Display(Name = "Отредактирована")]
  15. Edited,
  16. [Display(Name = "Проверена")]
  17. Verified,
  18. [Display(Name = "Отклонена")]
  19. Rejected,
  20. [Display(Name = "Удалена")]
  21. Deleted }
  22. public class ArticleModel : ICloneable
  23. {
  24. public static Dictionary<int, ArticleModel> articleModels;
  25. public static async Task LoadArticles()
  26. {
  27. MySQLConnector dbCon = MySQLConnector.Instance();
  28. string stringSQL = $"SELECT a.id, filename, article_name, date_publish, authors, ah.action_type " +
  29. $"FROM articles a " +
  30. $"LEFT JOIN actions_history ah ON a.id = ah.article_id " +
  31. $"AND EXISTS (SELECT 1 FROM actions_history ah1 WHERE ah.article_id = ah1.article_id HAVING MAX(ah1.date_add) = ah.date_add) " +
  32. $"ORDER BY a.id";
  33. articleModels = await dbCon.SQLSelectArticles(stringSQL);
  34. //dbCon.Close();
  35. }
  36. public int ID { get; set; }
  37. public string Filename { get; set; }
  38. public string FilenameReal { get { return ID + "_" + Filename; } }
  39. [Required]
  40. public string Name { get; set; }
  41. [Required]
  42. public DateTime PublishDate { get; set; } = DateTime.Now.Date;
  43. [Required]
  44. public string Authors { get; set; }
  45. [Required]
  46. public string Keywords { get; set; }
  47. [Required]
  48. public string Annotation { get; set; }
  49. public string Text { get; set; }
  50. public ArticleStatus Status { get; set; } = ArticleStatus.New;
  51. public int? Rating
  52. {
  53. get { return rating; }
  54. set
  55. {
  56. rating = value;
  57. if (rating < 1) rating = 1;
  58. if (rating > 5) rating = 5;
  59. }
  60. }
  61. private int? rating;
  62. private string initiatorUUID = "";
  63. public static ArticleModel Find(int id)
  64. {
  65. if (id > 0) {
  66. if (articleModels.ContainsKey(id))
  67. return articleModels[id];
  68. }
  69. return null;
  70. }
  71. public async Task<int> GetEditsCount(string acc_id = "")
  72. {
  73. MySQLConnector dbCon = MySQLConnector.Instance();
  74. string stringSQL = $"SELECT COUNT(*) " +
  75. $"FROM articles_edit_log ";
  76. if (acc_id.Length < 36)
  77. stringSQL += $"WHERE article_id={this.ID}";
  78. else
  79. stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";
  80. int count = await dbCon.SQLSelectCount(stringSQL);
  81. dbCon.Close();
  82. return count;
  83. }
  84. public async Task<string> GetInitiatorUUID()
  85. {
  86. if (initiatorUUID.Length < 36)
  87. {
  88. Console.WriteLine("initiatorUUID is null, getting it from DB");
  89. MySQLConnector dbCon = MySQLConnector.Instance();
  90. string stringSQL = $"SELECT acc_id " +
  91. $"FROM articles " +
  92. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  93. $"WHERE articles.id={this.ID} AND action_type={(int)ArticleStatus.Added}";
  94. initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
  95. dbCon.Close();
  96. }
  97. else
  98. Console.WriteLine("initiatorUUID already set");
  99. return initiatorUUID;
  100. }
  101. public object Clone()
  102. {
  103. return MemberwiseClone();
  104. }
  105. }
  106. }