Adding visited countries by robot + it stays #2

Closed
marcdescotes wants to merge 7 commits from main into main
pull from: main
10 changed files with 235 additions and 139 deletions
Showing only changes of commit c3fe66ce2b - Show all commits

No files matched your search

@@ -2,15 +2,17 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using RobotsWantedLeague.Models; using RobotsWantedLeague.Models;
using RobotsWantedLeague.Services; using RobotsWantedLeague.Services;
using System.ComponentModel.DataAnnotations;
namespace RobotsWantedLeague.Controllers; namespace RobotsWantedLeague.Controllers;
public class RobotRequest{ public class RobotRequest
{
public string Country { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int Height { get; set; } public int Height { get; set; }
public int Weight { get; set; } public int Weight { get; set; }
public string Country { get; set; }
} }
public class RobotsController : Controller public class RobotsController : Controller
@@ -30,26 +32,44 @@ public class RobotsController : Controller
return View(robotsService.Robots); return View(robotsService.Robots);
} }
public IActionResult Robot(int id){ public IActionResult Robot(int id)
Robot robot = robotsService.GetRobotById(id); {
Robot? robot = robotsService.GetRobotById(id);
if (robot == null)
{
return NotFound();
}
return View(robot); return View(robot);
} }
[HttpGet] [HttpGet]
public IActionResult CreateRobot(){ public IActionResult CreateRobot()
{
return View(); return View();
} }
[HttpPost] [HttpPost]
public IActionResult CreateRobot([FromBody] RobotRequest robot) public IActionResult CreateRobot([FromBody] RobotRequest robot)
{ {
Robot r = robotsService.CreateRobot(robot.Name, if (!ModelState.IsValid)
robot.Weight, {
robot.Height, return View(robot);
}
Robot r = robotsService.CreateRobot(robot.Name,
robot.Weight,
robot.Height,
robot.Country); robot.Country);
string htmxRedirectHeaderName = "HX-Redirect"; string htmxRedirectHeaderName = "HX-Redirect";
string redirectURL = "/robots/robot?id=" + r.Id; string redirectURL = "/robots/robot?id=" + r.Id;
Response.Headers.Add(htmxRedirectHeaderName, redirectURL); Response.Headers.Add(htmxRedirectHeaderName, redirectURL);
return Ok(); return Ok();
} }
[HttpPost]
public IActionResult ChangeRobotCountry(int robotId, string newCountry)
{
robotsService.ChangeRobotCountry(robotId, newCountry);
return RedirectToAction("Robot", new { id = robotId });
}
} }
+12 -17
View File
@@ -1,27 +1,22 @@
namespace RobotsWantedLeague.Models; namespace RobotsWantedLeague.Models;
public class Robot{ public class Robot
public int Id { get; set;} {
public string Name { get; set;} 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<string> VisitedCountries { get; set; } = new List<string>();
/** public Robot(int Id, string Name, int Weight, int Height, string Country)
* 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){
this.Id = Id; this.Id = Id;
this.Name = Name; this.Name = Name;
this.Weight = Weight; this.Weight = Weight;
this.Height = Height; this.Height = Height;
this.Country = Country; this.Country = Country;
} }
} }
@@ -1,15 +1,17 @@
namespace RobotsWantedLeague.Services; namespace RobotsWantedLeague.Services;
using RobotsWantedLeague.Models; using RobotsWantedLeague.Models;
public interface IRobotsService{ public interface IRobotsService
{
public List<Robot> Robots { get; } public List<Robot> Robots { get; }
public Robot CreateRobot(string name, public Robot CreateRobot(string name,
int weight, int weight,
int height, int height,
string country); string country);
public Robot? GetRobotById(int id); public Robot? GetRobotById(int id);
public bool DeleteRobotById(int id); public bool DeleteRobotById(int id);
public void ChangeRobotCountry(int robotId, string newCountry);
} }
@@ -2,7 +2,7 @@ namespace RobotsWantedLeague.Services;
using RobotsWantedLeague.Models; using RobotsWantedLeague.Models;
public class NotEmptyRobotsService: IRobotsService public class NotEmptyRobotsService : IRobotsService
{ {
private readonly IRobotsService underlyingRobotsService; private readonly IRobotsService underlyingRobotsService;
public List<Robot> Robots { get => underlyingRobotsService.Robots; } public List<Robot> 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); return underlyingRobotsService.GetRobotById(id);
} }
public bool DeleteRobotById(int id){ public bool DeleteRobotById(int id)
{
return underlyingRobotsService.DeleteRobotById(id); return underlyingRobotsService.DeleteRobotById(id);
} }
public void ChangeRobotCountry(int robotId, string newCountry)
{
underlyingRobotsService.ChangeRobotCountry(robotId, newCountry);
}
} }
@@ -56,4 +56,14 @@ public class RobotsService: IRobotsService
return true; return true;
} }
public void ChangeRobotCountry(int robotId, string newCountry)
{
Robot robot = GetRobotById(robotId);
if (robot != null)
{
robot.VisitedCountries.Add(robot.Country);
robot.Country = newCountry;
}
}
} }
@@ -1,7 +1,7 @@
<form hx-post="/robots/createRobot" <form hx-post="/robots/createRobot"
hx-ext="json-enc" hx-ext="json-enc"
hx-trigger="submit"> hx-trigger="submit"
style="width: 40%; margin-left: auto; margin-right: auto;">
<div class="form-group"> <div class="form-group">
<label for="name">Le nom du robot</label> <label for="name">Le nom du robot</label>
<input type="text" <input type="text"
@@ -34,8 +34,8 @@
<label for="country">Pays</label> <label for="country">Pays</label>
<input type="text" <input type="text"
class="form-control" class="form-control"
id="country" id="Country"
name="country" name="Country"
placeholder="Suriname"> placeholder="Suriname">
</div> </div>
+7 -3
View File
@@ -3,14 +3,18 @@
} }
@model List<Robot> @model List<Robot>
<h1 style="text-align: center;">LISTE DES ROBOTS RECHERCHÉS</h1>
<ul class="list-group"> <div style="margin-bottom: 20px; margin-top: 8%;">
<ul class="list-group" style="width: 20%; margin-left: auto; margin-right: auto; text-align: center;">
@foreach (Robot robot in Model){ @foreach (Robot robot in Model){
<li class="list-group-item"> <li class="list-group-item">
<a href="robots/robot?id=@robot.Id">@robot.Name</a> <a href="robots/robot?id=@robot.Id">@robot.Name</a>
</li> </li>
} }
</ul> </ul>
</div>
<div class="text-center">
<a class="btn btn-primary mt-2" href="/robots/createRobot"> Ajouter un robot</a> <a class="btn btn-primary mt-2" href="/robots/createRobot">Ajouter un robot</a>
</div>
@@ -3,4 +3,4 @@
} }
<h1>@ViewData["Title"]</h1> <h1>@ViewData["Title"]</h1>
<p>Use this page to detail your site's privacy policy.</p> <p>This isn't the droids you're looking for</p>
+66 -17
View File
@@ -1,29 +1,78 @@
@model Robot @model Robot
@{
string imageURL = "https://robohash.org/toaster" + Model.Id.ToString() + "?set=set2";
}
<div class="container"> <div class="container">
<div class="d-flex justify-content-center"> <div class="d-flex justify-content-center">
<div class="card" style="width: 10rem;"> <div class="card mb-3" style="width: 10rem;">
<img class="card-img-top" <img class="card-img-top" src="@imageURL">
src="https://robohash.org/aligator@Model.Id?set=set2">
</div> </div>
</div> </div>
</div> </div>
<table class="table"> <table class="table table-hover" style="width: 40%; margin-left: auto; margin-right: auto;">
<tr>
<th>Nom</th><td>@Model.Name</td>
</tr>
<tr> <thead class="thead-dark">
<th>Poids</th><td>@Model.Weight (kg)</td> <tbody>
</tr> <tr>
<th>Nom</th>
<td>@Model.Name</td>
</tr>
<tr> <tr>
<th>Taille</th><td>@Model.Height (m)</td> <th>Poids</th>
</tr> <td>@Model.Weight (kg)</td>
<tr> </tr>
<th>Pays</th><td>@Model.Country</td>
</tr>
</table> <tr>
<th>Taille</th>
<td>@Model.Height (m)</td>
</tr>
<tr>
<th>Pays</th>
<td>
<span>@Model.Country</span>
<button class="btn btn-primary ml-3" data-toggle="modal" data-target="#changeRobotCountryModal">
Modifier
</button>
</td>
</tr>
<tr>
<th>Pays visités</th>
<td>@string.Join(", ", Model.VisitedCountries)</td>
</tr>
</thead>
</tbody>
</table>
<!-- Modal -->
<div class="modal fade" id="changeRobotCountryModal" tabindex="-1" role="dialog"
aria-labelledby="changeRobotCountryModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form method="post" asp-action="ChangeRobotCountry" asp-route-robotId="@Model.Id">
<div class="modal-header">
<h5 class="modal-title" id="changeRobotCountryModalLabel">Modification du pays</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label for="newCountry">Entrez le nouveau pays :</label>
<input type="text" class="form-control" name="newCountry" id="newCountry" placeholder="Italie">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
<button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
</form>
</div>
</div>
</div>
+91 -82
View File
@@ -1,95 +1,104 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - RobotsWantedLeague</title> <title>@ViewData["Title"] - RobotsWantedLeague</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" /> <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" /> <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/RobotsWantedLeague.styles.css" asp-append-version="true" /> <link rel="stylesheet" href="~/RobotsWantedLeague.styles.css" asp-append-version="true" />
<script src="https://unpkg.com/htmx.org@1.9.5"` <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
integrity="sha384-xcuj3WpfgjlKF+FXhSQFQ0ZNr39ln+hwjN3npfM9VBnUskLolQAcN80McRIVOPuO"
crossorigin="anonymous"></script>
<script>
(function () {
function _isNumeric(str) {
if (typeof str != "string") {
return false
}
return !isNaN(str) && !isNaN(parseFloat(str));
}
function _cast(str) {
return _isNumeric(str) ? parseFloat(str) : str;
}
function _addAssocToDict(obj, key, value) { <script src="https://unpkg.com/htmx.org@1.9.5" `
let splittedKey = Array.isArray(key) ? key : key.split("."); integrity="sha384-xcuj3WpfgjlKF+FXhSQFQ0ZNr39ln+hwjN3npfM9VBnUskLolQAcN80McRIVOPuO"
const keyToAdd = splittedKey.shift(); crossorigin="anonymous"></script>
const isTerminal = splittedKey.length === 0; <script>
if (isTerminal) { (function () {
obj[keyToAdd] = _cast(value); function _isNumeric(str) {
return obj; if (typeof str != "string") {
} return false
if (!(keyToAdd in obj)) { }
obj[keyToAdd] = {}; return !isNaN(str) && !isNaN(parseFloat(str));
}
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) { function _cast(str) {
xhr.overrideMimeType('text/json'); return _isNumeric(str) ? parseFloat(str) : str;
return (JSON.stringify(_createPayload(parameters))); }
}
}); function _addAssocToDict(obj, key, value) {
})(); let splittedKey = Array.isArray(key) ? key : key.split(".");
</script> 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)));
}
});
})();
</script>
</head> </head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container-fluid">
<a class="navbar-brand" href="/">RobotsWantedLeague</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted"> <body>
<div class="container"> <header>
&copy; 2023 - RobotsWantedLeague - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a> <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container-fluid">
<a class="navbar-brand" href="/">RobotsWantedLeague</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
</div> </div>
</footer> </div>
<script src="~/lib/jquery/dist/jquery.min.js"></script> </nav>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script> </header>
<script src="~/js/site.js" asp-append-version="true"></script> <div class="container">
@await RenderSectionAsync("Scripts", required: false) <main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
&copy; 2023 - RobotsWantedLeague
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@await RenderSectionAsync("Scripts", required: false)
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body> </body>
</html> </html>