Adding visited countries by robot + it stays #2

Closed
marcdescotes wants to merge 7 commits from main into main
pull from: main
18 changed files with 481 additions and 139 deletions

No files matched your search

@@ -0,0 +1,61 @@
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using RobotsWantedLeague.Models;
using RobotsWantedLeague.Services;
using System.ComponentModel.DataAnnotations;
namespace RobotsWantedLeague.Controllers;
public class AgentRequest
{
public string Name { get; set; }
public string Continent { get; set; }
}
public class AgentsController : Controller
{
private readonly ILogger<AgentsController> _logger;
private readonly IAgentsService agentsService;
public AgentsController(ILogger<AgentsController> logger,
IAgentsService agentsService)
{
_logger = logger;
this.agentsService = agentsService;
}
public IActionResult Index()
{
return View(agentsService.Agents);
}
public IActionResult Agent(int id)
{
Agent? agent = agentsService.GetAgentById(id);
if (agent == null)
{
return NotFound();
}
return View(agent);
}
[HttpGet]
public IActionResult CreateAgent()
{
return View();
}
[HttpPost]
public IActionResult CreateAgent([FromBody] AgentRequest agent)
{
Agent r = agentsService.CreateAgent(agent.Name,
agent.Continent);
string htmxRedirectHeaderName = "HX-Redirect";
string redirectURL = "/agents/agent?id=" + r.Id;
Response.Headers.Add(htmxRedirectHeaderName, redirectURL);
return Ok();
}
}
@@ -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,41 @@ 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,
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 });
}
}
+17
View File
@@ -0,0 +1,17 @@
namespace RobotsWantedLeague.Models;
public class Agent
{
public int Id { get; set; }
public string Name { get; set; }
public string Continent { get; set; }
public Agent(int Id, string Name, string Continent)
{
this.Id = Id;
this.Name = Name;
this.Continent = Continent;
}
}
+14 -17
View File
@@ -1,27 +1,24 @@
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 string NewCountry { get; set; }
public List<string> VisitedCountries { get; set; } = new List<string>();
/**
* 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;
}
}
+1
View File
@@ -5,6 +5,7 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IRobotsService, NotEmptyRobotsService>();
builder.Services.AddSingleton<IAgentsService, NotEmptyAgentsService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
@@ -0,0 +1,57 @@
namespace RobotsWantedLeague.Services;
using RobotsWantedLeague.Models;
public class AgentsService: IAgentsService
{
private readonly List<Agent> agents;
private int idGenerator = 0;
public List<Agent> Agents { get => agents; }
public AgentsService()
{
agents = new List<Agent>();
}
private int generateId(){
idGenerator = idGenerator + 1;
return idGenerator;
}
public Agent CreateAgent(string name,
string continent)
{
var agent = new Agent(generateId(), name, continent);
agents.Add(agent);
return agent;
}
private int getIndexOfAgentById(int id){
int idx = 0;
foreach (Agent agent in agents){
if (agent.Id == id){
return idx;
}
idx ++;
}
return -1;
}
public Agent? GetAgentById(int id){
int indexToDelete = getIndexOfAgentById(id);
if(indexToDelete == -1){
return null;
}
return agents[indexToDelete];
}
public bool DeleteAgentById(int id){
int indexToDelete = getIndexOfAgentById(id);
if(indexToDelete == -1){
return false;
}
agents.RemoveAt(indexToDelete);
return true;
}
}
@@ -0,0 +1,14 @@
namespace RobotsWantedLeague.Services;
using RobotsWantedLeague.Models;
public interface IAgentsService
{
public List<Agent> Agents { get; }
public Agent CreateAgent(string name,
string continent);
public Agent? GetAgentById(int id);
public bool DeleteAgentById(int id);
}
@@ -0,0 +1,38 @@
namespace RobotsWantedLeague.Services;
using RobotsWantedLeague.Models;
public class NotEmptyAgentsService : IAgentsService
{
private readonly IAgentsService underlyingAgentsService;
public List<Agent> Agents { get => underlyingAgentsService.Agents; }
public NotEmptyAgentsService()
{
this.underlyingAgentsService = new AgentsService();
this.underlyingAgentsService.CreateAgent("Mulder", "Amérique du Nord");
this.underlyingAgentsService.CreateAgent("Scully", "Amérique du Sud");
this.underlyingAgentsService.CreateAgent("Doggett", "Antarctique");
this.underlyingAgentsService.CreateAgent("Reyes", "Europe");
this.underlyingAgentsService.CreateAgent("Skinner", "Asie");
this.underlyingAgentsService.CreateAgent("Spender", "Afrique");
this.underlyingAgentsService.CreateAgent("Kersh", "Océanie");
}
public Agent CreateAgent(string name,
string continent)
{
return underlyingAgentsService.CreateAgent(name, continent);
}
public Agent? GetAgentById(int id)
{
return underlyingAgentsService.GetAgentById(id);
}
public bool DeleteAgentById(int id)
{
return underlyingAgentsService.DeleteAgentById(id);
}
}
@@ -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);
}
@@ -2,7 +2,7 @@ namespace RobotsWantedLeague.Services;
using RobotsWantedLeague.Models;
public class NotEmptyRobotsService: IRobotsService
public class NotEmptyRobotsService : IRobotsService
{
private readonly IRobotsService underlyingRobotsService;
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);
}
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;
}
}
}
@@ -0,0 +1,37 @@
@model Agent
@{
string imageURL = "https://good-hawk-21.deno.dev/toaster" + Model.Id.ToString();
}
<div class="container">
<div class="d-flex justify-content-center">
<div class="card mb-3" style="width: 10rem;">
<iframe class="card-img-top" src="@imageURL"></iframe>
</div>
</div>
</div>
<table class="table table-hover" style="width: 40%; margin-left: auto; margin-right: auto;">
<thead>
<tbody>
<tr>
<th>Id</th>
<td>@Model.Id</td>
</tr>
<tr>
<th>Nom</th>
<td>@Model.Name</td>
</tr>
<tr>
<th>Continent</th>
<td>@Model.Continent</td>
</tr>
</thead>
</tbody>
</table>
@@ -0,0 +1,17 @@
@{
ViewData["Title"] = "Agent List";
}
@model List<Agent>
<h1 style="text-align: center;">LISTE DES AGENTS</h1>
<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 (Agent agent in Model){
<li class="list-group-item">
<a href="agents/agent?id=@agent.Id">@agent.Name</a>
</li>
}
</ul>
</div>
@@ -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>
+7 -3
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>
<a class="btn btn-primary mt-2" href="/robots/createRobot"> Ajouter un robot</a>
<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>
+66 -17
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-hover" style="width: 40%; margin-left: auto; margin-right: auto;">
<table class="table">
<tr>
<th>Nom</th><td>@Model.Name</td>
</tr>
<thead>
<tbody>
<tr>
<th>Nom</th>
<td>@Model.Name</td>
</tr>
<tr>
<th>Poids</th><td>@Model.Weight (kg)</td>
</tr>
<tr>
<th>Poids</th>
<td>@Model.Weight (kg)</td>
</tr>
<tr>
<th>Taille</th><td>@Model.Height (m)</td>
</tr>
<tr>
<th>Pays</th><td>@Model.Country</td>
</tr>
<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>
</table>
@Html.ValidationSummary(true)
<!-- 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" data-val="true">
</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>
+96 -82
View File
@@ -1,95 +1,109 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - RobotsWantedLeague</title>
<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" />
<script src="https://unpkg.com/htmx.org@1.9.5"`
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));
}
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - RobotsWantedLeague</title>
<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" />
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";
<script src="https://unpkg.com/htmx.org@1.9.5" `
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));
}
},
encodeParameters: function (xhr, parameters, elt) {
xhr.overrideMimeType('text/json');
return (JSON.stringify(_createPayload(parameters)));
}
});
})();
</script>
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)));
}
});
})();
</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">
<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">
<div class="container">
&copy; 2023 - RobotsWantedLeague - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
<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>
<a class="navbar-brand" href="/agents">Agents</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>
</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)
</div>
</nav>
</header>
<div class="container">
<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>
</html>