-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathCourseContractsController.cs
More file actions
88 lines (83 loc) · 3.78 KB
/
CourseContractsController.cs
File metadata and controls
88 lines (83 loc) · 3.78 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
using System.ComponentModel.DataAnnotations;
using CourseGenerator.Api.Dto;
using CourseGenerator.Api.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace CourseGenerator.Api.Controllers;
[ApiController]
[Route("api/courses")]
public sealed class CourseContractsController(
ICourseContractsService contractsService,
ICourseContractGenerator contractGenerator) : ControllerBase
{
/// <summary>
/// Генерирует список контрактов курсов с кэшированием результата в Redis.
/// </summary>
/// <param name="count">Количество контрактов для генерации (от 1 до 100).</param>
/// <param name="cancellationToken">Токен отмены запроса.</param>
/// <returns>Список сгенерированных контрактов курсов.</returns>
/// <response code="200">Контракты успешно получены.</response>
/// <response code="400">Передан недопустимый параметр count.</response>
[HttpGet("generate")]
[ProducesResponseType(typeof(IReadOnlyList<CourseContractDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<IReadOnlyList<CourseContractDto>>> GenerateAsync(
[FromQuery, Range(1, 100)] int count,
CancellationToken cancellationToken)
{
try
{
var contracts = await contractsService.GenerateAsync(count, cancellationToken);
var dto = contracts
.Select(contract => new CourseContractDto(
contract.Id,
contract.CourseName,
contract.TeacherFullName,
contract.StartDate,
contract.EndDate,
contract.MaxStudents,
contract.CurrentStudents,
contract.HasCertificate,
contract.Price,
contract.Rating))
.ToList();
return Ok(dto);
}
catch (ArgumentOutOfRangeException ex)
{
var problem = new ValidationProblemDetails(new Dictionary<string, string[]>
{
["count"] = [ex.Message]
});
return BadRequest(problem);
}
}
/// <summary>
/// Возвращает один сгенерированный контракт по идентификатору для совместимости с клиентом.
/// </summary>
/// <param name="id">Неотрицательный идентификатор объекта.</param>
/// <param name="cancellationToken">Токен отмены запроса.</param>
/// <returns>Сгенерированный контракт.</returns>
/// <response code="200">Контракт успешно получен.</response>
/// <response code="400">Передан недопустимый параметр id.</response>
[HttpGet("by-id")]
[ProducesResponseType(typeof(CourseContractDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public ActionResult<CourseContractDto> GetByIdAsync(
[FromQuery, Range(0, int.MaxValue)] int id,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
var contract = contractGenerator.GenerateById(id);
return Ok(new CourseContractDto(
contract.Id,
contract.CourseName,
contract.TeacherFullName,
contract.StartDate,
contract.EndDate,
contract.MaxStudents,
contract.CurrentStudents,
contract.HasCertificate,
contract.Price,
contract.Rating));
}
}