Adding visited countries by robot + it stays

This commit is contained in:
Marc-André Descôteaux committed 2023-09-20 20:22:33 -04:00
1 parent 7ff304dadf
commit c3fe66ce2b
10 files changed
+135 -39

No files matched your search

@@ -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,19 +32,29 @@ 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)
{
if (!ModelState.IsValid)
{
return View(robot);
}
Robot r = robotsService.CreateRobot(robot.Name,
robot.Weight,
robot.Height,
@@ -52,4 +64,12 @@ public class RobotsController : Controller
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 });
}
}
+6 -11
View File
@@ -1,22 +1,16 @@
namespace RobotsWantedLeague.Models;
public class Robot{
public class Robot
{
public int Id { get; set; }
public string Name { get; set; }
/**
* Weight in kg.
*/
public int Weight { get; set; }
/**
* Height in m.
*/
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){
public Robot(int Id, string Name, int Weight, int Height, string Country)
{
this.Id = Id;
this.Name = Name;
this.Weight = Weight;
@@ -24,4 +18,5 @@ public class Robot{
this.Country = Country;
}
}
@@ -1,15 +1,17 @@
namespace RobotsWantedLeague.Services;
using RobotsWantedLeague.Models;
public interface IRobotsService{
public interface IRobotsService
{
public List<Robot> 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);
}
@@ -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);
}
}
@@ -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;
}
}
}
@@ -1,7 +1,7 @@
<form hx-post="/robots/createRobot"
hx-ext="json-enc"
hx-trigger="submit">
hx-trigger="submit"
style="width: 40%; margin-left: auto; margin-right: auto;">
<div class="form-group">
<label for="name">Le nom du robot</label>
<input type="text"
@@ -34,8 +34,8 @@
<label for="country">Pays</label>
<input type="text"
class="form-control"
id="country"
name="country"
id="Country"
name="Country"
placeholder="Suriname">
</div>
+6 -2
View File
@@ -3,14 +3,18 @@
}
@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){
<li class="list-group-item">
<a href="robots/robot?id=@robot.Id">@robot.Name</a>
</li>
}
</ul>
</div>
<div class="text-center">
<a class="btn btn-primary mt-2" href="/robots/createRobot">Ajouter un robot</a>
</div>
@@ -3,4 +3,4 @@
}
<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>
+57 -8
View File
@@ -1,29 +1,78 @@
@model Robot
@{
string imageURL = "https://robohash.org/toaster" + Model.Id.ToString() + "?set=set2";
}
<div class="container">
<div class="d-flex justify-content-center">
<div class="card" style="width: 10rem;">
<img class="card-img-top"
src="https://robohash.org/aligator@Model.Id?set=set2">
<div class="card mb-3" style="width: 10rem;">
<img class="card-img-top" src="@imageURL">
</div>
</div>
</div>
<table class="table">
<table class="table table-hover" style="width: 40%; margin-left: auto; margin-right: auto;">
<thead class="thead-dark">
<tbody>
<tr>
<th>Nom</th><td>@Model.Name</td>
<th>Nom</th>
<td>@Model.Name</td>
</tr>
<tr>
<th>Poids</th><td>@Model.Weight (kg)</td>
<th>Poids</th>
<td>@Model.Weight (kg)</td>
</tr>
<tr>
<th>Taille</th><td>@Model.Height (m)</td>
<th>Taille</th>
<td>@Model.Height (m)</td>
</tr>
<tr>
<th>Pays</th><td>@Model.Country</td>
<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>
+12 -3
View File
@@ -1,5 +1,6 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
@@ -7,6 +8,9 @@
<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="~/RobotsWantedLeague.styles.css" asp-append-version="true" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
<script src="https://unpkg.com/htmx.org@1.9.5" `
integrity="sha384-xcuj3WpfgjlKF+FXhSQFQ0ZNr39ln+hwjN3npfM9VBnUskLolQAcN80McRIVOPuO"
crossorigin="anonymous"></script>
@@ -61,13 +65,14 @@
</script>
</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">
<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">
@@ -84,12 +89,16 @@
<footer class="border-top footer text-muted">
<div class="container">
&copy; 2023 - RobotsWantedLeague - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
&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>
</html>