From 8c399ab5760cb42738c4745db36b44d08f0a920d Mon Sep 17 00:00:00 2001 From: didier amyot Date: Mon, 18 Sep 2023 10:38:11 -0400 Subject: [PATCH 1/8] fixed bug in image hash --- RobotsWantedLeague/Views/Robots/Robot.cshtml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/RobotsWantedLeague/Views/Robots/Robot.cshtml b/RobotsWantedLeague/Views/Robots/Robot.cshtml index 4406180..a4c44d9 100644 --- a/RobotsWantedLeague/Views/Robots/Robot.cshtml +++ b/RobotsWantedLeague/Views/Robots/Robot.cshtml @@ -1,10 +1,14 @@ @model Robot +@{ + string imageURL = "https://robohash.org/aligator" + Model.Id.ToString() + "?set=set2"; +} +
+ src="@imageURL">
-- 2.55.0 From 57c5f9f81570b6051c527a38fa4addbe2dd40fcc Mon Sep 17 00:00:00 2001 From: didier amyot Date: Mon, 18 Sep 2023 10:50:24 -0400 Subject: [PATCH 2/8] added defaults to robot request fields --- RobotsWantedLeague/Controllers/RobotsController.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/RobotsWantedLeague/Controllers/RobotsController.cs b/RobotsWantedLeague/Controllers/RobotsController.cs index dbd1087..3ddc848 100644 --- a/RobotsWantedLeague/Controllers/RobotsController.cs +++ b/RobotsWantedLeague/Controllers/RobotsController.cs @@ -6,6 +6,13 @@ using RobotsWantedLeague.Services; namespace RobotsWantedLeague.Controllers; public class RobotRequest{ + public RobotRequest(){ + this.Name = ""; + this.Country = ""; + this.Height = 0; + this.Weight = 0; + } + public string Name { get; set; } public int Height { get; set; } public int Weight { get; set; } -- 2.55.0 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 3/8] 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) + + + + -- 2.55.0 From fe72a360ae44cf2452852ec00684e4ab8d413fe3 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: Thu, 21 Sep 2023 08:50:01 -0400 Subject: [PATCH 4/8] Started new branch --- .../Controllers/RobotsController.cs | 35 ++++++++++++++++--- RobotsWantedLeague/Models/Robot.cs | 3 ++ .../Services/CountryConstants.cs | 10 ++++++ RobotsWantedLeague/Views/Robots/Robot.cshtml | 5 ++- 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 RobotsWantedLeague/Services/CountryConstants.cs diff --git a/RobotsWantedLeague/Controllers/RobotsController.cs b/RobotsWantedLeague/Controllers/RobotsController.cs index f9f783f..c408ef9 100644 --- a/RobotsWantedLeague/Controllers/RobotsController.cs +++ b/RobotsWantedLeague/Controllers/RobotsController.cs @@ -8,13 +8,43 @@ namespace RobotsWantedLeague.Controllers; public class RobotRequest { + // [AllowedCountry(ErrorMessage = "Le pays n'est pas autorisé.")] + // public string NewCountry { get; set; } public string Country { get; set; } public string Name { get; set; } public int Height { get; set; } public int Weight { get; set; } +// public static readonly List AllowedCountries = new List +// { +// "Canada", +// "United States of America", +// "Mexico", + +// }; + } +// public class AllowedCountryAttribute : ValidationAttribute +// { +// protected override ValidationResult IsValid(object value, ValidationContext validationContext) +// { +// string country = value as string; +// if (string.IsNullOrEmpty(country)) +// { +// return ValidationResult.Success; +// } + +// if (!CountryConstants.AllowedCountries.Contains(country, StringComparer.OrdinalIgnoreCase)) +// { +// return new ValidationResult(ErrorMessage); +// } + +// return ValidationResult.Success; +// } +// } + + public class RobotsController : Controller { private readonly ILogger _logger; @@ -51,10 +81,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/Robot.cs b/RobotsWantedLeague/Models/Robot.cs index b18f2f4..e76a8d6 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) { @@ -16,6 +18,7 @@ public class Robot this.Weight = Weight; this.Height = Height; this.Country = Country; + // this.NewCountry = NewCountry; } diff --git a/RobotsWantedLeague/Services/CountryConstants.cs b/RobotsWantedLeague/Services/CountryConstants.cs new file mode 100644 index 0000000..5107611 --- /dev/null +++ b/RobotsWantedLeague/Services/CountryConstants.cs @@ -0,0 +1,10 @@ +// public static class CountryConstants +// { +// public static readonly List AllowedCountries = new List +// { +// "Canada", +// "United States of America", +// "Mexico", +// // Ajoutez les 7 autres pays autorisés ici +// }; +// } \ No newline at end of file diff --git a/RobotsWantedLeague/Views/Robots/Robot.cshtml b/RobotsWantedLeague/Views/Robots/Robot.cshtml index 1e750a6..db84651 100644 --- a/RobotsWantedLeague/Views/Robots/Robot.cshtml +++ b/RobotsWantedLeague/Views/Robots/Robot.cshtml @@ -49,6 +49,7 @@ +@Html.ValidationSummary(true) +@*
    +
    +
    + +
    +
    +
    *@ + + + @@ -66,9 +76,7 @@
    + + + + + + + + + + + + + + + + + + + + + +
    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 @@
    - + + + + + + - - - - - @@ -35,6 +35,3 @@
    Id@Model.Id
    Nom @Model.Name
    Id@Model.Id
    Continent @Model.Continent
    - - - diff --git a/RobotsWantedLeague/Views/Agents/Index.cshtml b/RobotsWantedLeague/Views/Agents/Index.cshtml index a205d75..b8a001d 100644 --- a/RobotsWantedLeague/Views/Agents/Index.cshtml +++ b/RobotsWantedLeague/Views/Agents/Index.cshtml @@ -1,5 +1,5 @@ @{ - ViewData["Title"] = "Home Page"; + ViewData["Title"] = "Agent List"; } @model List diff --git a/RobotsWantedLeague/Views/Robots/Robot.cshtml b/RobotsWantedLeague/Views/Robots/Robot.cshtml index e1d0770..dd8d05c 100644 --- a/RobotsWantedLeague/Views/Robots/Robot.cshtml +++ b/RobotsWantedLeague/Views/Robots/Robot.cshtml @@ -14,7 +14,7 @@ - + -- 2.55.0
    Nom