|  | 
| 6 | 6 | using Microsoft.AspNetCore.Mvc; | 
| 7 | 7 | using Newtonsoft.Json; | 
| 8 | 8 | 
 | 
| 9 |  | -namespace surveyjs_aspnet_mvc { | 
| 10 |  | - public class SessionStorage { | 
|  | 9 | +namespace surveyjs_aspnet_mvc | 
|  | 10 | +{ | 
|  | 11 | + public class SessionStorage | 
|  | 12 | + { | 
| 11 | 13 |  private ISession session; | 
| 12 | 14 | 
 | 
| 13 |  | - public SessionStorage(ISession session) { | 
|  | 15 | + public SessionStorage(ISession session) | 
|  | 16 | + { | 
| 14 | 17 |  this.session = session; | 
| 15 | 18 |  } | 
| 16 | 19 | 
 | 
| 17 |  | - public T GetFromSession<T>(string storageId, T defaultValue) { | 
| 18 |  | - if(string.IsNullOrEmpty(session.GetString(storageId))) { | 
|  | 20 | + public T GetFromSession<T>(string storageId, T defaultValue) | 
|  | 21 | + { | 
|  | 22 | + if (string.IsNullOrEmpty(session.GetString(storageId))) | 
|  | 23 | + { | 
| 19 | 24 |  session.SetString(storageId, JsonConvert.SerializeObject(defaultValue)); | 
| 20 | 25 |  } | 
| 21 | 26 |  var value = session.GetString(storageId); | 
| 22 |  | - return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);  | 
|  | 27 | + return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value); | 
| 23 | 28 |  } | 
| 24 | 29 | 
 | 
| 25 |  | - public Dictionary<string, string> GetSurveys() { | 
|  | 30 | + public Dictionary<string, string> GetSurveys() | 
|  | 31 | + { | 
| 26 | 32 |  Dictionary<string, string> surveys = new Dictionary<string, string>(); | 
| 27 | 33 |  surveys["MySurvey1"] = @"{ | 
| 28 | 34 |  ""pages"": [ | 
| @@ -63,37 +69,52 @@ public Dictionary<string, string> GetSurveys() { | 
| 63 | 69 |  return GetFromSession<Dictionary<string, string>>("SurveyStorage", surveys); | 
| 64 | 70 |  } | 
| 65 | 71 | 
 | 
| 66 |  | - public Dictionary<string, List<string>> GetResults() { | 
|  | 72 | + public Dictionary<string, List<string>> GetResults() | 
|  | 73 | + { | 
| 67 | 74 |  Dictionary<string, List<string>> results = new Dictionary<string, List<string>>(); | 
| 68 | 75 |  return GetFromSession<Dictionary<string, List<string>>>("ResultsStorage", results); | 
| 69 | 76 |  } | 
| 70 | 77 | 
 | 
| 71 |  | - public string GetSurvey(string surveyId) { | 
|  | 78 | + public string GetSurvey(string surveyId) | 
|  | 79 | + { | 
| 72 | 80 |  return GetSurveys()[surveyId]; | 
| 73 | 81 |  } | 
| 74 | 82 | 
 | 
| 75 |  | - public void StoreSurvey(string surveyId, string jsonString) { | 
|  | 83 | + public void StoreSurvey(string surveyId, string jsonString) | 
|  | 84 | + { | 
| 76 | 85 |  var storage = GetSurveys(); | 
| 77 | 86 |  storage[surveyId] = jsonString; | 
| 78 | 87 |  session.SetString("SurveyStorage", JsonConvert.SerializeObject(storage)); | 
| 79 | 88 |  } | 
| 80 | 89 | 
 | 
| 81 |  | - public void DeleteSurvey(string surveyId) { | 
|  | 90 | + public void ChangeName(string id, string name) | 
|  | 91 | + { | 
|  | 92 | + var storage = GetSurveys(); | 
|  | 93 | + storage[name] = storage[id]; | 
|  | 94 | + storage.Remove(id); | 
|  | 95 | + session.SetString("SurveyStorage", JsonConvert.SerializeObject(storage)); | 
|  | 96 | + } | 
|  | 97 | + | 
|  | 98 | + public void DeleteSurvey(string surveyId) | 
|  | 99 | + { | 
| 82 | 100 |  var storage = GetSurveys(); | 
| 83 | 101 |  storage.Remove(surveyId); | 
| 84 | 102 |  session.SetString("SurveyStorage", JsonConvert.SerializeObject(storage)); | 
| 85 | 103 |  } | 
| 86 | 104 | 
 | 
| 87 |  | - public void PostResults(string postId, string resultJson) { | 
|  | 105 | + public void PostResults(string postId, string resultJson) | 
|  | 106 | + { | 
| 88 | 107 |  var storage = GetResults(); | 
| 89 |  | - if(!storage.ContainsKey(postId)) { | 
|  | 108 | + if (!storage.ContainsKey(postId)) | 
|  | 109 | + { | 
| 90 | 110 |  storage[postId] = new List<string>(); | 
| 91 | 111 |  } | 
| 92 | 112 |  storage[postId].Add(resultJson); | 
| 93 | 113 |  session.SetString("ResultsStorage", JsonConvert.SerializeObject(storage)); | 
| 94 | 114 |  } | 
| 95 | 115 | 
 | 
| 96 |  | - public List<string> GetResults(string postId) { | 
|  | 116 | + public List<string> GetResults(string postId) | 
|  | 117 | + { | 
| 97 | 118 |  var storage = GetResults(); | 
| 98 | 119 |  return storage.ContainsKey(postId) ? storage[postId] : new List<string>(); | 
| 99 | 120 |  } | 
|  | 
0 commit comments