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. [Required]
  37. public int? Rating
  38. {
  39. get { return rating; }
  40. set
  41. {
  42. rating = value;
  43. if (rating < 1) rating = 1;
  44. if (rating > 5) rating = 5;
  45. }
  46. }
  47. private int? rating;
  48. private string initiatorUUID = "";
  49. public async Task<int> GetEditsCount(string acc_id = "")
  50. {
  51. MySQLConnector dbCon = MySQLConnector.Instance();
  52. string stringSQL = $"SELECT COUNT(*) " +
  53. $"FROM articles_edit_log ";
  54. if (acc_id.Length < 36)
  55. stringSQL += $"WHERE article_id={this.ID}";
  56. else
  57. stringSQL += $"WHERE article_id={this.ID} AND acc_id='{acc_id}'";
  58. int count = await dbCon.SQLSelectCount(stringSQL);
  59. dbCon.Close();
  60. return count;
  61. }
  62. public async Task<string> GetInitiatorUUID()
  63. {
  64. if (initiatorUUID.Length < 36)
  65. {
  66. Console.WriteLine("initiatorUUID is null, getting it from DB");
  67. MySQLConnector dbCon = MySQLConnector.Instance();
  68. string stringSQL = $"SELECT acc_id " +
  69. $"FROM articles " +
  70. $"JOIN actions_history ON actions_history.article_id = articles.id " +
  71. $"WHERE articles.id={this.ID} AND action_type={(int)ArticleStatus.Added}";
  72. initiatorUUID = await dbCon.SQLSelectUUID(stringSQL);
  73. dbCon.Close();
  74. }
  75. else
  76. Console.WriteLine("initiatorUUID already set");
  77. return initiatorUUID;
  78. }
  79. public object Clone()
  80. {
  81. return MemberwiseClone();
  82. }
  83. }
  84. }