Files
idoklad/Controllers/ContactsController.cs
T
2026-07-14 06:21:37 +02:00

68 lines
2.7 KiB
C#

using IdokladSdk.Models.Contact;
using Microsoft.AspNetCore.Mvc;
using Idoklad.Services;
namespace Idoklad.Controllers;
/// <summary>
/// Contacts (customers/suppliers) agenda.
///
/// All endpoints require iDoklad credentials. Provide them as request headers
/// (<c>X-ClientId</c>, <c>X-ClientSecret</c>, <c>X-ApplicationId</c>) or rely on the service
/// environment defaults. See the Swagger description for details.
/// </summary>
[ApiController]
[Route("contacts")]
[Produces("application/json")]
[Tags("Contacts")]
public sealed class ContactsController : ControllerBase
{
private readonly ContactsService _service;
public ContactsController(ContactsService service) => _service = service;
/// <summary>
/// List contacts (paged, optionally filtered and sorted).
/// </summary>
/// <param name="filter">
/// Optional iDoklad-style filter, e.g. <c>(IdentificationNumber~eq~12345678)</c> or
/// <c>(CompanyName~ct~s.r.o.)</c>. Operators: eq, neq, gt, gte, lt, lte, ct, nct.
/// </param>
/// <param name="filtertype">How multiple conditions combine: <c>and</c> (default) or <c>or</c>.</param>
/// <param name="sort">Optional sort, e.g. <c>CompanyName~asc</c>.</param>
[HttpGet]
public async Task<IActionResult> List(
[FromQuery] int page = 1,
[FromQuery] int pageSize = 20,
[FromQuery] string? filter = null,
[FromQuery] string? filtertype = null,
[FromQuery] string? sort = null,
CancellationToken ct = default)
=> Ok(await _service.ListAsync(page, pageSize, filter, filtertype, sort, ct));
/// <summary>Get a contact detail by id.</summary>
[HttpGet("{id:int}")]
public async Task<IActionResult> Detail(int id, CancellationToken ct)
=> Ok(await _service.DetailAsync(id, ct));
/// <summary>Get a pre-filled default contact model for creating a new contact.</summary>
[HttpGet("default")]
public async Task<IActionResult> Default(CancellationToken ct)
=> Ok(await _service.DefaultAsync(ct));
/// <summary>Create a new contact.</summary>
[HttpPost]
public async Task<IActionResult> Create([FromBody] ContactPostModel model, CancellationToken ct)
=> Ok(await _service.CreateAsync(model, ct));
/// <summary>Update an existing contact (the model id identifies the contact).</summary>
[HttpPatch]
public async Task<IActionResult> Update([FromBody] ContactPatchModel model, CancellationToken ct)
=> Ok(await _service.UpdateAsync(model, ct));
/// <summary>Delete a contact by id.</summary>
[HttpDelete("{id:int}")]
public async Task<IActionResult> Delete(int id, CancellationToken ct)
=> Ok(await _service.DeleteAsync(id, ct));
}