ArticleModel.cs 3.4 KB

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