Survey.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Console = HyperCube.Utils.AdvConsole;
  6. namespace HyperCube.Models
  7. {
  8. public class Survey
  9. {
  10. public int ID { get; set; }
  11. public int EvenID { get; set; }
  12. public string Name { get; set; }
  13. public string Description { get; set; }
  14. public DateTime DateCreated { get; set; }
  15. public DateTime DateUpdated { get; set; }
  16. public string CreatorID { get; set; }
  17. public Dictionary<int, SurveyItem> SurveyItems { get; set; } = new();
  18. public bool IsNew { get; set; } = true;
  19. public bool IsDeleted { get; set; } = false;
  20. public async Task<int> Save(string userID)
  21. {
  22. long newID;
  23. string stringSQL;
  24. if (IsNew)
  25. {
  26. stringSQL = $"INSERT INTO surveys (eventid, name, description, creatorid) " +
  27. $"VALUES ({EvenID}, '{Name}', '{Description}', '{userID}')";
  28. }
  29. else
  30. {
  31. stringSQL = $"UPDATE surveys " +
  32. $"SET eventid={EvenID}, name='{Name}', description='{Description}', deleted={Convert.ToInt32(IsDeleted)} " +
  33. $"WHERE id={ID}";
  34. }
  35. newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
  36. IsNew = false;
  37. /// saving survey items
  38. int newItemID;
  39. Dictionary<int, SurveyItem> tmpSurveyItems = new();
  40. foreach (var item in SurveyItems)
  41. {
  42. if (newID != 0)
  43. item.Value.SurveyID = (int)newID;
  44. newItemID = await item.Value.Save(userID);
  45. if (newItemID != 0 && item.Key != newItemID)
  46. {
  47. Console.WriteLine($"Adding and updating item id:{item.Key} with new id:{newItemID}");
  48. tmpSurveyItems.Add(newItemID, item.Value);
  49. }
  50. else
  51. {
  52. Console.WriteLine($"Adding existed item id:{item.Key}");
  53. tmpSurveyItems.Add(item.Key, item.Value);
  54. }
  55. /// saving survey item options
  56. int newOptionID;
  57. Dictionary<int, SurveyItemOption> tmpSurveyItemOption = new();
  58. foreach (var option in item.Value.SurveyItemOptions)
  59. {
  60. if (newItemID != 0)
  61. option.Value.ItemID = newItemID;
  62. newOptionID = await option.Value.Save(userID);
  63. if (newOptionID != 0 && option.Key != newOptionID)
  64. {
  65. Console.WriteLine($"Adding and updating option id:{option.Key} with new id:{newOptionID}");
  66. tmpSurveyItemOption.Add(newOptionID, option.Value);
  67. }
  68. else
  69. {
  70. Console.WriteLine($"Adding existed option id:{option.Key}");
  71. tmpSurveyItemOption.Add(option.Key, option.Value);
  72. }
  73. }
  74. /// updating options collection with actual IDs from DB
  75. if (tmpSurveyItemOption.Count > 0)
  76. {
  77. item.Value.SurveyItemOptions.Clear();
  78. item.Value.SurveyItemOptions = tmpSurveyItemOption.ToDictionary(entry => entry.Key, entry => (SurveyItemOption)entry.Value.Clone());
  79. }
  80. }
  81. /// updating items collection with actual IDs from DB
  82. if (tmpSurveyItems.Count > 0)
  83. {
  84. SurveyItems.Clear();
  85. SurveyItems = tmpSurveyItems.ToDictionary(entry => entry.Key, entry => (SurveyItem)entry.Value.Clone());
  86. }
  87. return ID = (int)newID;
  88. }
  89. public void AddNewItem(string userID)
  90. {
  91. int surveyID = ID;
  92. Console.WriteLine($"Survey AddNewItem. survey id={surveyID}.");
  93. int newTempKey = (SurveyItems.Count > 0) ? SurveyItems.Keys.Max() + 1 : 1;
  94. int position = (SurveyItems.Count > 0) ? SurveyItems.Select(o => o.Value.Position).Max() + 1 : 1;
  95. Console.WriteLine($"Survey AddNewItem. item newTempKey={newTempKey}.");
  96. SurveyItem item = new()
  97. {
  98. ID = newTempKey,
  99. SurveyID = surveyID,
  100. Position = position,
  101. CreatorID = userID,
  102. DateCreated = DateTime.Now
  103. };
  104. SurveyItems.Add(newTempKey, item);
  105. }
  106. public void DeleteItem(int itemID)
  107. {
  108. if (SurveyItems.Count > 0 && SurveyItems.ContainsKey(itemID))
  109. {
  110. Console.WriteLine($"Survey [{ID}] DeleteItem id:{SurveyItems[itemID].ID} - isnew:{SurveyItems[itemID].IsNew}");
  111. if (SurveyItems[itemID].IsNew)
  112. SurveyItems.Remove(itemID);
  113. else
  114. SurveyItems[itemID].IsDeleted = true;
  115. }
  116. }
  117. public void MoveItem(int itemID, int step)
  118. {
  119. if (SurveyItems.Count > 0 && SurveyItems.ContainsKey(itemID))
  120. {
  121. Console.WriteLine($"Survey:[{ID}] MoveItem id: [{SurveyItems[itemID].ID}]");
  122. int min = SurveyItems.Select(o => o.Value.Position).Min();
  123. int max = SurveyItems.Select(o => o.Value.Position).Max();
  124. Console.WriteLine($"min:{min}, max:{max}.");
  125. int oldPosition, newPosition;
  126. oldPosition = SurveyItems[itemID].Position;
  127. int targetPosition = (oldPosition + step < min) ? min : oldPosition + step;
  128. targetPosition = (oldPosition + step > max) ? max : oldPosition + step;
  129. Console.WriteLine($"oldPosition:{oldPosition}, step:{step}, target:{targetPosition}.");
  130. int targetKey = SurveyItems.FirstOrDefault(x => x.Value.Position == targetPosition).Key;
  131. if (targetKey != 0)
  132. {
  133. newPosition = SurveyItems[targetKey].Position;
  134. Console.WriteLine($"newPosition:{newPosition}.");
  135. SurveyItems[itemID].Position = newPosition;
  136. SurveyItems[targetKey].Position = oldPosition;
  137. }
  138. }
  139. }
  140. }
  141. public class SurveyItem : ICloneable
  142. {
  143. public int ID { get; set; }
  144. public int SurveyID { get; set; }
  145. public string Text { get; set; }
  146. public int Position { get; set; }
  147. public string CreatorID { get; set; }
  148. public DateTime DateCreated { get; set; }
  149. public DateTime DateUpdated { get; set; }
  150. public bool Required { get; set; } = true;
  151. public Dictionary<int, SurveyItemOption> SurveyItemOptions { get; set; } = new();
  152. public int AnswerID { get; set; } /// SurveyItemOption selected ID
  153. public bool IsNew { get; set; } = true;
  154. public bool IsDeleted { get; set; } = false;
  155. public async Task<int> Save(string userID)
  156. {
  157. long newID;
  158. string stringSQL;
  159. if (IsNew)
  160. {
  161. stringSQL = $"INSERT INTO surveyitems (surveyid, itemtext, position, required, creatorid) " +
  162. $"VALUES ({SurveyID}, '{Text}', {Position}, {Convert.ToInt32(Required)}, '{userID}')";
  163. }
  164. else
  165. {
  166. stringSQL = $"UPDATE surveyitems " +
  167. $"SET surveyid={SurveyID}, itemtext='{Text}', position={Position}, required={Convert.ToInt32(Required)}, deleted={Convert.ToInt32(IsDeleted)} " +
  168. $"WHERE id={ID}";
  169. }
  170. newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
  171. IsNew = false;
  172. return ID = (int)newID;
  173. }
  174. public void AddNewOption(string userID)
  175. {
  176. int itemID = ID;
  177. Console.WriteLine($"SurveyItem AddNewOption. item id={itemID}.");
  178. int newTempKey = (SurveyItemOptions.Count > 0) ? SurveyItemOptions.Keys.Max() + 1 : 1;
  179. int position = (SurveyItemOptions.Count > 0) ? SurveyItemOptions.Select(o => o.Value.Position).Max() + 1 : 1;
  180. Console.WriteLine($"SurveyItem AddNewOption. option newTempKey={newTempKey}.");
  181. SurveyItemOption option = new()
  182. {
  183. ID = newTempKey,
  184. ItemID = itemID,
  185. Position = position,
  186. CreatorID = userID,
  187. DateCreated = DateTime.Now
  188. };
  189. SurveyItemOptions.Add(newTempKey, option);
  190. }
  191. public void DeleteOption(int optionID)
  192. {
  193. if (SurveyItemOptions.Count > 0 && SurveyItemOptions.ContainsKey(optionID))
  194. {
  195. Console.WriteLine($"Survey [{SurveyID}] Item [{ID}] DeleteItem id:{SurveyItemOptions[optionID].ID} - isnew:{SurveyItemOptions[optionID].IsNew}");
  196. if (SurveyItemOptions[optionID].IsNew)
  197. SurveyItemOptions.Remove(optionID);
  198. else
  199. SurveyItemOptions[optionID].IsDeleted = true;
  200. }
  201. }
  202. public void MoveItem(int optionID, int step)
  203. {
  204. if (SurveyItemOptions.Count > 0 && SurveyItemOptions.ContainsKey(optionID))
  205. {
  206. Console.WriteLine($"Survey [{SurveyID}] Item [{ID}] MoveOption id:{SurveyItemOptions[optionID].ID}");
  207. }
  208. }
  209. public object Clone()
  210. {
  211. return MemberwiseClone();
  212. }
  213. }
  214. public class SurveyItemOption : ICloneable
  215. {
  216. public int ID { get; set; }
  217. public int ItemID { get; set; }
  218. public string Text { get; set; }
  219. public int Rate1 { get; set; }
  220. public int Rate2 { get; set; }
  221. public int Rate3 { get; set; }
  222. public int Rate4 { get; set; }
  223. public int Position { get; set; }
  224. public string CreatorID { get; set; }
  225. public DateTime DateCreated { get; set; }
  226. public DateTime DateUpdated { get; set; }
  227. public bool IsNew { get; set; } = true;
  228. public bool IsDeleted { get; set; } = false;
  229. public async Task<int> Save(string userID)
  230. {
  231. long newID;
  232. string stringSQL;
  233. if (IsNew)
  234. {
  235. stringSQL = $"INSERT INTO surveyitemoptions (itemid, optiontext, position, rate1, rate2, rate3, rate4, creatorid) " +
  236. $"VALUES ({ItemID}, '{Text}', {Position}, {Rate1}, {Rate2}, {Rate3}, {Rate4}, '{userID}')";
  237. }
  238. else
  239. {
  240. stringSQL = $"UPDATE surveyitemoptions " +
  241. $"SET itemid={ItemID}, optiontext='{Text}', position={Position}, rate1={Rate1}, rate2={Rate2}, rate3={Rate3}, rate4={Rate4}, deleted={Convert.ToInt32(IsDeleted)} " +
  242. $"WHERE id={ID}";
  243. }
  244. newID = await MySQLConnector.Instance().SQLInsert(stringSQL);
  245. IsNew = false;
  246. return ID = (int)newID;
  247. }
  248. public object Clone()
  249. {
  250. return MemberwiseClone();
  251. }
  252. }
  253. }