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 dbd1087..9e9be2a 100644 --- a/RobotsWantedLeague/Controllers/RobotsController.cs +++ b/RobotsWantedLeague/Controllers/RobotsController.cs @@ -2,15 +2,17 @@ using Microsoft.AspNetCore.Mvc; using RobotsWantedLeague.Models; using RobotsWantedLeague.Services; +using System.ComponentModel.DataAnnotations; namespace RobotsWantedLeague.Controllers; -public class RobotRequest{ +public class RobotRequest +{ + public string Country { get; set; } public string Name { get; set; } public int Height { get; set; } public int Weight { get; set; } - - public string Country { get; set; } + } public class RobotsController : Controller @@ -30,26 +32,41 @@ public class RobotsController : Controller return View(robotsService.Robots); } - public IActionResult Robot(int id){ - Robot robot = robotsService.GetRobotById(id); + public IActionResult Robot(int id) + { + Robot? robot = robotsService.GetRobotById(id); + if (robot == null) + { + return NotFound(); + } return View(robot); } [HttpGet] - public IActionResult CreateRobot(){ + public IActionResult CreateRobot() + { return View(); } [HttpPost] public IActionResult CreateRobot([FromBody] RobotRequest robot) { - Robot r = robotsService.CreateRobot(robot.Name, - robot.Weight, - robot.Height, + + 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(); } + + [HttpPost] + public IActionResult ChangeRobotCountry(int robotId, string newCountry) + { + robotsService.ChangeRobotCountry(robotId, newCountry); + return RedirectToAction("Robot", new { id = robotId }); + } + } 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 c3bd0f7..a4fdf0b 100644 --- a/RobotsWantedLeague/Models/Robot.cs +++ b/RobotsWantedLeague/Models/Robot.cs @@ -1,27 +1,24 @@ namespace RobotsWantedLeague.Models; -public class Robot{ - public int Id { get; set;} - public string Name { get; set;} +public class Robot +{ + public int Id { get; set; } + public string Name { get; set; } + 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(); + - /** - * Weight in kg. - */ - public int Weight { get; set;} - - /** - * Height in m. - */ - public int Height { get; set;} - - public string Country { get; set;} - - public Robot(int Id, string Name, int Weight, int Height, string Country){ + public Robot(int Id, string Name, int Weight, int Height, string Country) + { this.Id = Id; this.Name = Name; this.Weight = Weight; this.Height = Height; this.Country = Country; } - + + } \ No newline at end of file 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 77% rename from RobotsWantedLeague/Services/IRobotsService.cs rename to RobotsWantedLeague/Services/Robots/IRobotsService.cs index d8e3302..6ff3931 100644 --- a/RobotsWantedLeague/Services/IRobotsService.cs +++ b/RobotsWantedLeague/Services/Robots/IRobotsService.cs @@ -1,15 +1,17 @@ namespace RobotsWantedLeague.Services; using RobotsWantedLeague.Models; -public interface IRobotsService{ +public interface IRobotsService +{ public List Robots { get; } public Robot CreateRobot(string name, int weight, int height, string country); - public Robot? GetRobotById(int id); - public bool DeleteRobotById(int id); + public void ChangeRobotCountry(int robotId, string newCountry); + + } \ No newline at end of file diff --git a/RobotsWantedLeague/Services/NotEmptyRobotsService.cs b/RobotsWantedLeague/Services/Robots/NotEmptyRobotsService.cs similarity index 75% rename from RobotsWantedLeague/Services/NotEmptyRobotsService.cs rename to RobotsWantedLeague/Services/Robots/NotEmptyRobotsService.cs index bfc193e..23d8e23 100644 --- a/RobotsWantedLeague/Services/NotEmptyRobotsService.cs +++ b/RobotsWantedLeague/Services/Robots/NotEmptyRobotsService.cs @@ -2,7 +2,7 @@ namespace RobotsWantedLeague.Services; using RobotsWantedLeague.Models; -public class NotEmptyRobotsService: IRobotsService +public class NotEmptyRobotsService : IRobotsService { private readonly IRobotsService underlyingRobotsService; public List Robots { get => underlyingRobotsService.Robots; } @@ -24,12 +24,19 @@ public class NotEmptyRobotsService: IRobotsService } - public Robot? GetRobotById(int id){ + public Robot? GetRobotById(int id) + { return underlyingRobotsService.GetRobotById(id); } - public bool DeleteRobotById(int id){ + public bool DeleteRobotById(int id) + { return underlyingRobotsService.DeleteRobotById(id); } + public void ChangeRobotCountry(int robotId, string newCountry) + { + underlyingRobotsService.ChangeRobotCountry(robotId, newCountry); + } + } \ No newline at end of file diff --git a/RobotsWantedLeague/Services/RobotsService.cs b/RobotsWantedLeague/Services/Robots/RobotsService.cs similarity index 83% rename from RobotsWantedLeague/Services/RobotsService.cs rename to RobotsWantedLeague/Services/Robots/RobotsService.cs index a110c47..242dcef 100644 --- a/RobotsWantedLeague/Services/RobotsService.cs +++ b/RobotsWantedLeague/Services/Robots/RobotsService.cs @@ -56,4 +56,14 @@ public class RobotsService: IRobotsService return true; } + public void ChangeRobotCountry(int robotId, string newCountry) + { + Robot robot = GetRobotById(robotId); + if (robot != null) + { + robot.VisitedCountries.Add(robot.Country); + robot.Country = newCountry; + } + } + } \ No newline at end of file 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/CreateRobot.cshtml b/RobotsWantedLeague/Views/Robots/CreateRobot.cshtml index 8353150..87968b0 100644 --- a/RobotsWantedLeague/Views/Robots/CreateRobot.cshtml +++ b/RobotsWantedLeague/Views/Robots/CreateRobot.cshtml @@ -1,7 +1,7 @@ -
+ hx-trigger="submit" + style="width: 40%; margin-left: auto; margin-right: auto;">
Pays
diff --git a/RobotsWantedLeague/Views/Robots/Index.cshtml b/RobotsWantedLeague/Views/Robots/Index.cshtml index e664878..8a411ce 100644 --- a/RobotsWantedLeague/Views/Robots/Index.cshtml +++ b/RobotsWantedLeague/Views/Robots/Index.cshtml @@ -3,14 +3,18 @@ } @model List +

LISTE DES ROBOTS RECHERCHÉS

-
    +
    + +
    - - Ajouter un robot + diff --git a/RobotsWantedLeague/Views/Robots/Privacy.cshtml b/RobotsWantedLeague/Views/Robots/Privacy.cshtml index af4fb19..b3d45f6 100644 --- a/RobotsWantedLeague/Views/Robots/Privacy.cshtml +++ b/RobotsWantedLeague/Views/Robots/Privacy.cshtml @@ -3,4 +3,4 @@ }

    @ViewData["Title"]

    -

    Use this page to detail your site's privacy policy.

    +

    This isn't the droids you're looking for

    diff --git a/RobotsWantedLeague/Views/Robots/Robot.cshtml b/RobotsWantedLeague/Views/Robots/Robot.cshtml index 4406180..dd8d05c 100644 --- a/RobotsWantedLeague/Views/Robots/Robot.cshtml +++ b/RobotsWantedLeague/Views/Robots/Robot.cshtml @@ -1,29 +1,78 @@ @model Robot +@{ + string imageURL = "https://robohash.org/toaster" + Model.Id.ToString() + "?set=set2"; +} +
    -
    - +
    +
    + -
    - - - + + + + + + - - - + + + + - - - - - - + + + + + + + + + + + + + + -
    Nom@Model.Name
    Nom@Model.Name
    Poids@Model.Weight (kg)
    Poids@Model.Weight (kg)
    Taille@Model.Height (m)
    Pays@Model.Country
    Taille@Model.Height (m)
    Pays + @Model.Country + +
    Pays visités@string.Join(", ", Model.VisitedCountries)
    \ No newline at end of file + + +@Html.ValidationSummary(true) + + + diff --git a/RobotsWantedLeague/Views/Shared/_Layout.cshtml b/RobotsWantedLeague/Views/Shared/_Layout.cshtml index 9029f1f..c23b51d 100644 --- a/RobotsWantedLeague/Views/Shared/_Layout.cshtml +++ b/RobotsWantedLeague/Views/Shared/_Layout.cshtml @@ -1,95 +1,109 @@  + - - - @ViewData["Title"] - RobotsWantedLeague - - - - - + + function _cast(str) { + return _isNumeric(str) ? parseFloat(str) : str; + } + + function _addAssocToDict(obj, key, value) { + let splittedKey = Array.isArray(key) ? key : key.split("."); + const keyToAdd = splittedKey.shift(); + const isTerminal = splittedKey.length === 0; + if (isTerminal) { + obj[keyToAdd] = _cast(value); + return obj; + } + if (!(keyToAdd in obj)) { + obj[keyToAdd] = {}; + } + return _addAssocToDict(obj[keyToAdd], splittedKey, value); + } + + function _createPayload(params) { + let ret = {}; + for (let key of Object.keys(params)) { + _addAssocToDict(ret, key, params[key]); + } + return ret; + } + + htmx.defineExtension('json-enc', { + onEvent: function (name, evt) { + if (name === "htmx:configRequest") { + evt.detail.headers['Content-Type'] = "application/json"; + } + }, + + encodeParameters: function (xhr, parameters, elt) { + xhr.overrideMimeType('text/json'); + return (JSON.stringify(_createPayload(parameters))); + } + }); + })(); + - -
    - -
    -
    -
    - @RenderBody() -
    -
    - - - - - @await RenderSectionAsync("Scripts", required: false) +
    + + +
    +
    + @RenderBody() +
    +
    + +
    +
    + © 2023 - RobotsWantedLeague +
    +
    + + + + @await RenderSectionAsync("Scripts", required: false) + + + +