Skip to content

Commit

Permalink
Added Logging+
Browse files Browse the repository at this point in the history
  • Loading branch information
ergen35 committed Jun 23, 2021
1 parent 760d998 commit 5570f95
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 80 deletions.
79 changes: 4 additions & 75 deletions PoliceOp.API/Controllers/AgentsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public async Task<ActionResult<IEnumerable<Agent>>> GetAgents()
return await _context.Agents.ToListAsync();
}

logger.LogInformation("Liste::Agents");
return Unauthorized("Session ID is Required");
}

Expand All @@ -63,7 +64,7 @@ public async Task<ActionResult<IEnumerable<Agent>>> GetAgents()
await _context.Entry(agent).Reference(a => a.Biometrie).LoadAsync();


logger.LogInformation($"Agent @{agent.Matricule} Connecté ::{HttpContext.Connection.RemotePort}, {DateTime.Now.ToShortDateString()}");
logger.LogInformation($"Agent @{agent.Matricule}");

return agent;
}
Expand All @@ -87,87 +88,15 @@ public async Task<ActionResult<IEnumerable<Agent>>> GetAgents()
{
await _context.Entry(item).Reference(r => r.Residence).LoadAsync();
}

logger.LogInformation($"Liste:: Recherche Agent @Mot-Clé:: {keyword} |");

return Results;
}

return Unauthorized("Session ID Requis");
}

// PUT: api/Agents/5
[HttpPut("{id}")]
public async Task<IActionResult> PutAgent(int id, Agent agent)
{

if (await SessionExists(HttpContext))
{
if (id != agent.PersonneId)
{
return BadRequest();
}

_context.Entry(agent).State = EntityState.Modified;

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!AgentExists(id))
{
return NotFound();
}
else
{
throw;
}
}

return NoContent();
}

return Unauthorized("Session ID is Required");

}

// POST: api/Agents
[HttpPost]
public async Task<ActionResult<Agent>> PostAgent(Agent agent)
{
if (await SessionExists(HttpContext))
{

_context.Agents.Add(agent);
await _context.SaveChangesAsync();

return CreatedAtAction("GetAgent", new { id = agent.PersonneId }, agent);
}

return Unauthorized("Session ID is Required");
}

// DELETE: api/Agents/5
[HttpDelete("{id}")]
public async Task<ActionResult<Agent>> DeleteAgent(int id)
{
if (await SessionExists(HttpContext))
{
var agent = await _context.Agents.FindAsync(id);
if (agent == null)
{
return NotFound();
}

_context.Agents.Remove(agent);
await _context.SaveChangesAsync();

return agent;
}

return Unauthorized("Session ID is Required");
}

private bool AgentExists(int id)
{
return _context.Agents.Any(e => e.PersonneId == id);
Expand Down
10 changes: 9 additions & 1 deletion PoliceOp.API/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;


namespace PoliceOp.API.Controllers
Expand All @@ -19,11 +20,13 @@ public class AuthController : Controller
private readonly PoliceOpAPIContext _context;
private readonly Services.JWTServices jWTService;
private readonly IConfiguration configuration;
private readonly ILogger<AuthController> logger;


public AuthController(PoliceOpAPIContext context, IConfiguration configuration)
public AuthController(PoliceOpAPIContext context, IConfiguration configuration, ILogger<AuthController> logger)
{
this.configuration = configuration;
this.logger = logger;
jWTService = new Services.JWTServices(this.configuration);
_context = context;

Expand Down Expand Up @@ -62,15 +65,19 @@ public AuthController(PoliceOpAPIContext context, IConfiguration configuration)

SessionVM svm = new SessionVM() { SessionID = newSession.SessionID.ToString(), AgentID = agent.PersonneId };

logger.LogInformation($"Nouvelle Session Crée Par @{agent.Matricule}, {DateTime.Now}");
return Json(svm);
}
else
{
logger.LogWarning($"Echec de Création de Session @{agent.Matricule}, {DateTime.Now}, depuis @{HttpContext.Connection.RemoteIpAddress}");
return NotFound("Bad Match");
}
}
else
{
logger.LogError($"Echec de Création de Session @{mat}, {DateTime.Now}, depuis @{HttpContext.Connection.RemoteIpAddress}");

return NotFound("No Such Data");
}
}
Expand All @@ -95,6 +102,7 @@ public async Task<ActionResult<Session>> DestroySession(Guid uid)
_context.Sessions.Remove(session);
await _context.SaveChangesAsync();

logger.LogInformation($"Session {session.SessionID} detruite");
return Ok("Logout successfull");
}
}
Expand Down
8 changes: 7 additions & 1 deletion PoliceOp.API/Controllers/AvisRechercheController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using PoliceOp.API.Data;
using PoliceOp.Models;
using System;
Expand All @@ -20,10 +21,12 @@ public class AvisRechercheController : ControllerBase
private readonly PoliceOpAPIContext _context;
private readonly IConfiguration configuration;
private readonly Services.JWTServices jWTService;
private readonly ILogger<AvisRecherche> logger;

public AvisRechercheController(PoliceOpAPIContext context, IConfiguration configuration)
public AvisRechercheController(PoliceOpAPIContext context, IConfiguration configuration, ILogger<AvisRecherche> logger)
{
this.configuration = configuration;
this.logger = logger;
jWTService = new Services.JWTServices(this.configuration);
_context = context;
}
Expand All @@ -40,6 +43,8 @@ public async Task<ActionResult<IEnumerable<AvisRecherche>>> GetAvisRecherches()
{
item.PersonneRecherchee = await _context.Personnes.FindAsync(item.PersonneRechercheeId);
}

logger.LogInformation("Liste:: AvisRecherche");
return liar;
}

Expand Down Expand Up @@ -116,6 +121,7 @@ public async Task<ActionResult<AvisRecherche>> PostAvisRecherche(AvisRecherche a
_context.AvisRecherches.Add(avisRecherche);
await _context.SaveChangesAsync();

logger.LogInformation($"Nouvel AvisRecherche @Id{avisRecherche.UID}, le {DateTime.Now}");
return Ok();
}

Expand Down
12 changes: 10 additions & 2 deletions PoliceOp.API/Controllers/DiffusionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace PoliceOp.API.Controllers
{
Expand All @@ -17,10 +18,12 @@ public class DiffusionsController : ControllerBase
private readonly PoliceOpAPIContext _context;
private readonly IConfiguration configuration;
private readonly Services.JWTServices jWTService;
private readonly ILogger<DiffusionsController> logger;

public DiffusionsController(PoliceOpAPIContext context, IConfiguration configuration)
public DiffusionsController(PoliceOpAPIContext context, IConfiguration configuration, ILogger<DiffusionsController> logger)
{
_context = context;
this.logger = logger;
this.configuration = configuration;
jWTService = new Services.JWTServices(this.configuration);
}
Expand All @@ -32,6 +35,7 @@ public async Task<ActionResult<IEnumerable<Diffusion>>> GetDiffusions()

if (await SessionExists(HttpContext))
{
logger.LogInformation("Liste:: Diffusions");
return await _context.Diffusions.ToListAsync();
}

Expand All @@ -51,6 +55,8 @@ public async Task<ActionResult<Diffusion>> GetDiffusion(int id)
return NotFound();
}

logger.LogInformation($"Details Diffusion:: N°{id}");

return diffusion;
}

Expand Down Expand Up @@ -100,7 +106,9 @@ public async Task<ActionResult<Diffusion>> PostDiffusion(Diffusion diffusion)
_context.Diffusions.Add(diffusion);
await _context.SaveChangesAsync();

return CreatedAtAction("GetDiffusion", new { id = diffusion.DiffusionId }, diffusion);
logger.LogInformation($"Diffusion:: Ajouté @Id:: {diffusion.DiffusionId}");

return Ok();
}

return Unauthorized("Session ID is Required");
Expand Down
9 changes: 8 additions & 1 deletion PoliceOp.API/Controllers/IdentificationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;


namespace PoliceOp.API.Controllers
Expand All @@ -20,10 +21,12 @@ public class IdentificationController : ControllerBase
private readonly PoliceOpAPIContext _context;
private readonly IConfiguration configuration;
private readonly Services.JWTServices jWTService;
private readonly ILogger<IdentificationController> logger;

public IdentificationController(PoliceOpAPIContext context, IConfiguration configuration)
public IdentificationController(PoliceOpAPIContext context, IConfiguration configuration, ILogger<IdentificationController> logger)
{
_context = context;
this.logger = logger;
this.configuration = configuration;
jWTService = new Services.JWTServices(this.configuration);
}
Expand All @@ -34,6 +37,7 @@ public async Task<ActionResult<IEnumerable<Personne>>> GetPersonnes()
{
if (await SessionExists(HttpContext))
{
logger.LogInformation("Liste:: Personnes");
return await _context.Personnes.ToListAsync();
}

Expand All @@ -57,6 +61,7 @@ public async Task<ActionResult<Personne>> GetPersonne(int id)
await _context.Entry(personne).Reference(p => p.Residence).LoadAsync();
await _context.Entry(personne).Reference(p => p.Biometrie).LoadAsync();

logger.LogInformation($"Personne:: {personne.UID}");
return personne;
}

Expand All @@ -81,6 +86,8 @@ public async Task<ActionResult<IEnumerable<Personne>>> SearchFor(string keyword)
await _context.Entry(item).Reference(r => r.Residence).LoadAsync();
}

logger.LogInformation($"Liste:: Recherche Personne @Mot-Clé:: {keyword} |");

return Results;
}

Expand Down

0 comments on commit 5570f95

Please sign in to comment.