When a user gets to a location, he will get a question. As such, I have a class for "Questions" and a class for "Locations". When I retrieve a location, however, the Question parameter is always null. This seems to be a problem with the whole project, as the same problem repeats itself somewhere else (here, a "Game" has a list of "Teams", but the teams are always empty).
Objects are created when database is initialized:
public static void Initialize(DBContext context)
{
context.Database.EnsureCreated();
if (!context.Games.Any())
{
var teams = new List<Team>();
var team1 = new Team()
{
TeamName = "Kwizmasterz",
TotalPoints = 0,
TotalBoobyTraps = 2
};
var team2 = new Team()
{
TeamName = "Xesennettet",
TotalPoints = 0,
TotalBoobyTraps = 2
};
teams.Add(team1);
teams.Add(team2);
var game = new Game()
{
GameCode = "X35H0",
team = teams
};
context.Games.Add(game);
context.SaveChanges();
}
if (!context.Locations.Any())
{
var que = new Question()
{
QuestionText = "How much is 2 + 2?",
Answer = "4",
IsSolved = false,
Points = 1000000
};
var loc = new Location()
{
LocationName = "LocationName",
Latitude = 50.2299036,
Longitude = 5.4163052,
Question = que,
IsBoobyTrapped = false
};
context.Locations.Add(loc);
context.SaveChanges();
}
}
Location Class:
public class Location
{
public int LocationID { get; set; }
public string LocationName { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public Question Question { get; set; }
public bool IsBoobyTrapped { get; set; }
public int VictorTeamID { get; set; } = -1;
}
Question Class:
public class Question
{
public int QuestionID { get; set; }
public int QuestionType { get; set; } // 1 = Question - Answer
public string QuestionText { get; set; }
public int Points { get; set; }
public bool IsSolved { get; set; }
public string Answer { get; set; }
}
Controller Class:
[Route("api/v1")]
public class GameController : Controller
{
private readonly DBContext context;
public GameController(DBContext context)
{
this.context = context;
}
public IActionResult Index()
{
return View();
}
[Route("location")]
[HttpPost]
public IActionResult postGame([FromBody] Location newLocation)
{
newLocation.LocationID = context.Games.Count();
context.Locations.Add(newLocation);
return Created("", newLocation);
}
[Route("location")]
[HttpGet]
public List<Location> getLocations()
{
return context.Locations.ToList();
}
[Route("location/{id}")]
[HttpGet]
public Location getLocation(int id)
{
int _id = id - 1;
List<Location> loc = context.Locations.ToList();
if (loc[_id] != null)
return loc[_id];
else
return null;
}
[Route("game")]
[HttpPost]
public IActionResult postGame([FromBody] Game newGame)
{
newGame.GameID = context.Games.Count();
context.Games.Add(newGame);
return Created("", newGame);
}
[Route("game")]
[HttpGet]
public List<Game> getGames()
{
return context.Games.ToList();
}
[Route("game/{id}")]
[HttpGet]
public Game getGame(int id)
{
List<Game> game = context.Games.ToList();
if (game[id] != null)
return game[id];
else
return null;
}
}