ArticleModel.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.ComponentModel;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Threading.Tasks;
  5. using Console = HyperCube.Utils.AdvConsole;
  6. namespace HyperCube.Models
  7. {
  8. public enum ArticleStatus {
  9. [Display(Name = "Новая")]
  10. New = 0,
  11. [Display(Name = "Добавлена")]
  12. Added,
  13. [Display(Name = "Отредактирована")]
  14. Edited,
  15. [Display(Name = "Проверена")]
  16. Verified,
  17. [Display(Name = "Отклонена")]
  18. Rejected,
  19. [Display(Name = "Удалена")]
  20. Deleted }
  21. public class ArticleModel : ICloneable
  22. {
  23. public int ID { get; set; }
  24. public string Filename { get; set; }
  25. [Required]
  26. public string Name { get; set; }
  27. [Required]
  28. public DateTime PublishDate { get; set; } = DateTime.Now.Date;
  29. [Required]
  30. public string Authors { get; set; }
  31. [Required]
  32. public string Keywords { get; set; }
  33. [Required]
  34. public string Annotation { get; set; }
  35. public string Text { get; set; }
  36. public ArticleStatus Status { get; set; } = ArticleStatus.New;
  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. }