Skip to content

Commit

Permalink
Implemented "change name" functionality (+ prettier :( )
Browse files Browse the repository at this point in the history
  • Loading branch information
tsv2013 committed Feb 21, 2018
1 parent b572543 commit 222fe23
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
49 changes: 35 additions & 14 deletions Code/SessionStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace surveyjs_aspnet_mvc {
public class SessionStorage {
namespace surveyjs_aspnet_mvc
{
public class SessionStorage
{
private ISession session;

public SessionStorage(ISession session) {
public SessionStorage(ISession session)
{
this.session = session;
}

public T GetFromSession<T>(string storageId, T defaultValue) {
if(string.IsNullOrEmpty(session.GetString(storageId))) {
public T GetFromSession<T>(string storageId, T defaultValue)
{
if (string.IsNullOrEmpty(session.GetString(storageId)))
{
session.SetString(storageId, JsonConvert.SerializeObject(defaultValue));
}
var value = session.GetString(storageId);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}

public Dictionary<string, string> GetSurveys() {
public Dictionary<string, string> GetSurveys()
{
Dictionary<string, string> surveys = new Dictionary<string, string>();
surveys["MySurvey1"] = @"{
""pages"": [
Expand Down Expand Up @@ -63,37 +69,52 @@ public Dictionary<string, string> GetSurveys() {
return GetFromSession<Dictionary<string, string>>("SurveyStorage", surveys);
}

public Dictionary<string, List<string>> GetResults() {
public Dictionary<string, List<string>> GetResults()
{
Dictionary<string, List<string>> results = new Dictionary<string, List<string>>();
return GetFromSession<Dictionary<string, List<string>>>("ResultsStorage", results);
}

public string GetSurvey(string surveyId) {
public string GetSurvey(string surveyId)
{
return GetSurveys()[surveyId];
}

public void StoreSurvey(string surveyId, string jsonString) {
public void StoreSurvey(string surveyId, string jsonString)
{
var storage = GetSurveys();
storage[surveyId] = jsonString;
session.SetString("SurveyStorage", JsonConvert.SerializeObject(storage));
}

public void DeleteSurvey(string surveyId) {
public void ChangeName(string id, string name)
{
var storage = GetSurveys();
storage[name] = storage[id];
storage.Remove(id);
session.SetString("SurveyStorage", JsonConvert.SerializeObject(storage));
}

public void DeleteSurvey(string surveyId)
{
var storage = GetSurveys();
storage.Remove(surveyId);
session.SetString("SurveyStorage", JsonConvert.SerializeObject(storage));
}

public void PostResults(string postId, string resultJson) {
public void PostResults(string postId, string resultJson)
{
var storage = GetResults();
if(!storage.ContainsKey(postId)) {
if (!storage.ContainsKey(postId))
{
storage[postId] = new List<string>();
}
storage[postId].Add(resultJson);
session.SetString("ResultsStorage", JsonConvert.SerializeObject(storage));
}

public List<string> GetResults(string postId) {
public List<string> GetResults(string postId)
{
var storage = GetResults();
return storage.ContainsKey(postId) ? storage[postId] : new List<string>();
}
Expand Down
8 changes: 8 additions & 0 deletions Controllers/ServiceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public JsonResult Create(string name)
return Json("Ok");
}

[HttpGet("changeName")]
public JsonResult ChangeName(string id, string name)
{
var db = new SessionStorage(HttpContext.Session);
db.ChangeName(id, name);
return Json("Ok");
}

[HttpPost("changeJson")]
public string ChangeJson([FromBody]ChangeSurveyModel model)
{
Expand Down

0 comments on commit 222fe23

Please sign in to comment.