Skip to content

Commit 222fe23

Browse files
committed
Implemented "change name" functionality (+ prettier :( )
1 parent b572543 commit 222fe23

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

Code/SessionStorage.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@
66
using Microsoft.AspNetCore.Mvc;
77
using Newtonsoft.Json;
88

9-
namespace surveyjs_aspnet_mvc {
10-
public class SessionStorage {
9+
namespace surveyjs_aspnet_mvc
10+
{
11+
public class SessionStorage
12+
{
1113
private ISession session;
1214

13-
public SessionStorage(ISession session) {
15+
public SessionStorage(ISession session)
16+
{
1417
this.session = session;
1518
}
1619

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+
{
1924
session.SetString(storageId, JsonConvert.SerializeObject(defaultValue));
2025
}
2126
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);
2328
}
2429

25-
public Dictionary<string, string> GetSurveys() {
30+
public Dictionary<string, string> GetSurveys()
31+
{
2632
Dictionary<string, string> surveys = new Dictionary<string, string>();
2733
surveys["MySurvey1"] = @"{
2834
""pages"": [
@@ -63,37 +69,52 @@ public Dictionary<string, string> GetSurveys() {
6369
return GetFromSession<Dictionary<string, string>>("SurveyStorage", surveys);
6470
}
6571

66-
public Dictionary<string, List<string>> GetResults() {
72+
public Dictionary<string, List<string>> GetResults()
73+
{
6774
Dictionary<string, List<string>> results = new Dictionary<string, List<string>>();
6875
return GetFromSession<Dictionary<string, List<string>>>("ResultsStorage", results);
6976
}
7077

71-
public string GetSurvey(string surveyId) {
78+
public string GetSurvey(string surveyId)
79+
{
7280
return GetSurveys()[surveyId];
7381
}
7482

75-
public void StoreSurvey(string surveyId, string jsonString) {
83+
public void StoreSurvey(string surveyId, string jsonString)
84+
{
7685
var storage = GetSurveys();
7786
storage[surveyId] = jsonString;
7887
session.SetString("SurveyStorage", JsonConvert.SerializeObject(storage));
7988
}
8089

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+
{
82100
var storage = GetSurveys();
83101
storage.Remove(surveyId);
84102
session.SetString("SurveyStorage", JsonConvert.SerializeObject(storage));
85103
}
86104

87-
public void PostResults(string postId, string resultJson) {
105+
public void PostResults(string postId, string resultJson)
106+
{
88107
var storage = GetResults();
89-
if(!storage.ContainsKey(postId)) {
108+
if (!storage.ContainsKey(postId))
109+
{
90110
storage[postId] = new List<string>();
91111
}
92112
storage[postId].Add(resultJson);
93113
session.SetString("ResultsStorage", JsonConvert.SerializeObject(storage));
94114
}
95115

96-
public List<string> GetResults(string postId) {
116+
public List<string> GetResults(string postId)
117+
{
97118
var storage = GetResults();
98119
return storage.ContainsKey(postId) ? storage[postId] : new List<string>();
99120
}

Controllers/ServiceController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public JsonResult Create(string name)
4646
return Json("Ok");
4747
}
4848

49+
[HttpGet("changeName")]
50+
public JsonResult ChangeName(string id, string name)
51+
{
52+
var db = new SessionStorage(HttpContext.Session);
53+
db.ChangeName(id, name);
54+
return Json("Ok");
55+
}
56+
4957
[HttpPost("changeJson")]
5058
public string ChangeJson([FromBody]ChangeSurveyModel model)
5159
{

0 commit comments

Comments
 (0)