Merge pull request #1 from marcdescotes/listeAgents

Liste agents
This commit is contained in:
Marc-André Descôteaux authored and GitHub committed 2023-09-25 09:09:56 -04:00
commit 70ddba0054
15 files changed
+253 -7

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();
}
}
@@ -51,10 +51,7 @@ public class RobotsController : Controller
[HttpPost] [HttpPost]
public IActionResult CreateRobot([FromBody] RobotRequest robot) public IActionResult CreateRobot([FromBody] RobotRequest robot)
{ {
if (!ModelState.IsValid)
{
return View(robot);
}
Robot r = robotsService.CreateRobot(robot.Name, Robot r = robotsService.CreateRobot(robot.Name,
robot.Weight, robot.Weight,
robot.Height, robot.Height,
+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;
}
}
+2
View File
@@ -7,8 +7,10 @@ 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 List<string> VisitedCountries { get; set; } = new List<string>(); 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.Id = Id;
+1
View File
@@ -5,6 +5,7 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IRobotsService, NotEmptyRobotsService>(); builder.Services.AddSingleton<IRobotsService, NotEmptyRobotsService>();
builder.Services.AddSingleton<IAgentsService, NotEmptyAgentsService>();
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // 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);
}
}
@@ -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>
+3 -3
View File
@@ -12,10 +12,9 @@
</div> </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>
<tbody> <tbody>
<tr> <tr>
<th>Nom</th> <th>Nom</th>
@@ -49,6 +48,7 @@
</table> </table>
@Html.ValidationSummary(true)
<!-- Modal --> <!-- Modal -->
<div class="modal fade" id="changeRobotCountryModal" tabindex="-1" role="dialog" <div class="modal fade" id="changeRobotCountryModal" tabindex="-1" role="dialog"
@@ -65,7 +65,7 @@
<div class="modal-body"> <div class="modal-body">
<div class="form-group"> <div class="form-group">
<label for="newCountry">Entrez le nouveau pays :</label> <label for="newCountry">Entrez le nouveau pays :</label>
<input type="text" class="form-control" name="newCountry" id="newCountry" placeholder="Italie"> <input type="text" class="form-control" name="NewCountry" id="newCountry" placeholder="Italie" data-val="true">
</div> </div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
@@ -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>