|
@@ -17,6 +17,7 @@ namespace HyperCube.Models
|
|
|
public string CreatorID { get; set; }
|
|
|
public Dictionary<int, SurveyItem> SurveyItems { get; set; } = new();
|
|
|
public bool IsNew { get; set; } = true;
|
|
|
+ public bool IsDeleted { get; set; } = false;
|
|
|
|
|
|
public async Task<int> Save(string userID)
|
|
|
{
|
|
@@ -25,21 +26,45 @@ namespace HyperCube.Models
|
|
|
|
|
|
if (IsNew)
|
|
|
{
|
|
|
- ///tmp
|
|
|
- EvenID = 1;
|
|
|
-
|
|
|
stringSQL = $"INSERT INTO surveys (eventid, name, description, creatorid) " +
|
|
|
$"VALUES ({EvenID}, '{Name}', '{Description}', '{userID}')";
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
stringSQL = $"UPDATE surveys " +
|
|
|
- $"SET eventid={EvenID}, name='{Name}', description='{Description}' " + // creatorid='{userID}'
|
|
|
+ $"SET eventid={EvenID}, name='{Name}', description='{Description}', deleted={Convert.ToInt32(IsDeleted)} " +
|
|
|
$"WHERE id={ID}";
|
|
|
}
|
|
|
|
|
|
newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
|
|
|
IsNew = false;
|
|
|
+
|
|
|
+ int newItemID;
|
|
|
+ Dictionary<int, SurveyItem> tmpSurveyItems = new();
|
|
|
+ foreach (var item in SurveyItems)
|
|
|
+ {
|
|
|
+ if (newID != 0)
|
|
|
+ item.Value.SurveyID = (int)newID;
|
|
|
+
|
|
|
+ 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}]");
|
|
|
+ tmpSurveyItems.Add(newItemID, item.Value);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Console.WriteLine($"Adding existed item id: [{item.Key}]");
|
|
|
+ tmpSurveyItems.Add(item.Key, item.Value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (tmpSurveyItems.Count > 0)
|
|
|
+ {
|
|
|
+ SurveyItems.Clear();
|
|
|
+ SurveyItems = tmpSurveyItems.ToDictionary(entry => entry.Key, entry => (SurveyItem)entry.Value.Clone());
|
|
|
+ }
|
|
|
+
|
|
|
return ID = (int)newID;
|
|
|
}
|
|
|
|
|
@@ -60,9 +85,54 @@ namespace HyperCube.Models
|
|
|
};
|
|
|
SurveyItems.Add(newTempKey, item);
|
|
|
}
|
|
|
+
|
|
|
+ public void DeleteItem(int itemID)
|
|
|
+ {
|
|
|
+ if (SurveyItems.Count > 0 && SurveyItems.ContainsKey(itemID))
|
|
|
+ {
|
|
|
+ Console.WriteLine($"Survey:[{ID}] DeleteItem id: [{SurveyItems[itemID].ID}] - isnew: {SurveyItems[itemID].IsNew}");
|
|
|
+
|
|
|
+ if (SurveyItems[itemID].IsNew)
|
|
|
+ SurveyItems.Remove(itemID);
|
|
|
+ else
|
|
|
+ SurveyItems[itemID].IsDeleted = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void MoveItem(int itemID, int step)
|
|
|
+ {
|
|
|
+ if (SurveyItems.Count > 0 && SurveyItems.ContainsKey(itemID))
|
|
|
+ {
|
|
|
+ Console.WriteLine($"Survey:[{ID}] MoveItem id: [{SurveyItems[itemID].ID}]");
|
|
|
+
|
|
|
+ int min = SurveyItems.Select(o => o.Value.Position).Min();
|
|
|
+ int max = SurveyItems.Select(o => o.Value.Position).Max();
|
|
|
+
|
|
|
+ Console.WriteLine($"min:{min}, max:{max}.");
|
|
|
+
|
|
|
+ int oldPosition, newPosition;
|
|
|
+ oldPosition = SurveyItems[itemID].Position;
|
|
|
+
|
|
|
+ int targetPosition = (oldPosition + step < min) ? min : oldPosition + step;
|
|
|
+ targetPosition = (oldPosition + step > max) ? max : oldPosition + step;
|
|
|
+
|
|
|
+ Console.WriteLine($"oldPosition:{oldPosition}, step:{step}, target:{targetPosition}.");
|
|
|
+
|
|
|
+ int targetKey = SurveyItems.FirstOrDefault(x => x.Value.Position == targetPosition).Key;
|
|
|
+ //SurveyItems.TryGetValue(targetPosition, out SurveyItem targetItem);
|
|
|
+ if (targetKey != 0)
|
|
|
+ {
|
|
|
+ newPosition = SurveyItems[targetKey].Position;
|
|
|
+ Console.WriteLine($"newPosition:{newPosition}.");
|
|
|
+
|
|
|
+ SurveyItems[itemID].Position = newPosition;
|
|
|
+ SurveyItems[targetKey].Position = oldPosition;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public class SurveyItem
|
|
|
+ public class SurveyItem : ICloneable
|
|
|
{
|
|
|
public int ID { get; set; }
|
|
|
public int SurveyID { get; set; }
|
|
@@ -75,6 +145,29 @@ namespace HyperCube.Models
|
|
|
public Dictionary<int, SurveyItemOption> SurveyItemOptions { get; set; } = new();
|
|
|
public int AnswerID { get; set; } /// SurveyItemOption selected ID
|
|
|
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 surveyitems (surveyid, itemtext, position, required, creatorid) " +
|
|
|
+ $"VALUES ({SurveyID}, '{Text}', {Position}, {Convert.ToInt32(Required)}, '{userID}')";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ stringSQL = $"UPDATE surveyitems " +
|
|
|
+ $"SET surveyid={SurveyID}, itemtext='{Text}', position={Position}, required={Convert.ToInt32(Required)}, deleted={Convert.ToInt32(IsDeleted)} " +
|
|
|
+ $"WHERE id={ID}";
|
|
|
+ }
|
|
|
+
|
|
|
+ newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
|
|
|
+ IsNew = false;
|
|
|
+ return ID = (int)newID;
|
|
|
+ }
|
|
|
|
|
|
public void AddNewOption(string userID)
|
|
|
{
|
|
@@ -93,9 +186,14 @@ namespace HyperCube.Models
|
|
|
};
|
|
|
SurveyItemOptions.Add(newTempKey, option);
|
|
|
}
|
|
|
+
|
|
|
+ public object Clone()
|
|
|
+ {
|
|
|
+ return MemberwiseClone();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public class SurveyItemOption
|
|
|
+ public class SurveyItemOption : ICloneable
|
|
|
{
|
|
|
public int ID { get; set; }
|
|
|
public int ItemID { get; set; }
|
|
@@ -109,5 +207,11 @@ namespace HyperCube.Models
|
|
|
public DateTime DateCreated { get; set; }
|
|
|
public DateTime DateUpdated { get; set; }
|
|
|
public bool IsNew { get; set; } = true;
|
|
|
+ public bool IsDeleted { get; set; } = false;
|
|
|
+
|
|
|
+ public object Clone()
|
|
|
+ {
|
|
|
+ return MemberwiseClone();
|
|
|
+ }
|
|
|
}
|
|
|
}
|