forked from itsecd/enterprise-development
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookController.cs
More file actions
108 lines (104 loc) · 4.74 KB
/
BookController.cs
File metadata and controls
108 lines (104 loc) · 4.74 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Library.Application.Contracts.Books;
using Library.Application.Contracts.BookIssues;
using Library.Application.Contracts.EditionTypes;
using Library.Application.Contracts.Publishers;
using Microsoft.AspNetCore.Mvc;
namespace Library.Api.Host.Controllers;
/// <summary>
/// Контроллер для работы с книгами
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class BookController(
IBookService bookService,
ILogger<BookController> logger)
: CrudControllerBase<BookDto, BookCreateUpdateDto, int>(bookService, logger)
{
/// <summary>
/// Возвращает записи о выдачах книги
/// </summary>
/// <param name="id">Идентификатор книги</param>
/// <returns>Список DTO для получения выдач книг</returns>
[HttpGet("{id}/Issues")]
[ProducesResponseType(typeof(IList<BookIssueDto>), 200)]
[ProducesResponseType(404)]
[ProducesResponseType(500)]
public async Task<ActionResult<IList<BookIssueDto>>> GetIssues(int id)
{
logger.LogInformation("{method} method of {controller} is called with {id} parameter", nameof(GetIssues), GetType().Name, id);
try
{
var res = await bookService.GetIssues(id);
logger.LogInformation("{method} method of {controller} executed successfully", nameof(GetIssues), GetType().Name);
return Ok(res);
}
catch (KeyNotFoundException ex)
{
logger.LogWarning("A not found exception happened during {method} method of {controller}: {@exception}", nameof(GetIssues), GetType().Name, ex);
return NotFound(ex.Message);
}
catch (Exception ex)
{
logger.LogError("An exception happened during {method} method of {controller}: {@exception}", nameof(GetIssues), GetType().Name, ex);
return StatusCode(500, $"{ex.Message}\n\r{ex.InnerException?.Message}");
}
}
/// <summary>
/// Возвращает вид издания книги
/// </summary>
/// <param name="id">Идентификатор книги</param>
/// <returns>DTO для получения вида издания</returns>
[HttpGet("{id}/EditionType")]
[ProducesResponseType(typeof(EditionTypeDto), 200)]
[ProducesResponseType(404)]
[ProducesResponseType(500)]
public async Task<ActionResult<EditionTypeDto>> GetEditionType(int id)
{
logger.LogInformation("{method} method of {controller} is called with {id} parameter", nameof(GetEditionType), GetType().Name, id);
try
{
var res = await bookService.GetEditionType(id);
logger.LogInformation("{method} method of {controller} executed successfully", nameof(GetEditionType), GetType().Name);
return Ok(res);
}
catch (KeyNotFoundException ex)
{
logger.LogWarning("A not found exception happened during {method} method of {controller}: {@exception}", nameof(GetEditionType), GetType().Name, ex);
return NotFound(ex.Message);
}
catch (Exception ex)
{
logger.LogError("An exception happened during {method} method of {controller}: {@exception}", nameof(GetEditionType), GetType().Name, ex);
return StatusCode(500, $"{ex.Message}\n\r{ex.InnerException?.Message}");
}
}
/// <summary>
/// Возвращает издательство книги
/// </summary>
/// <param name="id">Идентификатор книги</param>
/// <returns>DTO для получения издательства</returns>
[HttpGet("{id}/Publisher")]
[ProducesResponseType(typeof(PublisherDto), 200)]
[ProducesResponseType(404)]
[ProducesResponseType(500)]
public async Task<ActionResult<PublisherDto>> GetPublisher(int id)
{
logger.LogInformation("{method} method of {controller} is called with {id} parameter", nameof(GetPublisher), GetType().Name, id);
try
{
var res = await bookService.GetPublisher(id);
logger.LogInformation("{method} method of {controller} executed successfully", nameof(GetPublisher), GetType().Name);
return Ok(res);
}
catch (KeyNotFoundException ex)
{
logger.LogWarning("A not found exception happened during {method} method of {controller}: {@exception}", nameof(GetPublisher), GetType().Name, ex);
return NotFound(ex.Message);
}
catch (Exception ex)
{
logger.LogError("An exception happened during {method} method of {controller}: {@exception}", nameof(GetPublisher), GetType().Name, ex);
return StatusCode(500, $"{ex.Message}\n\r{ex.InnerException?.Message}");
}
}
}