Initial code commit.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.UI.Server.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// Represent the base controller class.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
[ApiController]
|
||||
[Route("api/v{version:apiVersion}/[controller]")]
|
||||
public abstract class BaseApiController<T> : ControllerBase
|
||||
{
|
||||
private IMediator? _mediatorInstance;
|
||||
private ILogger<T>? _loggerInstance;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the mediator for requests/responses.
|
||||
/// </summary>
|
||||
protected IMediator Mediator => _mediatorInstance ??= HttpContext.RequestServices.GetRequiredService<IMediator>();
|
||||
/// <summary>
|
||||
/// Gets the logger for current controller.
|
||||
/// </summary>
|
||||
protected ILogger<T> Logger => _loggerInstance ??= HttpContext.RequestServices.GetRequiredService<ILogger<T>>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net.Mime;
|
||||
using System.Text.Json;
|
||||
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.UI.Server.Controllers.v1
|
||||
{
|
||||
[ApiVersion(1.0)]
|
||||
public class BeaconController : BaseApiController<BeaconController>
|
||||
{
|
||||
[HttpGet]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var beaconData = new Dictionary<string, string>();
|
||||
|
||||
if (Request.Method == "GET")
|
||||
{
|
||||
foreach (var param in Request.Query)
|
||||
{
|
||||
beaconData[param.Key] = param.Value.ToString();
|
||||
}
|
||||
}
|
||||
else if (Request.Method == "POST")
|
||||
{
|
||||
var contentType = Request.ContentType?.ToLower();
|
||||
|
||||
if (contentType?.Contains(MediaTypeNames.Application.Json) == true)
|
||||
{
|
||||
using var reader = new StreamReader(Request.Body);
|
||||
var body = await reader.ReadToEndAsync();
|
||||
var jsonData = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(body)!;
|
||||
|
||||
foreach (var kvp in jsonData)
|
||||
{
|
||||
beaconData[kvp.Key] = kvp.Value.ToString();
|
||||
}
|
||||
}
|
||||
else if (contentType?.Contains(MediaTypeNames.Application.FormUrlEncoded) == true)
|
||||
{
|
||||
var form = await Request.ReadFormAsync();
|
||||
foreach (var kvp in form)
|
||||
{
|
||||
beaconData[kvp.Key] = kvp.Value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Sufi.Demo.PeopleDirectory.Application.Features.Contacts.Commands;
|
||||
using Sufi.Demo.PeopleDirectory.Application.Features.Contacts.Queries.GetAll;
|
||||
using Sufi.Demo.PeopleDirectory.Application.Features.Contacts.Queries.GetById;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.UI.Server.Controllers.v1
|
||||
{
|
||||
/// <summary>
|
||||
/// A controller for manipulating contact data.
|
||||
/// </summary>
|
||||
[ApiVersion(1.0)]
|
||||
public class ContactsController : BaseApiController<ContactsController>
|
||||
{
|
||||
/// <summary>
|
||||
/// Get all contacts.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetAll() => Ok(await Mediator.Send(new GetAllContactsQuery()));
|
||||
|
||||
/// <summary>
|
||||
/// Get a contact by id.
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> Get(int id) => Ok(await Mediator.Send(new GetContactByIdQuery { Id = id }));
|
||||
|
||||
/// <summary>
|
||||
/// Create/Update a contact.
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Post(AddEditContactCommand request) => Ok(await Mediator.Send(request));
|
||||
|
||||
/// <summary>
|
||||
/// Delete a contact.
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete("{id}")]
|
||||
public async Task<IActionResult> Delete([Required] int id)
|
||||
{
|
||||
var command = new DeleteContactCommand { Id = id };
|
||||
return Ok(await Mediator.Send(command));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.UI.Server.Controllers.v1
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides API endpoints for infrastructure-related operations.
|
||||
/// </summary>
|
||||
/// <remarks>This controller is part of the infrastructure layer and includes endpoints for monitoring and diagnostics.</remarks>
|
||||
[ApiVersion(1.0)]
|
||||
public class InfraController(
|
||||
ILogger<InfraController> logger
|
||||
) : BaseApiController<InfraController>
|
||||
{
|
||||
/// <summary>
|
||||
/// Simulates a ping to check if the server is responsive.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Route("ping")]
|
||||
[HttpGet]
|
||||
public IActionResult Ping()
|
||||
{
|
||||
logger.LogInformation("InfraController.Ping method called.");
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Sufi.Demo.PeopleDirectory.UI.Server.Controllers.v2
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides API endpoints for infrastructure-related operations.
|
||||
/// </summary>
|
||||
/// <remarks>This controller is part of the infrastructure layer and includes endpoints for monitoring and diagnostics.</remarks>
|
||||
[ApiVersion(2.0)]
|
||||
public class InfraController : BaseApiController<InfraController>
|
||||
{
|
||||
/// <summary>
|
||||
/// Simulates a ping to check if the server is responsive.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[Route("ping")]
|
||||
[HttpGet]
|
||||
public IActionResult Ping() => Ok("Server is OK");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user