123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- using Microsoft.AspNetCore.Components;
- using Microsoft.AspNetCore.Components.Authorization;
- using Microsoft.AspNetCore.Identity;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using HyperCube.Models;
- using Console = HyperCube.Utils.AdvConsole;
- namespace HyperCube.Pages
- {
- public partial class SurveyEditor : ComponentBase
- {
- [Inject]
- NavigationManager NavigationManager { get; set; }
- [Inject]
- AuthenticationStateProvider AuthenticationStateProvider { get; set; }
- [Inject]
- UserManager<IdentityUser> UserManager { get; set; }
- [Parameter]
- public int ID { get; set; }
- AccountModel _currentAccount;
- Survey _survey = new();
- ModalInfo _modalInfo;
- protected override async Task OnInitializedAsync()
- {
- _currentAccount = await GetCurrentAcc();
- if (ID > 0)
- await _survey.LoadByID(ID);
- }
- void NewSurvey() => NavigationManager.NavigateTo("surveyeditor", true);
- void AddNewItem()
- {
- _survey.AddNewItem(_currentAccount.UUID);
- _survey.SortItems();
- }
- void ItemMove(int itemID, int step)
- {
- _survey.MoveItem(itemID, step);
- _survey.SortItems();
- }
- void ItemDelete(int itemID)
- {
- _survey.DeleteItem(itemID);
- _survey.SortItems();
- }
- void AddNewOption(int itemID)
- {
- _survey.SurveyItems[itemID].AddNewOption(_currentAccount.UUID);
- _survey.SurveyItems[itemID].SortOptions();
- }
- void OptionMove(int itemID, int optionID, int step)
- {
- _survey.SurveyItems[itemID].MoveItem(optionID, step);
- _survey.SurveyItems[itemID].SortOptions();
- }
- void OptionDelete(int itemID, int optionID)
- {
- _survey.SurveyItems[itemID].DeleteOption(optionID);
- _survey.SurveyItems[itemID].SortOptions();
- }
- async Task SaveSurvey()
- {
- await _survey.Save(_currentAccount.UUID);
- _modalInfo.Open("Опрос сохранен.");
- }
- async Task<AccountModel> GetCurrentAcc()
- {
- Console.WriteLine($"SurveyEditor GetCurrentAcc");
- if (_currentAccount == null)
- {
- var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
- var user = authState.User;
- if (user.Identity.IsAuthenticated)
- {
- var currentUser = await UserManager.GetUserAsync(user);
- var acc = AccountModel.Find(currentUser.Id);
- if (acc != null)
- {
- _currentAccount = AccountModel.Loaded[currentUser.Id];
- }
- else
- {
- Dictionary<string, AccountModel> accounts = await MySQLConnector.Instance().SQLSelectASPUsers();
- if (accounts.ContainsKey(currentUser.Id))
- _currentAccount = accounts[currentUser.Id];
- }
- }
- }
- return _currentAccount;
- }
- }
- }
|