Functioning get requests

docker_db
Jukoga 2024-11-26 22:09:47 +01:00
parent 5428475717
commit 7e70ca87f6
4 changed files with 42 additions and 44 deletions

View File

@ -8,44 +8,44 @@ using Newtonsoft.Json;
namespace BHServer.Controllers
{
[Route("api/[controller]")]
public class AssaultTroopController : ControllerBase
public class AssaultTroopController(ApplicationDbContext db) : DBController(db)
{
private readonly ApplicationDbContext _db;
public AssaultTroopController(ApplicationDbContext db)
{
_db = db;
}
/*public IActionResult Index()
{
List<AssaultTroop> ObjectAssaultTroopList = _db.AssaultTroops.ToList();
// If the above line faulters then ensure SQL Server is running
return View(ObjectAssaultTroopList);
}*/
// GET /<APIController>/5
// GET: api/<APIController>
[HttpGet]
public string Get()
{
List<AssaultTroop> assaultTroopList = _db.assault_troop.ToList();
List<AssaultTroop> assaultTroopList = DB.assault_troop.ToList();
return JsonConvert.SerializeObject(assaultTroopList);
}
// GET /<APIController>/5
[HttpGet("{id}")]
public string Get(int id)
{
AssaultTroop assaultTroop = DB.assault_troop.ElementAt(id);
return JsonConvert.SerializeObject(assaultTroop);
}
// POST /<APIController>
[HttpPost]
public void Post([FromBody] string value)
{
//TODO: Implement
}
// PUT /<APIController>/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
//TODO: Implement
}
// DELETE /<APIController>/5
[HttpDelete("{id}")]
public void Delete(int id)
{
//TODO: Implement
}
}
}

View File

@ -0,0 +1,15 @@
using BHServer.Data;
using Microsoft.AspNetCore.Mvc;
namespace BHServer.Controllers
{
public class DBController : ControllerBase
{
protected readonly ApplicationDbContext DB;
public DBController(ApplicationDbContext db)
{
DB = db;
}
}
}

View File

@ -1,44 +1,26 @@
using Microsoft.AspNetCore.Mvc;
using System;
using BHServer.Data;
using Microsoft.AspNetCore.Mvc;
using BHServer.Models;
using Newtonsoft.Json;
namespace BHServer.Controllers
{
[Route("api/[controller]")]
public class ProvinceController : Controller
public class ProvinceController(ApplicationDbContext db) : DBController(db)
{
List<string> MockData = [
"{\"id\": 0, \"name\": \"Hessen\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [2, 3, 4], \"Axis\": [1, 5, 9, 10]}, \"state\": 1}",
"{\"id\": 1, \"name\": \"Thuringen\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [6, 7, 8]}, \"state\": 0}",
"{\"id\": 2, \"name\": \"Rheinland-Pfalz\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [13, 14], \"Axis\": [15]}, \"state\": 1}",
"{\"id\": 3, \"name\": \"Saarland\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [16, 17, 18], \"Axis\": []}, \"state\": 0}",
"{\"id\": 4, \"name\": \"Baden-Wurttemberg\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [17, 19], \"Axis\": []}, \"state\": 0}",
"{\"id\": 5, \"name\": \"Bayern\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [20, 21], \"Axis\": []}, \"state\": 0}",
"{\"id\": 6, \"name\": \"Sachsen\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [23], \"Axis\": []}, \"state\": 0}",
"{\"id\": 7, \"name\": \"Brandenburg\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [25, 26, 27, 28]}, \"state\": 0}",
"{\"id\": 8, \"name\": \"Berlin\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [29, 30, 31]}, \"state\": 0}",
"{\"id\": 9, \"name\": \"Schleswig-Holstein\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [33]}, \"state\": 0}",
"{\"id\": 10, \"name\": \"Hamburg\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [34]}, \"state\": 0}",
"{\"id\": 11, \"name\": \"Niedersachsen\", \"faction\": 2, \"mul\": 1, \"ats\": {\"Allies\": [], \"Axis\": [35]}, \"state\": 0}",
"{\"id\": 12, \"name\": \"Nordreihn-Westfalen\", \"faction\": 1, \"mul\": 1, \"ats\": {\"Allies\": [36, 37], \"Axis\": []}, \"state\": 0}"
];
public IActionResult Index()
{
return View();
}
// GET: api/<APIController>
[HttpGet]
public IEnumerable<string> Get()
public string Get()
{
return MockData;
List<Province> provinceList = DB.province.ToList();
return JsonConvert.SerializeObject(provinceList);
}
// GET /<APIController>/5
[HttpGet("{id}")]
public string Get(int id)
{
var entry = MockData.ElementAt(id);
return entry;
var province = DB.province.ElementAt(id);
return JsonConvert.SerializeObject(province);
}
// POST /<APIController>

View File

@ -10,6 +10,7 @@ CREATE TABLE assault_troop (
);
INSERT INTO assault_troop (id, name, faction, type, province, orders, owner) VALUES
(0, 'First Armor Division', 2, 1, 0, '{"Dep": [], "Mov": []}', 1),
(1, 'First Infantry Division', 2, 1, 0, '{"Dep": [], "Mov": []}', 1),
(2, 'First Infantry Division', 1, 1, 0, '{"Dep": [], "Mov": []}', 2),
(3, 'Second Infantry Division', 1, 1, 0, '{"Dep": [], "Mov": []}', 2),