using Asp.Versioning;
using GerstITS.Data;
using GerstITS.Examples.Api.Versioning;
using GerstITS.Examples.Logic.Customers;
using GerstITS.Search;
using GerstITS.Validation;
using GerstITS.Web.Api;
using GerstITS.Web.Api.FluentApi;
using Microsoft.AspNetCore.Mvc;
namespace GerstITS.Examples.Api.Controllers._1._1;
///
/// Is responsible to manage customers backed by the repository based data access.
///
[ApiController,
ApiVersion(Versions._1_1),
Route(ApplicationEnvironment.WebApi.ControllerRouteTemplate)]
public class CustomerController : FluentApiControllerBase
{
#region Fields
private readonly ICustomerProvider _provider;
#endregion
#region Constructors
public CustomerController(ICustomerProvider provider,
IValidator validator)
: base(validator)
{
_provider = provider;
}
#endregion
#region Methods
///
/// Gets a customer by specified id. Deactivated customers are excluded by default.
///
/// Id of the customer.
/// Includes deactivated (soft deleted) customers if set to true.
/// Returns a customer.
[HttpGet,
Route("{id}")]
public IActionResult Get(int id, [FromQuery] bool includeInactive = false)
{
return Api().Use(id)
.Get(x => _provider.GetById(x, includeInactive));
}
///
/// Searches customers by specified filter using the search engine (paging, sorting, filtering).
///
/// Filter criteria of the search.
/// Returns the matching customers including the total count.
[HttpPost,
Route("Search")]
public IActionResult Search([FromBody] CustomerSearchFilter filter)
{
return Api().Use(filter)
.Get(_provider.Search);
}
///
/// Creates a new customer.
///
/// Customer to create.
/// Returns the created customer.
[HttpPost]
public IActionResult Create([FromBody] Customer customer)
{
return Api().Use(customer)
.Create(_provider.Create);
}
///
/// Updates an existing customer.
///
/// Customer to update.
/// Returns the updated customer.
[HttpPut]
public IActionResult Update([FromBody] Customer customer)
{
return Api().Use(customer)
.Update(_provider.Update);
}
///
/// Deletes a customer. Performs a soft delete by default, a permanent delete if force is set to true.
///
/// Id of the customer.
/// Deletes the customer permanently if set to true.
/// Returns true if the customer was deleted.
[HttpDelete,
Route("{id}")]
public IActionResult Delete(int id, [FromQuery] bool force = false)
{
return Api().Use(id)
.Delete(x => _provider.Delete(x, force));
}
///
/// Imports customers using bulk operations.
///
/// Customers to import.
/// Returns the bulk operation result.
[HttpPost,
Route("Import")]
public IActionResult Import([FromBody] IEnumerable customers)
{
return Api().Use(customers)
.Create(_provider.Import);
}
///
/// Gets all notes of a customer ordered by their validity.
///
/// Id of the customer.
/// Returns the notes of the customer.
[HttpGet,
Route("{id}/Notes")]
public IActionResult GetNotes(int id)
{
return Api().Use(id)
.Get(_provider.GetNotes);
}
///
/// Adds a historicized note to a customer.
///
/// Id of the customer.
/// Note to add.
/// Returns the created note.
[HttpPost,
Route("{id}/Notes")]
public IActionResult AddNote(int id, [FromBody] CustomerNote note)
{
return Api().Use(id)
.Create(x => _provider.AddNote(x, note.Text));
}
#endregion
}