Skip to content

Commit f366e92

Browse files
committed
Added "store results" functionality
1 parent 61ef508 commit f366e92

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Code/SessionStorage.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public Dictionary<string, string> GetSurveys() {
6363
return GetFromSession<Dictionary<string, string>>("SurveyStorage", surveys);
6464
}
6565

66+
public Dictionary<string, List<string>> GetResults() {
67+
Dictionary<string, List<string>> results = new Dictionary<string, List<string>>();
68+
return GetFromSession<Dictionary<string, List<string>>>("ResultsStorage", results);
69+
}
70+
6671
public string GetSurvey(string surveyId) {
6772
return GetSurveys()[surveyId];
6873
}
@@ -78,5 +83,19 @@ public void DeleteSurvey(string surveyId) {
7883
storage.Remove(surveyId);
7984
session.SetString("SurveyStorage", JsonConvert.SerializeObject(storage));
8085
}
86+
87+
public void PostResults(string postId, string resultJson) {
88+
var storage = GetResults();
89+
if(!storage.ContainsKey(postId)) {
90+
storage[postId] = new List<string>();
91+
}
92+
storage[postId].Add(resultJson);
93+
session.SetString("ResultsStorage", JsonConvert.SerializeObject(storage));
94+
}
95+
96+
public List<string> GetResults(string postId) {
97+
var storage = GetResults();
98+
return storage.ContainsKey(postId) ? storage[postId] : new List<string>();
99+
}
81100
}
82101
}

Controllers/ServiceController.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public class ChangeSurveyModel {
1212
public string Text { get; set; }
1313
}
1414

15+
public class PostSurveyResultModel {
16+
public string postId { get; set; }
17+
public string surveyResult { get; set; }
18+
}
19+
1520
[Route("api/[controller]")]
1621
public class ServiceController : Controller {
1722

@@ -48,6 +53,19 @@ public JsonResult Delete(string id) {
4853
return Json("Ok");
4954
}
5055

56+
[HttpPost("post")]
57+
public JsonResult PostResult([FromBody]PostSurveyResultModel model) {
58+
var db = new SessionStorage(HttpContext.Session);
59+
db.PostResults(model.postId, model.surveyResult);
60+
return Json("Ok");
61+
}
62+
63+
[HttpGet("results")]
64+
public JsonResult GetResults(string postId) {
65+
var db = new SessionStorage(HttpContext.Session);
66+
return Json(db.GetResults(postId));
67+
}
68+
5169
// // GET api/values/5
5270
// [HttpGet("{id}")]
5371
// public string Get(int id)

0 commit comments

Comments
 (0)