diff --git a/RobotsWantedLeague/Controllers/AgentsController.cs b/RobotsWantedLeague/Controllers/AgentsController.cs new file mode 100644 index 0000000..ec0199b --- /dev/null +++ b/RobotsWantedLeague/Controllers/AgentsController.cs @@ -0,0 +1,61 @@ +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using RobotsWantedLeague.Models; +using RobotsWantedLeague.Services; +using System.ComponentModel.DataAnnotations; + +namespace RobotsWantedLeague.Controllers; + +public class AgentRequest +{ + public string Name { get; set; } + public string Continent { get; set; } + +} + +public class AgentsController : Controller +{ + private readonly ILogger _logger; + private readonly IAgentsService agentsService; + + public AgentsController(ILogger logger, + IAgentsService agentsService) + { + _logger = logger; + this.agentsService = agentsService; + } + + public IActionResult Index() + { + return View(agentsService.Agents); + } + + public IActionResult Agent(int id) + { + Agent? agent = agentsService.GetAgentById(id); + if (agent == null) + { + return NotFound(); + } + return View(agent); + } + + [HttpGet] + public IActionResult CreateAgent() + { + return View(); + } + + [HttpPost] + public IActionResult CreateAgent([FromBody] AgentRequest agent) + { + + Agent r = agentsService.CreateAgent(agent.Name, + agent.Continent); + string htmxRedirectHeaderName = "HX-Redirect"; + string redirectURL = "/agents/agent?id=" + r.Id; + Response.Headers.Add(htmxRedirectHeaderName, redirectURL); + return Ok(); + } + +} diff --git a/RobotsWantedLeague/Controllers/RobotsController.cs b/RobotsWantedLeague/Controllers/RobotsController.cs index f9f783f..9e9be2a 100644 --- a/RobotsWantedLeague/Controllers/RobotsController.cs +++ b/RobotsWantedLeague/Controllers/RobotsController.cs @@ -51,10 +51,7 @@ public class RobotsController : Controller [HttpPost] public IActionResult CreateRobot([FromBody] RobotRequest robot) { - if (!ModelState.IsValid) - { - return View(robot); - } + Robot r = robotsService.CreateRobot(robot.Name, robot.Weight, robot.Height, diff --git a/RobotsWantedLeague/Models/Agent.cs b/RobotsWantedLeague/Models/Agent.cs new file mode 100644 index 0000000..c8cde01 --- /dev/null +++ b/RobotsWantedLeague/Models/Agent.cs @@ -0,0 +1,17 @@ +namespace RobotsWantedLeague.Models; + +public class Agent +{ + public int Id { get; set; } + public string Name { get; set; } + public string Continent { get; set; } + + + public Agent(int Id, string Name, string Continent) + { + this.Id = Id; + this.Name = Name; + this.Continent = Continent; + } + +} \ No newline at end of file diff --git a/RobotsWantedLeague/Models/Robot.cs b/RobotsWantedLeague/Models/Robot.cs index b18f2f4..a4fdf0b 100644 --- a/RobotsWantedLeague/Models/Robot.cs +++ b/RobotsWantedLeague/Models/Robot.cs @@ -7,7 +7,9 @@ public class Robot public int Weight { get; set; } public int Height { get; set; } public string Country { get; set; } + public string NewCountry { get; set; } public List VisitedCountries { get; set; } = new List(); + public Robot(int Id, string Name, int Weight, int Height, string Country) { diff --git a/RobotsWantedLeague/Program.cs b/RobotsWantedLeague/Program.cs index 5718be6..a34d5d6 100644 --- a/RobotsWantedLeague/Program.cs +++ b/RobotsWantedLeague/Program.cs @@ -5,6 +5,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/Agents/AgentsService.cs b/RobotsWantedLeague/Services/Agents/AgentsService.cs new file mode 100644 index 0000000..83baffc --- /dev/null +++ b/RobotsWantedLeague/Services/Agents/AgentsService.cs @@ -0,0 +1,57 @@ +namespace RobotsWantedLeague.Services; + +using RobotsWantedLeague.Models; + +public class AgentsService: IAgentsService +{ + private readonly List agents; + private int idGenerator = 0; + public List Agents { get => agents; } + + public AgentsService() + { + agents = new List(); + } + + private int generateId(){ + idGenerator = idGenerator + 1; + return idGenerator; + } + + public Agent CreateAgent(string name, + string continent) + { + var agent = new Agent(generateId(), name, continent); + agents.Add(agent); + return agent; + } + + private int getIndexOfAgentById(int id){ + int idx = 0; + foreach (Agent agent in agents){ + if (agent.Id == id){ + return idx; + } + idx ++; + } + return -1; + } + + public Agent? GetAgentById(int id){ + int indexToDelete = getIndexOfAgentById(id); + if(indexToDelete == -1){ + return null; + } + return agents[indexToDelete]; + } + + public bool DeleteAgentById(int id){ + int indexToDelete = getIndexOfAgentById(id); + if(indexToDelete == -1){ + return false; + } + agents.RemoveAt(indexToDelete); + return true; + } + +} \ No newline at end of file diff --git a/RobotsWantedLeague/Services/Agents/IAgentsService.cs b/RobotsWantedLeague/Services/Agents/IAgentsService.cs new file mode 100644 index 0000000..2738824 --- /dev/null +++ b/RobotsWantedLeague/Services/Agents/IAgentsService.cs @@ -0,0 +1,14 @@ +namespace RobotsWantedLeague.Services; +using RobotsWantedLeague.Models; + +public interface IAgentsService +{ + public List Agents { get; } + + public Agent CreateAgent(string name, + string continent); + public Agent? GetAgentById(int id); + public bool DeleteAgentById(int id); + + +} \ No newline at end of file diff --git a/RobotsWantedLeague/Services/Agents/NotEmptyAgentsService.cs b/RobotsWantedLeague/Services/Agents/NotEmptyAgentsService.cs new file mode 100644 index 0000000..4fb7f0e --- /dev/null +++ b/RobotsWantedLeague/Services/Agents/NotEmptyAgentsService.cs @@ -0,0 +1,38 @@ +namespace RobotsWantedLeague.Services; + +using RobotsWantedLeague.Models; + +public class NotEmptyAgentsService : IAgentsService +{ + private readonly IAgentsService underlyingAgentsService; + public List Agents { get => underlyingAgentsService.Agents; } + + public NotEmptyAgentsService() + { + this.underlyingAgentsService = new AgentsService(); + this.underlyingAgentsService.CreateAgent("Mulder", "Amérique du Nord"); + this.underlyingAgentsService.CreateAgent("Scully", "Amérique du Sud"); + this.underlyingAgentsService.CreateAgent("Doggett", "Antarctique"); + this.underlyingAgentsService.CreateAgent("Reyes", "Europe"); + this.underlyingAgentsService.CreateAgent("Skinner", "Asie"); + this.underlyingAgentsService.CreateAgent("Spender", "Afrique"); + this.underlyingAgentsService.CreateAgent("Kersh", "Océanie"); + } + + public Agent CreateAgent(string name, + string continent) + { + return underlyingAgentsService.CreateAgent(name, continent); + } + + public Agent? GetAgentById(int id) + { + return underlyingAgentsService.GetAgentById(id); + } + + public bool DeleteAgentById(int id) + { + return underlyingAgentsService.DeleteAgentById(id); + } + +} \ No newline at end of file diff --git a/RobotsWantedLeague/Services/IRobotsService.cs b/RobotsWantedLeague/Services/Robots/IRobotsService.cs similarity index 100% rename from RobotsWantedLeague/Services/IRobotsService.cs rename to RobotsWantedLeague/Services/Robots/IRobotsService.cs diff --git a/RobotsWantedLeague/Services/NotEmptyRobotsService.cs b/RobotsWantedLeague/Services/Robots/NotEmptyRobotsService.cs similarity index 100% rename from RobotsWantedLeague/Services/NotEmptyRobotsService.cs rename to RobotsWantedLeague/Services/Robots/NotEmptyRobotsService.cs diff --git a/RobotsWantedLeague/Services/RobotsService.cs b/RobotsWantedLeague/Services/Robots/RobotsService.cs similarity index 100% rename from RobotsWantedLeague/Services/RobotsService.cs rename to RobotsWantedLeague/Services/Robots/RobotsService.cs diff --git a/RobotsWantedLeague/Views/Agents/Agent.cshtml b/RobotsWantedLeague/Views/Agents/Agent.cshtml new file mode 100644 index 0000000..22c3361 --- /dev/null +++ b/RobotsWantedLeague/Views/Agents/Agent.cshtml @@ -0,0 +1,37 @@ +@model Agent + +@{ + string imageURL = "https://good-hawk-21.deno.dev/toaster" + Model.Id.ToString(); +} + +
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + +
Id@Model.Id
Nom@Model.Name
Continent@Model.Continent
diff --git a/RobotsWantedLeague/Views/Agents/Index.cshtml b/RobotsWantedLeague/Views/Agents/Index.cshtml new file mode 100644 index 0000000..b8a001d --- /dev/null +++ b/RobotsWantedLeague/Views/Agents/Index.cshtml @@ -0,0 +1,17 @@ +@{ + ViewData["Title"] = "Agent List"; +} +@model List + +

LISTE DES AGENTS

+ +
+
    + @foreach (Agent agent in Model){ +
  • + @agent.Name +
  • + } +
+
+ diff --git a/RobotsWantedLeague/Views/Robots/Robot.cshtml b/RobotsWantedLeague/Views/Robots/Robot.cshtml index 1e750a6..dd8d05c 100644 --- a/RobotsWantedLeague/Views/Robots/Robot.cshtml +++ b/RobotsWantedLeague/Views/Robots/Robot.cshtml @@ -12,10 +12,9 @@ - - + @@ -49,6 +48,7 @@
Nom
+@Html.ValidationSummary(true)