Quellcode durchsuchen

survey engine&editor part3

ganahrhr vor 2 Jahren
Ursprung
Commit
1ecd691dce
4 geänderte Dateien mit 137 neuen und 30 gelöschten Zeilen
  1. 79 7
      Models/Survey.cs
  2. 11 11
      Pages/SurveyEditor.razor
  3. 46 11
      Pages/SurveyEditor.razor.cs
  4. 1 1
      Shared/Sidebar.razor.cs

+ 79 - 7
Models/Survey.cs

@@ -39,6 +39,7 @@ namespace HyperCube.Models
             newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
             IsNew = false;
 
+            /// saving survey items
             int newItemID;
             Dictionary<int, SurveyItem> tmpSurveyItems = new();
             foreach (var item in SurveyItems)
@@ -46,19 +47,48 @@ namespace HyperCube.Models
                 if (newID != 0)
                     item.Value.SurveyID = (int)newID;                
 
-                newItemID = await item.Value.Save(userID);                
+                newItemID = await item.Value.Save(userID);
                 if (newItemID != 0 && item.Key != newItemID)
                 {
-                    Console.WriteLine($"Adding and updating item id: [{item.Key}] with new id: [{newItemID}]");
+                    Console.WriteLine($"Adding and updating item id:{item.Key} with new id:{newItemID}");
                     tmpSurveyItems.Add(newItemID, item.Value);
                 }
                 else
                 {
-                    Console.WriteLine($"Adding existed item id: [{item.Key}]");
+                    Console.WriteLine($"Adding existed item id:{item.Key}");
                     tmpSurveyItems.Add(item.Key, item.Value);
-                }                
+                }
+
+                /// saving survey item options
+                int newOptionID;
+                Dictionary<int, SurveyItemOption> tmpSurveyItemOption = new();
+                foreach (var option in item.Value.SurveyItemOptions)
+                {
+                    if (newItemID != 0)
+                        option.Value.ItemID = newItemID;
+
+                    newOptionID = await option.Value.Save(userID);
+                    if (newOptionID != 0 && option.Key != newOptionID)
+                    {
+                        Console.WriteLine($"Adding and updating option id:{option.Key} with new id:{newOptionID}");
+                        tmpSurveyItemOption.Add(newOptionID, option.Value);
+                    }
+                    else
+                    {
+                        Console.WriteLine($"Adding existed option id:{option.Key}");
+                        tmpSurveyItemOption.Add(option.Key, option.Value);
+                    }
+                }
+
+                /// updating options collection with actual IDs from DB
+                if (tmpSurveyItemOption.Count > 0)
+                {
+                    item.Value.SurveyItemOptions.Clear();
+                    item.Value.SurveyItemOptions = tmpSurveyItemOption.ToDictionary(entry => entry.Key, entry => (SurveyItemOption)entry.Value.Clone());
+                }
             }
 
+            /// updating items collection with actual IDs from DB
             if (tmpSurveyItems.Count > 0)
             {
                 SurveyItems.Clear();
@@ -90,7 +120,7 @@ namespace HyperCube.Models
         {
             if (SurveyItems.Count > 0 && SurveyItems.ContainsKey(itemID))
             {
-                Console.WriteLine($"Survey:[{ID}] DeleteItem id: [{SurveyItems[itemID].ID}] - isnew: {SurveyItems[itemID].IsNew}");
+                Console.WriteLine($"Survey [{ID}] DeleteItem id:{SurveyItems[itemID].ID} - isnew:{SurveyItems[itemID].IsNew}");
 
                 if (SurveyItems[itemID].IsNew)
                     SurveyItems.Remove(itemID);
@@ -118,8 +148,7 @@ namespace HyperCube.Models
 
                 Console.WriteLine($"oldPosition:{oldPosition}, step:{step}, target:{targetPosition}.");
 
-                int targetKey = SurveyItems.FirstOrDefault(x => x.Value.Position == targetPosition).Key;
-                //SurveyItems.TryGetValue(targetPosition, out SurveyItem targetItem);
+                int targetKey = SurveyItems.FirstOrDefault(x => x.Value.Position == targetPosition).Key;                
                 if (targetKey != 0)
                 {
                     newPosition = SurveyItems[targetKey].Position;
@@ -187,6 +216,27 @@ namespace HyperCube.Models
             SurveyItemOptions.Add(newTempKey, option);
         }
 
+        public void DeleteOption(int optionID)
+        {
+            if (SurveyItemOptions.Count > 0 && SurveyItemOptions.ContainsKey(optionID))
+            {
+                Console.WriteLine($"Survey [{SurveyID}] Item [{ID}] DeleteItem id:{SurveyItemOptions[optionID].ID} - isnew:{SurveyItemOptions[optionID].IsNew}");
+
+                if (SurveyItemOptions[optionID].IsNew)
+                    SurveyItemOptions.Remove(optionID);
+                else
+                    SurveyItemOptions[optionID].IsDeleted = true;
+            }
+        }
+
+        public void MoveItem(int optionID, int step)
+        {
+            if (SurveyItemOptions.Count > 0 && SurveyItemOptions.ContainsKey(optionID))
+            {
+                Console.WriteLine($"Survey [{SurveyID}] Item [{ID}] MoveOption id:{SurveyItemOptions[optionID].ID}");
+            }
+        }
+
         public object Clone()
         {
             return MemberwiseClone();
@@ -209,6 +259,28 @@ namespace HyperCube.Models
         public bool IsNew { get; set; } = true;
         public bool IsDeleted { get; set; } = false;
 
+        public async Task<int> Save(string userID)
+        {
+            long newID;
+            string stringSQL;
+
+            if (IsNew)
+            {
+                stringSQL = $"INSERT INTO surveyitemoptions (itemid, optiontext, position, rate1, rate2, rate3, rate4, creatorid) " +
+                    $"VALUES ({ItemID}, '{Text}', {Position}, {Rate1}, {Rate2}, {Rate3}, {Rate4}, '{userID}')";
+            }
+            else
+            {
+                stringSQL = $"UPDATE surveyitemoptions " +
+                    $"SET itemid={ItemID}, optiontext='{Text}', position={Position}, rate1={Rate1}, rate2={Rate2}, rate3={Rate3}, rate4={Rate4}, deleted={Convert.ToInt32(IsDeleted)} " +
+                    $"WHERE id={ID}";
+            }
+
+            newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
+            IsNew = false;
+            return ID = (int)newID;
+        }
+
         public object Clone()
         {
             return MemberwiseClone();

+ 11 - 11
Pages/SurveyEditor.razor

@@ -21,8 +21,8 @@
         <div>
             <InputTextArea type="text" @bind-Value="_survey.Description" placeholder="Введите описание" />
         </div>
-        <div>Вопросы:</div>
-        @foreach (var item in _survey.SurveyItems)
+        <div style="border:2px solid #3a3838; margin:5px"></div>
+        @foreach (var item in _sortedItems)
         {
             if (!item.Value.IsDeleted)
             {
@@ -40,26 +40,26 @@
                     {
                         if (!option.Value.IsDeleted)
                         {
-                            Console.WriteLine($"");
-                            <div>
+                            <div style="margin-top:5px">
                                 Ответ №@option.Value.Position
-                                <a style="cursor:pointer" @onclick="OptionMove">&#11014;</a>
-                                <a style="cursor:pointer" @onclick="OptionMove">&#11015;</a>
-                                <a style="cursor:pointer" @onclick="OptionDelete">&#10006;</a>
+                                <a style="cursor:pointer" @onclick="(() => OptionMove(item.Key, option.Key, -1))">&#11014;</a>
+                                <a style="cursor:pointer" @onclick="(() => OptionMove(item.Key, option.Key, 1))">&#11015;</a>
+                                <a style="cursor:pointer" @onclick="(() => OptionDelete(item.Key, option.Key))">&#10006;</a>
                             </div>
                             <div>
                                 <InputText type="text" @bind-Value="option.Value.Text" placeholder="Введите текст ответа" />
                             </div>
                             <div>
-                                П - <InputNumber @bind-Value="option.Value.Rate1" style="width:40px; text-align:center" />
-                                М - <InputNumber @bind-Value="option.Value.Rate2" style="width:40px; text-align:center" />
-                                Л - <InputNumber @bind-Value="option.Value.Rate3" style="width:40px; text-align:center" />
-                                С - <InputNumber @bind-Value="option.Value.Rate4" style="width:40px; text-align:center" />
+                                П: <InputNumber @bind-Value="option.Value.Rate1" style="width:40px; text-align:center" />
+                                М: <InputNumber @bind-Value="option.Value.Rate2" style="width:40px; text-align:center" />
+                                Л: <InputNumber @bind-Value="option.Value.Rate3" style="width:40px; text-align:center" />
+                                С: <InputNumber @bind-Value="option.Value.Rate4" style="width:40px; text-align:center" />
                             </div>
                         }
                     }
                     <div style="margin-left:20px; margin-bottom:20px; cursor:pointer" @onclick="(() => AddNewOption(item.Key))">+ Добавить вариант ответа</div>
                 </div>
+                <div style="border:2px solid #3a3838; margin: 5px"></div>
             }
         }
         <div style="margin-bottom: 20px; cursor: pointer" @onclick="(() => AddNewItem())">+ Добавить вопрос</div>

+ 46 - 11
Pages/SurveyEditor.razor.cs

@@ -25,6 +25,9 @@ namespace HyperCube.Pages
         AccountModel _currentAccount;
         Survey _survey = new();
 
+        List<KeyValuePair<int, SurveyItem>> _sortedItems = new(); /// wrap for items sorting
+        List<KeyValuePair<int, SurveyItemOption>> _sortedOptions = new(); /// wrap for options sorting
+
         ModalInfo _modalInfo;
 
         protected override async Task OnInitializedAsync()
@@ -51,11 +54,15 @@ namespace HyperCube.Pages
                 }
 
                 SurveyItem item;
-                SurveyItemOption option;                
+                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()
@@ -66,15 +73,18 @@ namespace HyperCube.Pages
                             DateUpdated = Convert.ToDateTime(i["date_updated"]),
                             CreatorID = Convert.ToString(i["creatorid"]),
                             Text = Convert.ToString(i["itemtext"]),
-                            Position = Convert.ToInt32(i["position"]),
+                            //Position = Convert.ToInt32(i["position"]),
+                            Position = itemPosition++,
                             Required = Convert.ToBoolean(i["required"]),
                             IsNew = false
                         };
-                        Console.WriteLine($"SurveyEditor. load survey items id: {item.ID}, position: {item.Position}, name: {item.Text}");
+                        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()
@@ -85,21 +95,24 @@ namespace HyperCube.Pages
                                     DateUpdated = Convert.ToDateTime(o["date_updated"]),
                                     CreatorID = Convert.ToString(o["creatorid"]),
                                     Text = Convert.ToString(o["optiontext"]),
-                                    Position = Convert.ToInt32(o["position"]),
+                                    //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 option id: {option.ID}, position: {option.Position}, name: {option.Text}.");
+                                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();
+                }                               
             }
         }
 
@@ -112,20 +125,25 @@ namespace HyperCube.Pages
         void ItemMove(int itemID, int step)
         {
             _survey.MoveItem(itemID, step);
+            SortItems();
         }
 
         void ItemDelete(int itemID)
         {
             _survey.DeleteItem(itemID);
-            //StateHasChanged();
-        }
+            SortItems();
+        } 
 
-        void OptionMove()
+        void OptionMove(int itemID, int optionID, int step)
         {
+            _survey.SurveyItems[itemID].MoveItem(optionID, step);
+            SortOptions(itemID);
         }
 
-        void OptionDelete()
+        void OptionDelete(int itemID, int optionID)
         {
+            _survey.SurveyItems[itemID].DeleteOption(optionID);
+            SortOptions(itemID);
         }
 
         async Task SaveSurvey()
@@ -134,6 +152,23 @@ namespace HyperCube.Pages
             _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)
+        {
+            //Console.WriteLine($"SortOptions, itemID:{itemID}, SurveyItems.Count:{_survey.SurveyItems.Count}.");
+            //Console.WriteLine($"SurveyItemOptions.Count:{_survey.SurveyItems[itemID].SurveyItemOptions.Count}.");
+
+            int optionPosition = 1;
+            _sortedOptions = _survey.SurveyItems[itemID].SurveyItemOptions.OrderBy(i => i.Value.Position).ToList();
+            foreach (var item in _sortedOptions) { item.Value.Position = optionPosition++; }
+        }
+
         async Task<AccountModel> GetCurrentAcc()
         {
             Console.WriteLine($"SurveyEditor GetCurrentAcc");

+ 1 - 1
Shared/Sidebar.razor.cs

@@ -129,7 +129,7 @@ namespace HyperCube.Shared
                         IsApproved = Convert.ToBoolean(n["isapproved"]),
                         IsRejected = Convert.ToBoolean(n["isrejected"])
                     };
-                    Console.WriteLine($"add notification. id: {notification.ID}, header: {notification.Header}, body: {notification.Body}");
+                    //Console.WriteLine($"add notification. id: {notification.ID}, header: {notification.Header}, body: {notification.Body}");
 
                     _currentAccount.notifications.Add(notification.ID, notification);