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)