E
E
evgemiil2017-06-09 09:59:59
ASP.NET
evgemiil, 2017-06-09 09:59:59

json stats method not executing?

Please help me figure it out, I don’t ask me to do it, I ask you to figure it out, understand the problem and solve it. There is an MVC project, a game project, here is the code of the HomeController file

using Newtonsoft.Json;
using System.Web.Mvc;
using WebApplication.Models;
using XO;


namespace WebApplication.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new StatisticsModels());
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

         [HttpPost]
        public bool SaveGameInfo([System.Web.Http.FromBody]dynamic stat)
        {
            var newStat = JsonConvert.DeserializeObject<Statistcs>(stat.StatisticsList);
            return StatisticsModels.AddStatistics(newStat);
        }

        [HttpGet]
        public string GetGameInfo()
        {
            return JsonConvert.SerializeObject(new StatisticsModels().StatisticsList);
        }

    }
}

here is the StatisticsModels code
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using XO;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication.Models
{
    public class StatisticsModels
    {
        public List<Statistcs> StatisticsList { get; set; }

        public StatisticsModels()
        {
            var jsonData = File.ReadAllText(@"C:\Users\EvgenieL\Source\Repos\XO\XO\XO\bin\Debug\stats.json");
            StatisticsList = JsonConvert.DeserializeObject<List<Statistcs>>(jsonData);

        }

        public static bool AddStatistics(Statistcs stat)
        {
            //var conn = new SqlConnection(@"Data Source=localhost;Initial Catalog=XO;Integrated Security=true");
            //var command = new SqlCommand();
            //command.Connection = conn;
            //command.CommandType = CommandType.Text;
            //command.CommandText = $"INSERT INTO Statistic(Date, Result, StepCounter, UserFirst) VALUES('{stat.Date}', '{stat.Result}', {stat.StepCounter}, '{stat.UserFirst}')";

            //try
            //{
            //    conn.Open();
            //    command.ExecuteNonQuery();
            //}
            //catch
            //{
            //    return false;
            //}
            //finally
            //{
            //    conn.Close();
            //}

            //return true;

            try
            {
                var filePath = "stats.json";

                var jsonData = File.ReadAllText(filePath);

                var statisticsList = JsonConvert.DeserializeObject<List<Statistcs>>(jsonData)
                                        ?? new List<Statistcs>();

                statisticsList.Add(stat);

                jsonData = JsonConvert.SerializeObject(statisticsList);
                File.WriteAllText(filePath, jsonData);
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}

The problem is that the AddStatistics method is not executed, I looked at the breakpoints, I could not decide, I do not understand why it is not executed. To make it clearer, here is a link to the github https://github.com/evgeniel/XO

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Eremin, 2017-06-09
@EreminD

  1. Is something written in newStat as a result?
  2. AddStatistics method fails
    - What means? Not called? Passes through it and there is no result? Is an error thrown? Does it come out somewhere in the middle?
  3. In the AddStatistics method, the list from the file is written to the statisticsList?
  4. Make a temporarily intelligible handler. Look at the stack trace if an exception is thrown
    catch
                {
                    return false;
                }

D
Dmitry Kovalsky, 2017-06-09
@dmitryKovalskiy

var jsonData = File.ReadAllText(@"C:\Users\EvgenieL\Source\Repos\XO\XO\XO\bin\Debug\stats.json");
-Very bad idea. Just unrealistically bad hardcode.
1) Add logging to the catch block (whatever, it is better to use a ready-made solution like log4net or Nlog).
2)
var filePath = "stats.json";
var jsonData = File.ReadAllText(filePath);

Attention to the question - what and where are you reading here?
You have a very bad layer of business logic that will work in a sugary white scenario, but if you deviate from this scenario, there will be an error that you will never catch.
Write a script when it was not possible to deserialize,
when it was not possible to save.
At least catch different exceptions, and don't mute them all at once, pretending that errors are part of your business logic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question