ArticleModel.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 int ID { get; set; }
  25. public string Filename { get; set; }
  26. public string FileHashSum { get; set; }
  27. public string FilenameReal { get { return ID + "_" + Filename; } }
  28. [Required]
  29. public string Name { get; set; }
  30. [Required]
  31. public DateTime PublishDate { get; set; } = DateTime.Now.Date;
  32. [Required]
  33. public string Authors { get; set; }
  34. [Required]
  35. public string Keywords { get; set; }
  36. [Required]
  37. public string Annotation { get; set; }
  38. public string Text { get; set; }
  39. public ArticleStatus Status { get; set; } = ArticleStatus.New;
  40. public int? Rating
  41. {
  42. get { return rating; }
  43. set
  44. {
  45. rating = value;
  46. if (rating < 1) rating = 1;
  47. if (rating > 5) rating = 5;
  48. }
  49. }
  50. private int? rating;
  51. private string initiatorUUID = "";
  52. public static ArticleModel Find(int id)
  53. {
  54. if (id > 0) {
  55. if (AppData.Articles.ContainsKey(id))
  56. return AppData.Articles[id];
  57. }
  58. return null;
  59. }
  60. public async Task<int> GetEditsCount(string acc_id = "")
  61. {
  62. MySQLConnector dbCon = MySQLConnector.Instance();
  63. string stringSQL = $"SELECT COUNT(*) " +
  64. $"FROM articles_edit_log ";
  65. if (acc_id.Length < 36)
  66. stringSQL += $"WHERE article_id={this.ID}";
  67. else
  68. stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";
  69. int count = await dbCon.SQLSelectCount(stringSQL);
  70. //dbCon.Close();
  71. return count;
  72. }
  73. public async Task<string> GetInitiatorUUID()
  74. {
  75. if (initiatorUUID.Length < 36)
  76. {
  77. Console.WriteLine("initiatorUUID is null, getting it from DB");
  78. MySQLConnector dbCon = MySQLConnector.Instance();
  79. string stringSQL = $"SELECT acc_id " +
  80. $"FROM articles " +
  81. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  82. $"WHERE articles.id={this.ID} AND action_type={(int)ArticleStatus.Added}";
  83. initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
  84. //dbCon.Close();
  85. }
  86. else
  87. Console.WriteLine("initiatorUUID already set");
  88. return initiatorUUID;
  89. }
  90. public object Clone()
  91. {
  92. return MemberwiseClone();
  93. }
  94. }
  95. }