forked from itsecd/cloud-development
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreditController.cs
More file actions
61 lines (55 loc) · 2.14 KB
/
CreditController.cs
File metadata and controls
61 lines (55 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using CreditApp.Api.Services;
using CreditApp.Domain.Data;
using Microsoft.AspNetCore.Mvc;
namespace CreditApp.Api.Controllers;
/// <summary>
/// Контроллер для работы с кредитными заявками
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class CreditController(
ICreditService creditService,
ILogger<CreditController> logger)
: ControllerBase
{
/// <summary>
/// Получить кредитную заявку по идентификатору
/// </summary>
[HttpGet]
[ProducesResponseType(typeof(CreditApplication), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<CreditApplication>> Get(
int id,
CancellationToken cancellationToken)
{
try
{
if (id <= 0)
{
logger.LogWarning("Invalid credit application ID: {CreditId}", id);
return BadRequest("Id must be positive number");
}
logger.LogInformation("Requesting credit application {CreditId}", id);
var result = await creditService.GetAsync(id, cancellationToken);
if (result == null)
{
logger.LogWarning("Credit application {CreditId} not found", id);
return NotFound($"Credit application with ID {id} not found");
}
logger.LogInformation("Successfully retrieved credit application {CreditId}", id);
return Ok(result);
}
catch (OperationCanceledException)
{
logger.LogWarning("Request for credit application {CreditId} was cancelled", id);
return StatusCode(499, "Request cancelled by client");
}
catch (Exception ex)
{
logger.LogError(ex, "Error retrieving credit application {CreditId}: {ErrorMessage}", id, ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError, "Internal server error");
}
}
}