Exemple #1
0
        public IActionResult PostNetWorth([FromBody] NetWorthDto dto)
        {
            var result = dto;

            lock (_lock)
            {
                if (_netValues is null)
                {
                    dto.Result = new Tuple <bool, string>(false, "Net Worth Tracker was not initiated. Please load it first.");
                }
                else if (_netValues.Version > dto.Version)
                {
                    dto.Result = new Tuple <bool, string>(false, "The Net Worth Tracker had been updated earlier. Please reload and make your changes again.");
                }
                else if (!ModelState.IsValid)
                {
                    PopulateErrors(dto);
                    dto.Result = new Tuple <bool, string>(false, "There are errors below.");
                }
                else
                {
                    _netWorthTracking.ProcessNewLines(dto);
                    dto.Result = new Tuple <bool, string>(true, "The Net Worth Tracker has been successfully updated.");
                    _netValues = dto;
                    result     = dto;
                }
            }

            return(Content(JsonConvert.SerializeObject(result), "application/json"));
        }
Exemple #2
0
        private void PopulateErrors(NetWorthDto dto)
        {
            // Clear previous error messages
            _netWorthTracking.ClearErrorMessages(dto);
            var messagePropName = "Message";

            foreach (var k in ModelState)
            {
                var    errorMessage = String.Join(" - ", k.Value.Errors.ToList().Select(t => t.ErrorMessage));
                object curObj       = dto;
                var    propPathList = k.Key.Split('.').ToList();
                propPathList = propPathList.Take(propPathList.Count - 1).ToList();
                foreach (var propertyNameCombo in propPathList)
                {
                    var openBracketIndex  = propertyNameCombo.IndexOf('[');
                    var closeBracketIndex = propertyNameCombo.IndexOf(']');
                    if (openBracketIndex >= 0)
                    {
                        var propertyName = propertyNameCombo.Substring(0, openBracketIndex);
                        var index        = Int32.Parse(propertyNameCombo.Substring(openBracketIndex + 1, closeBracketIndex - openBracketIndex - 1));
                        curObj = ((IList)curObj.GetType().GetProperty(propertyName).GetValue(curObj, null))[index];
                    }
                    else
                    {
                        var propertyName = propertyNameCombo;
                        curObj = curObj.GetType().GetProperty(propertyName).GetValue(curObj, null);
                    }
                }
                curObj.GetType().GetProperty(messagePropName).SetValue(curObj, errorMessage);
            }
        }
Exemple #3
0
 public IActionResult GetNetWorth()
 {
     if (_netValues is null)
     {
         lock (_lock)
         {
             _netValues = new NetWorthDto()
             {
                 Items   = _netWorthTracking.GetInitialData(_configuration.GetSection("InitialData")),
                 Version = 1,
                 Result  = new Tuple <bool, string>(true, "")
             };
         }
     }
     _netValues.Result = new Tuple <bool, string>(true, "");
     return(Content(JsonConvert.SerializeObject(_netValues), "application/json"));
 }
Exemple #4
0
        public void NetWorthTrackingTest()
        {
            var config = GetConfigBuilder();
            var netWorthTrackingServ = new NetWorthTracking();
            var lines       = netWorthTrackingServ.GetInitialData(config.GetSection("InitialData"));
            var newLines    = netWorthTrackingServ.GetInitialData(config.GetSection("InitialData"));
            var netWorthDto = new NetWorthDto()
            {
                Items   = newLines,
                Version = 1,
                Result  = new Tuple <bool, string>(true, "")
            };

            netWorthTrackingServ.ProcessNewLines(netWorthDto);
            Assert.True(lines.Count == netWorthDto.Items.Count);
            var result = true;

            for (var i = 0; i < lines.Count; i++)
            {
                result = result && lines[i].Equals(netWorthDto.Items[i]);
            }
            Assert.True(result);
        }