ArticleModel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. Saved,
  14. [Display(Name = "Ожидает верификации")]
  15. AwatingVerify,
  16. [Display(Name = "Верифицируется")]
  17. Verifying,
  18. [Display(Name = "Верифицирована")]
  19. Verified,
  20. [Display(Name = "Отклонена")]
  21. Rejected,
  22. [Display(Name = "Удалена")]
  23. Deleted }
  24. public class ArticleModel : ICloneable
  25. {
  26. public int ID { get; set; }
  27. public string Filename { get; set; }
  28. public string HashSum { get; set; }
  29. public string FilenameReal { get { return ID + "_" + Filename; } }
  30. [Required]
  31. public string Name { get; set; }
  32. [Required]
  33. public DateTime PublishDate { get; set; } = DateTime.Now.Date;
  34. [Required]
  35. public string Authors { get; set; }
  36. [Required]
  37. public string Keywords { get; set; }
  38. [Required]
  39. public string Annotation { get; set; }
  40. public string Text { get; set; }
  41. public ArticleStatus Status { get; set; } = ArticleStatus.New;
  42. public int? Rating
  43. {
  44. get { return rating; }
  45. set
  46. {
  47. rating = value;
  48. if (rating < 1) rating = 1;
  49. if (rating > 5) rating = 5;
  50. }
  51. }
  52. private int? rating;
  53. private string initiatorUUID = "";
  54. public static ArticleModel Find(int id)
  55. {
  56. if (id > 0) {
  57. if (AppData.Articles.ContainsKey(id))
  58. return AppData.Articles[id];
  59. }
  60. return null;
  61. }
  62. public async Task<int> GetEditsCount(string acc_id = "")
  63. {
  64. MySQLConnector dbCon = MySQLConnector.Instance();
  65. string stringSQL = $"SELECT COUNT(*) " +
  66. $"FROM articles_edit_log ";
  67. if (acc_id.Length < 36)
  68. stringSQL += $"WHERE article_id={this.ID}";
  69. else
  70. stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";
  71. int count = await dbCon.SQLSelectCount(stringSQL);
  72. //dbCon.Close();
  73. return count;
  74. }
  75. public async Task<string> GetInitiatorUUID()
  76. {
  77. if (initiatorUUID.Length < 36)
  78. {
  79. Console.WriteLine("initiatorUUID is null, getting it from DB");
  80. MySQLConnector dbCon = MySQLConnector.Instance();
  81. string stringSQL = $"SELECT acc_id " +
  82. $"FROM articles " +
  83. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  84. $"WHERE articles.id={this.ID} AND action_type={(int)ArticleStatus.Saved}";
  85. initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
  86. //dbCon.Close();
  87. }
  88. else
  89. Console.WriteLine("initiatorUUID already set");
  90. return initiatorUUID;
  91. }
  92. public object Clone()
  93. {
  94. return MemberwiseClone();
  95. }
  96. }
  97. }