ArticleModel.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.ComponentModel;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Threading.Tasks;
  5. namespace HyperCube.Models
  6. {
  7. public enum ArticleStatus {
  8. [Display(Name = "Новая")]
  9. New = 0,
  10. [Display(Name = "Добавлена")]
  11. Added,
  12. [Display(Name = "Отредактирована")]
  13. Edited,
  14. [Display(Name = "Проверена")]
  15. Verified,
  16. [Display(Name = "Отклонена")]
  17. Rejected,
  18. [Display(Name = "Удалена")]
  19. Deleted }
  20. public class ArticleModel : ICloneable
  21. {
  22. public int ID { get; set; }
  23. public string Filename { get; set; }
  24. [Required]
  25. public string Name { get; set; }
  26. [Required]
  27. public DateTime PublishDate { get; set; } = DateTime.Now.Date;
  28. [Required]
  29. public string Authors { get; set; }
  30. [Required]
  31. public string Keywords { get; set; }
  32. [Required]
  33. public string Annotation { get; set; }
  34. public string Text { get; set; }
  35. public ArticleStatus Status { get; set; } = ArticleStatus.New;
  36. public int? Rating
  37. {
  38. get { return rating; }
  39. set
  40. {
  41. rating = value;
  42. if (rating < 1) rating = 1;
  43. if (rating > 5) rating = 5;
  44. }
  45. }
  46. private int? rating;
  47. private string initiatorUUID = "";
  48. public async Task<int> GetEditsCount(string acc_id = "")
  49. {
  50. MySQLConnector dbCon = MySQLConnector.Instance();
  51. string stringSQL = $"SELECT COUNT(*) " +
  52. $"FROM articles_edit_log ";
  53. if (acc_id.Length < 36)
  54. stringSQL += $"WHERE article_id={this.ID}";
  55. else
  56. stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";
  57. int count = await dbCon.SQLSelectCount(stringSQL);
  58. dbCon.Close();
  59. return count;
  60. }
  61. public async Task<string> GetInitiatorUUID()
  62. {
  63. if (initiatorUUID.Length < 36)
  64. {
  65. Console.WriteLine("initiatorUUID is null, getting it from DB");
  66. MySQLConnector dbCon = MySQLConnector.Instance();
  67. string stringSQL = $"SELECT acc_id " +
  68. $"FROM articles " +
  69. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  70. $"WHERE articles.id={this.ID} AND action_type={(int)ArticleStatus.Added}";
  71. initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
  72. dbCon.Close();
  73. }
  74. else
  75. Console.WriteLine("initiatorUUID already set");
  76. return initiatorUUID;
  77. }
  78. public object Clone()
  79. {
  80. return MemberwiseClone();
  81. }
  82. }
  83. }