|
@@ -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();
|