private static BaseStatDTO ExtractSingleStatFromResponse(JObject jObject) { //check for items JToken itemsToken; BaseStatDTO stat = null; if (jObject.TryGetValue("items", out itemsToken)) { //special case for stats that contains item objects stat = new GeneralStatWithItemsDTO() { Id = jObject["id"]?.ToString(), Title = jObject["title"]?.ToString(), VisualType = jObject["visualType"]?.ToString(), Notes = jObject["notes"]?.ToString(), CurrentIndex = jObject["currentIndex"] != null?int.Parse(jObject["currentIndex"].ToString()) : 0, LastUpdatedDateTime = jObject["lastUpdatedDateTime"] != null?DateTime.Parse(jObject["lastUpdatedDateTime"].ToString()) : (DateTime?)null, NotesLastUpdatedDateTime = jObject["notesLastUpdatedDateTime"] != null?DateTime.Parse(jObject["notesLastUpdatedDateTime"].ToString()) : (DateTime?)null, }; foreach (var valueItem in itemsToken) { if (valueItem is JValue) { ((GeneralStatWithItemsDTO)stat).Items.Add(new StatItemValueDTO() { Name = valueItem.ToString(), Value = valueItem.ToString() }); } else { ((GeneralStatWithItemsDTO)stat).Items.Add(new StatItemValueDTO() { Name = valueItem["name"]?.ToString(), Value = valueItem["value"]?.ToString(), Checked = valueItem["checked"] != null && bool.Parse(valueItem["checked"].ToString()) }); } } } else { stat = new GeneralStatDTO() { Id = jObject["id"]?.ToString(), Title = jObject["title"]?.ToString(), VisualType = jObject["visualType"]?.ToString(), Value = jObject["value"]?.ToString(), Notes = jObject["notes"]?.ToString(), CurrentIndex = jObject["currentIndex"] != null?int.Parse(jObject["currentIndex"].ToString()) : 0, LastUpdatedDateTime = jObject["lastUpdatedDateTime"] != null?DateTime.Parse(jObject["lastUpdatedDateTime"].ToString()) : (DateTime?)null, NotesLastUpdatedDateTime = jObject["notesLastUpdatedDateTime"] != null?DateTime.Parse(jObject["notesLastUpdatedDateTime"].ToString()) : (DateTime?)null, }; } stat.DynamicJsonIgnoreProperties = new[] { "visualType" }; return(stat); }
public async Task UpdateStatValue(StatXAuthDTO statXAuthDTO, string groupId, string statId, Dictionary <string, string> statValues, string title, string notes) { try { var uri = new Uri($"{StatXBaseApiUrl}/groups/{groupId}/stats/{statId}"); //get the stat and look for value var currentStat = await GetStat(statXAuthDTO, groupId, statId); if (currentStat != null) { if (string.IsNullOrEmpty(title)) { title = currentStat.Title; } string response = string.Empty; //process stat types that can update multiple items if (currentStat.VisualType == StatTypes.CheckList || currentStat.VisualType == StatTypes.HorizontalBars) { var statDTO = currentStat as GeneralStatWithItemsDTO; if (statDTO != null) { statDTO.LastUpdatedDateTime = DateTime.UtcNow; statDTO.NotesLastUpdatedDateTime = DateTime.UtcNow; statDTO.Title = title; statDTO.Notes = notes; var tempItems = new List <StatItemValueDTO>(); tempItems.AddRange(statDTO.Items); statDTO.Items.Clear(); if (currentStat.VisualType == StatTypes.CheckList) { statDTO.DynamicJsonIgnoreProperties = new string[] { "visualType", "value", "currentIndex" }; statDTO.Items.AddRange(statValues.Select(x => new StatItemValueDTO() { Name = x.Key, Checked = string.IsNullOrEmpty(x.Value) ? tempItems.FirstOrDefault(l => l.Name == x.Key).Checked : StatXUtilities.ConvertChecklistItemValue(x.Value) }).ToList()); } else { statDTO.DynamicJsonIgnoreProperties = new string[] { "visualType", "checked", "currentIndex" }; statDTO.Items.AddRange(statValues.Select(x => new StatItemValueDTO() { Name = x.Key, Value = string.IsNullOrEmpty(x.Value) ? tempItems.FirstOrDefault(l => l.Name == x.Key).Value : x.Value }).ToList()); } string json = JsonConvert.SerializeObject(statDTO, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new DynamicContractResolver(statDTO.DynamicJsonIgnoreProperties) }); response = await _restfulServiceClient.PutAsync(uri, (HttpContent) new StringContent(json), null, GetStatxAPIHeaders(statXAuthDTO)); } } else { var updateStatContent = new GeneralStatDTO { Title = title, Notes = notes, LastUpdatedDateTime = DateTime.UtcNow, NotesLastUpdatedDateTime = DateTime.UtcNow, }; if (currentStat.VisualType == StatTypes.PickList) { int currentIndex = 0; int.TryParse(statValues.First().Value, out currentIndex); updateStatContent.CurrentIndex = currentIndex; updateStatContent.DynamicJsonIgnoreProperties = new[] { "visualType", "value" }; } else { updateStatContent.Value = statValues.First().Value; updateStatContent.DynamicJsonIgnoreProperties = new[] { "visualType", "currentIndex" }; } string json = JsonConvert.SerializeObject(updateStatContent, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new DynamicContractResolver(updateStatContent.DynamicJsonIgnoreProperties) }); response = await _restfulServiceClient.PutAsync(uri, (HttpContent) new StringContent(json), null, GetStatxAPIHeaders(statXAuthDTO)); } var jObject = JObject.Parse(response); CheckForExistingErrors(jObject, true); } } catch (RestfulServiceException exception) { var jObject = JObject.Parse(exception.ResponseMessage); JToken errorsToken; if (jObject.TryGetValue("errors", out errorsToken)) { if (errorsToken is JArray) { var firstError = (JObject)errorsToken.First; throw new ApplicationException($"StatX request error: {firstError["message"]?.ToString()}"); } } } }