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(); List> _sortedItems = new(); /// wrap for items sorting ModalInfo _modalInfo; protected override async Task OnInitializedAsync() { _currentAccount = await GetCurrentAcc(); if (ID > 0) { var surveys = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveys WHERE id={ID}"); if (surveys.Count > 0) { _survey = new() { EvenID = Convert.ToInt32(surveys[0]["eventid"]), ID = Convert.ToInt32(surveys[0]["id"]), Name = Convert.ToString(surveys[0]["name"]), Description = Convert.ToString(surveys[0]["description"]), DateCreated = Convert.ToDateTime(surveys[0]["date_created"]), DateUpdated = Convert.ToDateTime(surveys[0]["date_updated"]), CreatorID = Convert.ToString(surveys[0]["creatorid"]), IsNew = false }; Console.WriteLine($"SurveyEditor. load survey id: {_survey.ID}, name: {_survey.Name}"); } SurveyItem item; SurveyItemOption option; int itemPosition = 1; int optionPosition = 1; var surveyItems = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitems WHERE surveyid={_survey.ID} AND deleted<>1 ORDER BY position"); if (surveyItems.Count > 0) { Console.WriteLine($"surveyItems.Count={surveyItems.Count}"); foreach (var i in surveyItems) { item = new() { ID = Convert.ToInt32(i["id"]), SurveyID = Convert.ToInt32(i["surveyid"]), DateCreated = Convert.ToDateTime(i["date_created"]), DateUpdated = Convert.ToDateTime(i["date_updated"]), CreatorID = Convert.ToString(i["creatorid"]), Text = Convert.ToString(i["itemtext"]), //Position = Convert.ToInt32(i["position"]), Position = itemPosition++, Required = Convert.ToBoolean(i["required"]), IsNew = false }; Console.WriteLine($"SurveyEditor. load survey [{item.SurveyID}] item id: {item.ID}, position: {item.Position}, name: {item.Text}"); var surveyItemOption = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitemoptions WHERE itemid={item.ID} AND deleted<>1 ORDER BY position"); if (surveyItemOption.Count > 0) { Console.WriteLine($"surveyItemOption.Count={surveyItemOption.Count}"); foreach (var o in surveyItemOption) { option = new() { ID = Convert.ToInt32(o["id"]), ItemID = Convert.ToInt32(o["itemid"]), DateCreated = Convert.ToDateTime(o["date_created"]), DateUpdated = Convert.ToDateTime(o["date_updated"]), CreatorID = Convert.ToString(o["creatorid"]), Text = Convert.ToString(o["optiontext"]), //Position = Convert.ToInt32(o["position"]), Position = optionPosition++, Rate1 = Convert.ToInt32(o["rate1"]), Rate2 = Convert.ToInt32(o["rate2"]), Rate3 = Convert.ToInt32(o["rate3"]), Rate4 = Convert.ToInt32(o["rate4"]), IsNew = false }; Console.WriteLine($"SurveyEditor. load survey [{item.SurveyID}] item [{item.ID}] option id: {option.ID}, position: {option.Position}, name: {option.Text}."); item.SurveyItemOptions.Add(option.ID, option); } } _survey.SurveyItems.Add(item.ID, item); SortOptions(item.ID); } SortItems(); } } } void NewSurvey() => NavigationManager.NavigateTo("surveyeditor", true); void AddNewItem() { _survey.AddNewItem(_currentAccount.UUID); SortItems(); } void ItemMove(int itemID, int step) { _survey.MoveItem(itemID, step); SortItems(); } void ItemDelete(int itemID) { _survey.DeleteItem(itemID); SortItems(); } void AddNewOption(int itemID) { _survey.SurveyItems[itemID].AddNewOption(_currentAccount.UUID); SortOptions(itemID); } void OptionMove(int itemID, int optionID, int step) { _survey.SurveyItems[itemID].MoveItem(optionID, step); SortOptions(itemID); } void OptionDelete(int itemID, int optionID) { _survey.SurveyItems[itemID].DeleteOption(optionID); SortOptions(itemID); } async Task SaveSurvey() { await _survey.Save(_currentAccount.UUID); _modalInfo.Open("Опрос сохранен."); } void SortItems() { int itemPosition = 1; _sortedItems = _survey.SurveyItems.OrderBy(i => i.Value.Position).ToList(); foreach (var item in _sortedItems) { item.Value.Position = itemPosition++; } } void SortOptions(int itemID) { int optionPosition = 1; _survey.SurveyItems[itemID].SurveyItemOptionsSorted = _survey.SurveyItems[itemID].SurveyItemOptions.OrderBy(i => i.Value.Position).ToList(); foreach (var item in _survey.SurveyItems[itemID].SurveyItemOptionsSorted) { item.Value.Position = optionPosition++; } } 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; } } }