feat: Implemento el subendpoint de POST para las lineas de facturas.

This commit is contained in:
javiermengual 2026-01-02 21:05:46 +01:00
parent a06ac2bdd9
commit 2e1062cc71
2 changed files with 29 additions and 0 deletions

View File

@ -62,4 +62,16 @@ public class InvoicesController : ControllerBase
await _invoiceService.UpdateInvoiceAsync(id, updateInvoiceDto); await _invoiceService.UpdateInvoiceAsync(id, updateInvoiceDto);
return NoContent(); return NoContent();
} }
[HttpPost("{id:int}/lines")]
[ProducesResponseType(typeof(string), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<string>> AddInvoiceLine([Range(1, int.MaxValue)] int id, [FromBody] CreateInvoiceLineDto lineDto)
{
var result = await _invoiceService.AddInvoiceLineAsync(id, lineDto);
return CreatedAtAction(nameof(GetInvoice), new { id }, result);
}
} }

View File

@ -68,6 +68,22 @@ public class InvoiceService
return int.Parse(responseBody); return int.Parse(responseBody);
} }
public async Task<string> AddInvoiceLineAsync(int invoiceId, CreateInvoiceLineDto lineDto)
{
var invoice = await _apiClient.GetResourceAsync<InvoiceDetailResponse>($"invoices/{invoiceId}");
if (invoice.statut != "0") throw new ForbiddenException("Solo se pueden añadir líneas a facturas en borrador");
var requestBody = new
{
desc = lineDto.Description,
qty = lineDto.Quantity.ToString(CultureInfo.InvariantCulture),
subprice = lineDto.UnitPrice.ToString(CultureInfo.InvariantCulture),
tva_tx = lineDto.TaxRate.ToString(CultureInfo.InvariantCulture)
};
return await _apiClient.PostAsync($"invoices/{invoiceId}/lines", requestBody);
}
public async Task UpdateInvoiceAsync(int id, UpdateInvoiceDto dto) public async Task UpdateInvoiceAsync(int id, UpdateInvoiceDto dto)
{ {
// GET el JSON completo // GET el JSON completo
@ -85,4 +101,5 @@ public class InvoiceService
await _apiClient.PutAsync($"invoices/{id}", current); await _apiClient.PutAsync($"invoices/{id}", current);
} }
} }