SurveyEditor.razor.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. await _survey.LoadByID(ID);
  30. }
  31. void NewSurvey() => NavigationManager.NavigateTo("surveyeditor", true);
  32. void AddNewItem()
  33. {
  34. _survey.AddNewItem(_currentAccount.UUID);
  35. _survey.SortItems();
  36. }
  37. void ItemMove(int itemID, int step)
  38. {
  39. _survey.MoveItem(itemID, step);
  40. _survey.SortItems();
  41. }
  42. void ItemDelete(int itemID)
  43. {
  44. _survey.DeleteItem(itemID);
  45. _survey.SortItems();
  46. }
  47. void AddNewOption(int itemID)
  48. {
  49. _survey.SurveyItems[itemID].AddNewOption(_currentAccount.UUID);
  50. _survey.SurveyItems[itemID].SortOptions();
  51. }
  52. void OptionMove(int itemID, int optionID, int step)
  53. {
  54. _survey.SurveyItems[itemID].MoveItem(optionID, step);
  55. _survey.SurveyItems[itemID].SortOptions();
  56. }
  57. void OptionDelete(int itemID, int optionID)
  58. {
  59. _survey.SurveyItems[itemID].DeleteOption(optionID);
  60. _survey.SurveyItems[itemID].SortOptions();
  61. }
  62. async Task SaveSurvey()
  63. {
  64. await _survey.Save(_currentAccount.UUID);
  65. _modalInfo.Open("Опрос сохранен.");
  66. }
  67. async Task<AccountModel> GetCurrentAcc()
  68. {
  69. Console.WriteLine($"SurveyEditor GetCurrentAcc");
  70. if (_currentAccount == null)
  71. {
  72. var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  73. var user = authState.User;
  74. if (user.Identity.IsAuthenticated)
  75. {
  76. var currentUser = await UserManager.GetUserAsync(user);
  77. var acc = AccountModel.Find(currentUser.Id);
  78. if (acc != null)
  79. {
  80. _currentAccount = AccountModel.Loaded[currentUser.Id];
  81. }
  82. else
  83. {
  84. Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
  85. if (accounts.ContainsKey(currentUser.Id))
  86. _currentAccount = accounts[currentUser.Id];
  87. }
  88. }
  89. }
  90. return _currentAccount;
  91. }
  92. }
  93. }