SurveyEditor.razor.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. List<KeyValuePair<int, SurveyItem>> _sortedItems = new(); /// wrap for items sorting
  25. ModalInfo _modalInfo;
  26. protected override async Task OnInitializedAsync()
  27. {
  28. _currentAccount = await GetCurrentAcc();
  29. if (ID > 0)
  30. {
  31. var surveys = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveys WHERE id={ID}");
  32. if (surveys.Count > 0)
  33. {
  34. _survey = new()
  35. {
  36. EvenID = Convert.ToInt32(surveys[0]["eventid"]),
  37. ID = Convert.ToInt32(surveys[0]["id"]),
  38. Name = Convert.ToString(surveys[0]["name"]),
  39. Description = Convert.ToString(surveys[0]["description"]),
  40. DateCreated = Convert.ToDateTime(surveys[0]["date_created"]),
  41. DateUpdated = Convert.ToDateTime(surveys[0]["date_updated"]),
  42. CreatorID = Convert.ToString(surveys[0]["creatorid"]),
  43. IsNew = false
  44. };
  45. Console.WriteLine($"SurveyEditor. load survey id: {_survey.ID}, name: {_survey.Name}");
  46. }
  47. SurveyItem item;
  48. SurveyItemOption option;
  49. int itemPosition = 1;
  50. int optionPosition = 1;
  51. var surveyItems = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitems WHERE surveyid={_survey.ID} AND deleted<>1 ORDER BY position");
  52. if (surveyItems.Count > 0)
  53. {
  54. Console.WriteLine($"surveyItems.Count={surveyItems.Count}");
  55. foreach (var i in surveyItems)
  56. {
  57. item = new()
  58. {
  59. ID = Convert.ToInt32(i["id"]),
  60. SurveyID = Convert.ToInt32(i["surveyid"]),
  61. DateCreated = Convert.ToDateTime(i["date_created"]),
  62. DateUpdated = Convert.ToDateTime(i["date_updated"]),
  63. CreatorID = Convert.ToString(i["creatorid"]),
  64. Text = Convert.ToString(i["itemtext"]),
  65. //Position = Convert.ToInt32(i["position"]),
  66. Position = itemPosition++,
  67. Required = Convert.ToBoolean(i["required"]),
  68. IsNew = false
  69. };
  70. Console.WriteLine($"SurveyEditor. load survey [{item.SurveyID}] item id: {item.ID}, position: {item.Position}, name: {item.Text}");
  71. var surveyItemOption = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitemoptions WHERE itemid={item.ID} AND deleted<>1 ORDER BY position");
  72. if (surveyItemOption.Count > 0)
  73. {
  74. Console.WriteLine($"surveyItemOption.Count={surveyItemOption.Count}");
  75. foreach (var o in surveyItemOption)
  76. {
  77. option = new()
  78. {
  79. ID = Convert.ToInt32(o["id"]),
  80. ItemID = Convert.ToInt32(o["itemid"]),
  81. DateCreated = Convert.ToDateTime(o["date_created"]),
  82. DateUpdated = Convert.ToDateTime(o["date_updated"]),
  83. CreatorID = Convert.ToString(o["creatorid"]),
  84. Text = Convert.ToString(o["optiontext"]),
  85. //Position = Convert.ToInt32(o["position"]),
  86. Position = optionPosition++,
  87. Rate1 = Convert.ToInt32(o["rate1"]),
  88. Rate2 = Convert.ToInt32(o["rate2"]),
  89. Rate3 = Convert.ToInt32(o["rate3"]),
  90. Rate4 = Convert.ToInt32(o["rate4"]),
  91. IsNew = false
  92. };
  93. Console.WriteLine($"SurveyEditor. load survey [{item.SurveyID}] item [{item.ID}] option id: {option.ID}, position: {option.Position}, name: {option.Text}.");
  94. item.SurveyItemOptions.Add(option.ID, option);
  95. }
  96. }
  97. _survey.SurveyItems.Add(item.ID, item);
  98. SortOptions(item.ID);
  99. }
  100. SortItems();
  101. }
  102. }
  103. }
  104. void NewSurvey() => NavigationManager.NavigateTo("surveyeditor", true);
  105. void AddNewItem()
  106. {
  107. _survey.AddNewItem(_currentAccount.UUID);
  108. SortItems();
  109. }
  110. void ItemMove(int itemID, int step)
  111. {
  112. _survey.MoveItem(itemID, step);
  113. SortItems();
  114. }
  115. void ItemDelete(int itemID)
  116. {
  117. _survey.DeleteItem(itemID);
  118. SortItems();
  119. }
  120. void AddNewOption(int itemID)
  121. {
  122. _survey.SurveyItems[itemID].AddNewOption(_currentAccount.UUID);
  123. SortOptions(itemID);
  124. }
  125. void OptionMove(int itemID, int optionID, int step)
  126. {
  127. _survey.SurveyItems[itemID].MoveItem(optionID, step);
  128. SortOptions(itemID);
  129. }
  130. void OptionDelete(int itemID, int optionID)
  131. {
  132. _survey.SurveyItems[itemID].DeleteOption(optionID);
  133. SortOptions(itemID);
  134. }
  135. async Task SaveSurvey()
  136. {
  137. await _survey.Save(_currentAccount.UUID);
  138. _modalInfo.Open("Опрос сохранен.");
  139. }
  140. void SortItems()
  141. {
  142. int itemPosition = 1;
  143. _sortedItems = _survey.SurveyItems.OrderBy(i => i.Value.Position).ToList();
  144. foreach (var item in _sortedItems) { item.Value.Position = itemPosition++; }
  145. }
  146. void SortOptions(int itemID)
  147. {
  148. int optionPosition = 1;
  149. _survey.SurveyItems[itemID].SurveyItemOptionsSorted = _survey.SurveyItems[itemID].SurveyItemOptions.OrderBy(i => i.Value.Position).ToList();
  150. foreach (var item in _survey.SurveyItems[itemID].SurveyItemOptionsSorted) { item.Value.Position = optionPosition++; }
  151. }
  152. async Task<AccountModel> GetCurrentAcc()
  153. {
  154. Console.WriteLine($"SurveyEditor GetCurrentAcc");
  155. if (_currentAccount == null)
  156. {
  157. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  158. var user = authState.User;
  159. if (user.Identity.IsAuthenticated)
  160. {
  161. var currentUser = await UserManager.GetUserAsync(user);
  162. var acc = AccountModel.Find(currentUser.Id);
  163. if (acc != null)
  164. {
  165. _currentAccount = AccountModel.Loaded[currentUser.Id];
  166. }
  167. else
  168. {
  169. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  170. if (accounts.ContainsKey(currentUser.Id))
  171. _currentAccount = accounts[currentUser.Id];
  172. }
  173. }
  174. }
  175. return _currentAccount;
  176. }
  177. }
  178. }