123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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)
- {
- 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;
- var surveyItems = await MySQLConnector.Instance().SQLSelectComplex($"SELECT * FROM surveyitems WHERE surveyid={_survey.ID} AND deleted<>1 ORDER BY position");
- if (surveyItems.Count > 0)
- {
- 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"]),
- Required = Convert.ToBoolean(i["required"]),
- IsNew = false
- };
- Console.WriteLine($"SurveyEditor. load survey items 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)
- {
- 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"]),
- 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 option id: {option.ID}, position: {option.Position}, name: {option.Text}.");
- item.SurveyItemOptions.Add(option.ID, option);
- }
- }
- _survey.SurveyItems.Add(item.ID, item);
- }
- }
- }
- }
- void AddNewItem() => _survey.AddNewItem(_currentAccount.UUID);
- void AddNewOption(int ItemID) => _survey.SurveyItems[ItemID].AddNewOption(_currentAccount.UUID);
- void NewSurvey() => NavigationManager.NavigateTo("surveyeditor", true);
- void ItemMove(int itemID, int step)
- {
- _survey.MoveItem(itemID, step);
- }
- void ItemDelete(int itemID)
- {
- _survey.DeleteItem(itemID);
- StateHasChanged();
- }
- void OptionMove()
- {
- }
- void OptionDelete()
- {
- }
- 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;
- }
- }
- }
|