SurveyEditor.razor.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Microsoft.AspNetCore.Components;
  2. using Microsoft.AspNetCore.Components.Authorization;
  3. using Microsoft.AspNetCore.Identity;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using HyperCube.Models;
  9. using Console = HyperCube.Utils.AdvConsole;
  10. namespace HyperCube.Pages
  11. {
  12. public partial class SurveyEditor : ComponentBase
  13. {
  14. [Inject]
  15. NavigationManager NavigationManager { get; set; }
  16. [Inject]
  17. AuthenticationStateProvider AuthenticationStateProvider { get; set; }
  18. [Inject]
  19. UserManager<IdentityUser> UserManager { get; set; }
  20. [Parameter]
  21. public int ID { get; set; }
  22. AccountModel _currentAccount;
  23. Survey _survey = new();
  24. ModalInfo _modalInfo;
  25. protected override async Task OnInitializedAsync()
  26. {
  27. _currentAccount = await GetCurrentAcc();
  28. if (ID > 0)
  29. {
  30. var surveys = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveys WHERE id={ID}");
  31. if (surveys.Count > 0)
  32. {
  33. _survey = new()
  34. {
  35. EvenID = Convert.ToInt32(surveys[0]["eventid"]),
  36. ID = Convert.ToInt32(surveys[0]["id"]),
  37. Name = Convert.ToString(surveys[0]["name"]),
  38. Description = Convert.ToString(surveys[0]["description"]),
  39. DateCreated = Convert.ToDateTime(surveys[0]["date_created"]),
  40. DateUpdated = Convert.ToDateTime(surveys[0]["date_updated"]),
  41. CreatorID = Convert.ToString(surveys[0]["creatorid"]),
  42. IsNew = false
  43. };
  44. Console.WriteLine($"SurveyEditor. load survey id: {_survey.ID}, name: {_survey.Name}");
  45. }
  46. SurveyItem item;
  47. SurveyItemOption option;
  48. var surveyItems = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitems WHERE surveyid={_survey.ID} AND deleted<>1 ORDER BY position");
  49. if (surveyItems.Count > 0)
  50. {
  51. foreach (var i in surveyItems)
  52. {
  53. item = new()
  54. {
  55. ID = Convert.ToInt32(i["id"]),
  56. SurveyID = Convert.ToInt32(i["surveyid"]),
  57. DateCreated = Convert.ToDateTime(i["date_created"]),
  58. DateUpdated = Convert.ToDateTime(i["date_updated"]),
  59. CreatorID = Convert.ToString(i["creatorid"]),
  60. Text = Convert.ToString(i["itemtext"]),
  61. Position = Convert.ToInt32(i["position"]),
  62. Required = Convert.ToBoolean(i["required"]),
  63. IsNew = false
  64. };
  65. Console.WriteLine($"SurveyEditor. load survey items id: {item.ID}, position: {item.Position}, name: {item.Text}");
  66. var surveyItemOption = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitemoptions WHERE itemid={item.ID} AND deleted<>1 ORDER BY position");
  67. if (surveyItemOption.Count > 0)
  68. {
  69. foreach (var o in surveyItemOption)
  70. {
  71. option = new()
  72. {
  73. ID = Convert.ToInt32(o["id"]),
  74. ItemID = Convert.ToInt32(o["itemid"]),
  75. DateCreated = Convert.ToDateTime(o["date_created"]),
  76. DateUpdated = Convert.ToDateTime(o["date_updated"]),
  77. CreatorID = Convert.ToString(o["creatorid"]),
  78. Text = Convert.ToString(o["optiontext"]),
  79. Position = Convert.ToInt32(o["position"]),
  80. Rate1 = Convert.ToInt32(o["rate1"]),
  81. Rate2 = Convert.ToInt32(o["rate2"]),
  82. Rate3 = Convert.ToInt32(o["rate3"]),
  83. Rate4 = Convert.ToInt32(o["rate4"]),
  84. IsNew = false
  85. };
  86. Console.WriteLine($"SurveyEditor. load survey item option id: {option.ID}, position: {option.Position}, name: {option.Text}.");
  87. item.SurveyItemOptions.Add(option.ID, option);
  88. }
  89. }
  90. _survey.SurveyItems.Add(item.ID, item);
  91. }
  92. }
  93. }
  94. }
  95. void AddNewItem() => _survey.AddNewItem(_currentAccount.UUID);
  96. void AddNewOption(int ItemID) => _survey.SurveyItems[ItemID].AddNewOption(_currentAccount.UUID);
  97. void NewSurvey() => NavigationManager.NavigateTo("surveyeditor", true);
  98. void ItemMove(int itemID, int step)
  99. {
  100. _survey.MoveItem(itemID, step);
  101. }
  102. void ItemDelete(int itemID)
  103. {
  104. _survey.DeleteItem(itemID);
  105. StateHasChanged();
  106. }
  107. void OptionMove()
  108. {
  109. }
  110. void OptionDelete()
  111. {
  112. }
  113. async Task SaveSurvey()
  114. {
  115. await _survey.Save(_currentAccount.UUID);
  116. _modalInfo.Open("Опрос сохранен.");
  117. }
  118. async Task<AccountModel> GetCurrentAcc()
  119. {
  120. Console.WriteLine($"SurveyEditor GetCurrentAcc");
  121. if (_currentAccount == null)
  122. {
  123. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  124. var user = authState.User;
  125. if (user.Identity.IsAuthenticated)
  126. {
  127. var currentUser = await UserManager.GetUserAsync(user);
  128. var acc = AccountModel.Find(currentUser.Id);
  129. if (acc != null)
  130. {
  131. _currentAccount = AccountModel.Loaded[currentUser.Id];
  132. }
  133. else
  134. {
  135. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  136. if (accounts.ContainsKey(currentUser.Id))
  137. _currentAccount = accounts[currentUser.Id];
  138. }
  139. }
  140. }
  141. return _currentAccount;
  142. }
  143. }
  144. }