From 7e70ca87f658df25327a1395be3c3ddf25c5d331 Mon Sep 17 00:00:00 2001 From: Jukoga Date: Tue, 26 Nov 2024 22:09:47 +0100 Subject: [PATCH] Functioning get requests --- Controllers/AssaultTroopController.cs | 30 ++++++++++---------- Controllers/DBController.cs | 15 ++++++++++ Controllers/ProvinceController.cs | 40 ++++++++------------------- PostgressDB/init.sql | 1 + 4 files changed, 42 insertions(+), 44 deletions(-) create mode 100644 Controllers/DBController.cs diff --git a/Controllers/AssaultTroopController.cs b/Controllers/AssaultTroopController.cs index e787c44..d152862 100644 --- a/Controllers/AssaultTroopController.cs +++ b/Controllers/AssaultTroopController.cs @@ -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 ObjectAssaultTroopList = _db.AssaultTroops.ToList(); - // If the above line faulters then ensure SQL Server is running - return View(ObjectAssaultTroopList); - }*/ - - // GET //5 + + // GET: api/ [HttpGet] public string Get() { - List assaultTroopList = _db.assault_troop.ToList(); + List assaultTroopList = DB.assault_troop.ToList(); return JsonConvert.SerializeObject(assaultTroopList); } + + // GET //5 + [HttpGet("{id}")] + public string Get(int id) + { + AssaultTroop assaultTroop = DB.assault_troop.ElementAt(id); + return JsonConvert.SerializeObject(assaultTroop); + } // POST / [HttpPost] public void Post([FromBody] string value) { + //TODO: Implement } // PUT //5 [HttpPut("{id}")] public void Put(int id, [FromBody] string value) { + //TODO: Implement } // DELETE //5 [HttpDelete("{id}")] public void Delete(int id) { + //TODO: Implement } } } diff --git a/Controllers/DBController.cs b/Controllers/DBController.cs new file mode 100644 index 0000000..9d7e749 --- /dev/null +++ b/Controllers/DBController.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Controllers/ProvinceController.cs b/Controllers/ProvinceController.cs index 8ad6964..f6cba51 100644 --- a/Controllers/ProvinceController.cs +++ b/Controllers/ProvinceController.cs @@ -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 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/ [HttpGet] - public IEnumerable Get() + public string Get() { - return MockData; + List provinceList = DB.province.ToList(); + return JsonConvert.SerializeObject(provinceList); } // GET //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 / diff --git a/PostgressDB/init.sql b/PostgressDB/init.sql index 1b1d46d..fa1ca03 100644 --- a/PostgressDB/init.sql +++ b/PostgressDB/init.sql @@ -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),