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 fbb9aa2..9e9be2a 100644 --- a/RobotsWantedLeague/Controllers/RobotsController.cs +++ b/RobotsWantedLeague/Controllers/RobotsController.cs @@ -8,19 +8,13 @@ namespace RobotsWantedLeague.Controllers; public class RobotRequest { - public string Country { get; set; } public string Name { get; set; } public int Height { get; set; } public int Weight { get; set; } - - } - - - public class RobotsController : Controller { private readonly ILogger _logger; 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 786cb39..a4fdf0b 100644 --- a/RobotsWantedLeague/Models/Robot.cs +++ b/RobotsWantedLeague/Models/Robot.cs @@ -7,7 +7,6 @@ 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(); 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..365e0eb --- /dev/null +++ b/RobotsWantedLeague/Views/Agents/Agent.cshtml @@ -0,0 +1,40 @@ +@model Agent + +@{ + string imageURL = "https://good-hawk-21.deno.dev/toaster" + Model.Id.ToString(); +} + +
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + +
Nom@Model.Name
Id@Model.Id
Continent@Model.Continent
+ + + diff --git a/RobotsWantedLeague/Views/Agents/Index.cshtml b/RobotsWantedLeague/Views/Agents/Index.cshtml new file mode 100644 index 0000000..a205d75 --- /dev/null +++ b/RobotsWantedLeague/Views/Agents/Index.cshtml @@ -0,0 +1,17 @@ +@{ + ViewData["Title"] = "Home Page"; +} +@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 aaee6d1..e1d0770 100644 --- a/RobotsWantedLeague/Views/Robots/Robot.cshtml +++ b/RobotsWantedLeague/Views/Robots/Robot.cshtml @@ -12,17 +12,6 @@ -@*
-
-
- -
-
-
*@ - - - - diff --git a/RobotsWantedLeague/Views/Shared/_Layout.cshtml b/RobotsWantedLeague/Views/Shared/_Layout.cshtml index d5b0dbb..c23b51d 100644 --- a/RobotsWantedLeague/Views/Shared/_Layout.cshtml +++ b/RobotsWantedLeague/Views/Shared/_Layout.cshtml @@ -71,6 +71,11 @@