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 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 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 accounts = await MySQLConnector.Instance().SQLSelectASPUsers(); if (accounts.ContainsKey(currentUser.Id)) _currentAccount = accounts[currentUser.Id]; } } } return _currentAccount; } } }