From c3fe66ce2b18deb7f257b70fb95677da3c1fdb46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Desc=C3=B4teaux?= <125318411+marcdescotes@users.noreply.github.com> Date: Wed, 20 Sep 2023 20:22:33 -0400 Subject: [PATCH] Adding visited countries by robot + it stays --- .../Controllers/RobotsController.cs | 38 +++- RobotsWantedLeague/Models/Robot.cs | 29 ++- RobotsWantedLeague/Services/IRobotsService.cs | 8 +- .../Services/NotEmptyRobotsService.cs | 13 +- RobotsWantedLeague/Services/RobotsService.cs | 10 + .../Views/Robots/CreateRobot.cshtml | 8 +- RobotsWantedLeague/Views/Robots/Index.cshtml | 10 +- .../Views/Robots/Privacy.cshtml | 2 +- RobotsWantedLeague/Views/Robots/Robot.cshtml | 83 +++++++-- .../Views/Shared/_Layout.cshtml | 173 +++++++++--------- 10 files changed, 235 insertions(+), 139 deletions(-) diff --git a/RobotsWantedLeague/Controllers/RobotsController.cs b/RobotsWantedLeague/Controllers/RobotsController.cs index dbd1087..f9f783f 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,44 @@ 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, + if (!ModelState.IsValid) + { + return View(robot); + } + 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/Robot.cs b/RobotsWantedLeague/Models/Robot.cs index c3bd0f7..b18f2f4 100644 --- a/RobotsWantedLeague/Models/Robot.cs +++ b/RobotsWantedLeague/Models/Robot.cs @@ -1,27 +1,22 @@ 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 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/Services/IRobotsService.cs b/RobotsWantedLeague/Services/IRobotsService.cs index d8e3302..6ff3931 100644 --- a/RobotsWantedLeague/Services/IRobotsService.cs +++ b/RobotsWantedLeague/Services/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/NotEmptyRobotsService.cs index bfc193e..23d8e23 100644 --- a/RobotsWantedLeague/Services/NotEmptyRobotsService.cs +++ b/RobotsWantedLeague/Services/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/RobotsService.cs index a110c47..242dcef 100644 --- a/RobotsWantedLeague/Services/RobotsService.cs +++ b/RobotsWantedLeague/Services/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/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..1e750a6 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
    - - - + + + + + + - - - - - - + + + + -
    Poids@Model.Weight (kg)
    Nom@Model.Name
    Taille@Model.Height (m)
    Pays@Model.Country
    Poids@Model.Weight (kg)
    \ No newline at end of file + + Taille + @Model.Height (m) + + + Pays + + @Model.Country + + + + + Pays visités + @string.Join(", ", Model.VisitedCountries) + + + + + + + + + diff --git a/RobotsWantedLeague/Views/Shared/_Layout.cshtml b/RobotsWantedLeague/Views/Shared/_Layout.cshtml index 9029f1f..d5b0dbb 100644 --- a/RobotsWantedLeague/Views/Shared/_Layout.cshtml +++ b/RobotsWantedLeague/Views/Shared/_Layout.cshtml @@ -1,95 +1,104 @@  + - - - @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) + + + +