From 7ff304dadf68157c22e89a3fcce049bd1cfa4fe0 Mon Sep 17 00:00:00 2001 From: didier amyot Date: Mon, 18 Sep 2023 10:08:16 -0400 Subject: [PATCH] Finished api --- .gitignore | 1 + .../Services/NotEmptyRobotsServiceTest.cs | 25 +++++++++ .../Controllers/RobotsController.cs | 30 ++++++++-- RobotsWantedLeague/Program.cs | 2 +- .../Services/NotEmptyRobotsService.cs | 35 ++++++++++++ .../Views/Robots/CreateRobot.cshtml | 43 +++++++++++++++ RobotsWantedLeague/Views/Robots/Index.cshtml | 5 +- RobotsWantedLeague/Views/Robots/Robot.cshtml | 29 ++++++++++ .../Views/Shared/_Layout.cshtml | 55 ++++++++++++++++++- 9 files changed, 217 insertions(+), 8 deletions(-) create mode 100644 RobotsWantedLeague.Test/Services/NotEmptyRobotsServiceTest.cs create mode 100644 RobotsWantedLeague/Services/NotEmptyRobotsService.cs create mode 100644 RobotsWantedLeague/Views/Robots/CreateRobot.cshtml create mode 100644 RobotsWantedLeague/Views/Robots/Robot.cshtml diff --git a/.gitignore b/.gitignore index 2aa278e..617cf3b 100644 --- a/.gitignore +++ b/.gitignore @@ -132,3 +132,4 @@ $RECYCLE.BIN/ _NCrunch* +.vscode diff --git a/RobotsWantedLeague.Test/Services/NotEmptyRobotsServiceTest.cs b/RobotsWantedLeague.Test/Services/NotEmptyRobotsServiceTest.cs new file mode 100644 index 0000000..ef808a1 --- /dev/null +++ b/RobotsWantedLeague.Test/Services/NotEmptyRobotsServiceTest.cs @@ -0,0 +1,25 @@ +namespace RobotsWantedLeague.Test.Services; +using RobotsWantedLeague.Models; +using RobotsWantedLeague.Services; + +[TestClass] +public class NotEmptyRobotsServiceTest +{ + + [TestMethod] + public void TestHappyPath() + { + IRobotsService service = new NotEmptyRobotsService(); + Assert.AreEqual(3, service.Robots.Count); + Robot robot = service.CreateRobot("paul", 2, 3, "canada"); + Assert.AreEqual(robot.Name, "paul"); + Assert.AreEqual(robot.Weight, 2); + + Assert.AreEqual(4, service.Robots.Count); + + service.DeleteRobotById(robot.Id); + Assert.AreEqual(3, service.Robots.Count); + } + + +} \ No newline at end of file diff --git a/RobotsWantedLeague/Controllers/RobotsController.cs b/RobotsWantedLeague/Controllers/RobotsController.cs index fc38714..dbd1087 100644 --- a/RobotsWantedLeague/Controllers/RobotsController.cs +++ b/RobotsWantedLeague/Controllers/RobotsController.cs @@ -5,6 +5,14 @@ using RobotsWantedLeague.Services; namespace RobotsWantedLeague.Controllers; +public class RobotRequest{ + public string Name { get; set; } + public int Height { get; set; } + public int Weight { get; set; } + + public string Country { get; set; } +} + public class RobotsController : Controller { private readonly ILogger _logger; @@ -22,14 +30,26 @@ public class RobotsController : Controller return View(robotsService.Robots); } - public IActionResult Privacy() - { + public IActionResult Robot(int id){ + Robot robot = robotsService.GetRobotById(id); + return View(robot); + } + + [HttpGet] + public IActionResult CreateRobot(){ return View(); } - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] - public IActionResult Error() + [HttpPost] + public IActionResult CreateRobot([FromBody] RobotRequest robot) { - return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + Robot r = robotsService.CreateRobot(robot.Name, + robot.Weight, + robot.Height, + robot.Country); + string htmxRedirectHeaderName = "HX-Redirect"; + string redirectURL = "/robots/robot?id=" + r.Id; + Response.Headers.Add(htmxRedirectHeaderName, redirectURL); + return Ok(); } } diff --git a/RobotsWantedLeague/Program.cs b/RobotsWantedLeague/Program.cs index 7c470c8..5718be6 100644 --- a/RobotsWantedLeague/Program.cs +++ b/RobotsWantedLeague/Program.cs @@ -4,7 +4,7 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); -builder.Services.AddSingleton(); +builder.Services.AddSingleton(); var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/RobotsWantedLeague/Services/NotEmptyRobotsService.cs b/RobotsWantedLeague/Services/NotEmptyRobotsService.cs new file mode 100644 index 0000000..bfc193e --- /dev/null +++ b/RobotsWantedLeague/Services/NotEmptyRobotsService.cs @@ -0,0 +1,35 @@ +namespace RobotsWantedLeague.Services; + +using RobotsWantedLeague.Models; + +public class NotEmptyRobotsService: IRobotsService +{ + private readonly IRobotsService underlyingRobotsService; + public List Robots { get => underlyingRobotsService.Robots; } + + public NotEmptyRobotsService() + { + this.underlyingRobotsService = new RobotsService(); + this.underlyingRobotsService.CreateRobot("Alice", 1050, 2, "Bhutan"); + this.underlyingRobotsService.CreateRobot("Bob", 5001, 5, "Vanuatu"); + this.underlyingRobotsService.CreateRobot("Xu", 890, 1, "Taiwan"); + } + + public Robot CreateRobot(string name, + int weight, + int height, + string country) + { + return underlyingRobotsService.CreateRobot(name, weight, height, country); + } + + + public Robot? GetRobotById(int id){ + return underlyingRobotsService.GetRobotById(id); + } + + public bool DeleteRobotById(int id){ + return underlyingRobotsService.DeleteRobotById(id); + } + +} \ No newline at end of file diff --git a/RobotsWantedLeague/Views/Robots/CreateRobot.cshtml b/RobotsWantedLeague/Views/Robots/CreateRobot.cshtml new file mode 100644 index 0000000..8353150 --- /dev/null +++ b/RobotsWantedLeague/Views/Robots/CreateRobot.cshtml @@ -0,0 +1,43 @@ + +
+
+ + +
+
+ + +
+ +
+ + +
+ +
+ + +
+ + +
\ No newline at end of file diff --git a/RobotsWantedLeague/Views/Robots/Index.cshtml b/RobotsWantedLeague/Views/Robots/Index.cshtml index ad56260..e664878 100644 --- a/RobotsWantedLeague/Views/Robots/Index.cshtml +++ b/RobotsWantedLeague/Views/Robots/Index.cshtml @@ -10,4 +10,7 @@ @robot.Name } - \ No newline at end of file + + + + Ajouter un robot diff --git a/RobotsWantedLeague/Views/Robots/Robot.cshtml b/RobotsWantedLeague/Views/Robots/Robot.cshtml new file mode 100644 index 0000000..4406180 --- /dev/null +++ b/RobotsWantedLeague/Views/Robots/Robot.cshtml @@ -0,0 +1,29 @@ +@model Robot + +
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + +
Nom@Model.Name
Poids@Model.Weight (kg)
Taille@Model.Height (m)
Pays@Model.Country
\ No newline at end of file diff --git a/RobotsWantedLeague/Views/Shared/_Layout.cshtml b/RobotsWantedLeague/Views/Shared/_Layout.cshtml index 9cd31bf..9029f1f 100644 --- a/RobotsWantedLeague/Views/Shared/_Layout.cshtml +++ b/RobotsWantedLeague/Views/Shared/_Layout.cshtml @@ -7,12 +7,65 @@ + + +