Pasted, changed labels for agents instead of robot
This commit is contained in:
1 parent
a4f4206f3b
commit
0c0392d0be
14 files changed
+249
-18
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,19 +8,13 @@ namespace RobotsWantedLeague.Controllers;
|
|||||||
|
|
||||||
public class RobotRequest
|
public class RobotRequest
|
||||||
{
|
{
|
||||||
|
|
||||||
public string Country { get; set; }
|
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 class RobotsController : Controller
|
public class RobotsController : Controller
|
||||||
{
|
{
|
||||||
private readonly ILogger<RobotsController> _logger;
|
private readonly ILogger<RobotsController> _logger;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -7,7 +7,6 @@ public class Robot
|
|||||||
public int Weight { get; set; }
|
public int Weight { get; set; }
|
||||||
public int Height { get; set; }
|
public int Height { get; set; }
|
||||||
public string Country { get; set; }
|
public string Country { get; set; }
|
||||||
|
|
||||||
public string NewCountry { get; set; }
|
public string NewCountry { get; set; }
|
||||||
public List<string> VisitedCountries { get; set; } = new List<string>();
|
public List<string> VisitedCountries { get; set; } = new List<string>();
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,40 @@
|
|||||||
|
@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 class="thead-dark">
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<th>Nom</th>
|
||||||
|
<td>@Model.Name</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<th>Id</th>
|
||||||
|
<td>@Model.Id</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<th>Continent</th>
|
||||||
|
<td>@Model.Continent</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
</thead>
|
||||||
|
</tbody>
|
||||||
|
|
||||||
|
</table>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
@{
|
||||||
|
ViewData["Title"] = "Home Page";
|
||||||
|
}
|
||||||
|
@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>
|
||||||
|
|
||||||
@@ -12,17 +12,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@* <div class="container">
|
|
||||||
<div class="d-flex justify-content-center">
|
|
||||||
<div class="card mb-3" style="width: 10rem;">
|
|
||||||
<iframe class="card-img-top" src="https://good-hawk-21.deno.dev/ljl"></iframe>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div> *@
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<table class="table table-hover" style="width: 40%; margin-left: auto; margin-right: auto;">
|
<table class="table table-hover" style="width: 40%; margin-left: auto; margin-right: auto;">
|
||||||
|
|
||||||
<thead class="thead-dark">
|
<thead class="thead-dark">
|
||||||
|
|||||||
@@ -71,6 +71,11 @@
|
|||||||
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
|
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<a class="navbar-brand" href="/">RobotsWantedLeague</a>
|
<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"
|
<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">
|
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
||||||
<span class="navbar-toggler-icon"></span>
|
<span class="navbar-toggler-icon"></span>
|
||||||
|
|||||||
Reference in new issue
Block a user