forked from ddrilling/AsbCloudServer
Настройка в AddSwaggerGen cтрокового формата данных для DateOnly
This commit is contained in:
parent
7fdb47e4cc
commit
6f08629966
@ -29,7 +29,7 @@ namespace AsbCloudApp.Data.DailyReport
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// дата рапорта
|
/// дата рапорта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime ReportDate { get; set; }
|
public DateOnly ReportDate { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// глубина забоя на дату начала интервала
|
/// глубина забоя на дату начала интервала
|
||||||
|
@ -20,7 +20,7 @@ namespace AsbCloudApp.Services
|
|||||||
/// <param name="end"></param>
|
/// <param name="end"></param>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<DailyReportDto>> GetListAsync(int idWell, DateTime? begin, DateTime? end, CancellationToken cancellationToken);
|
Task<IEnumerable<DailyReportDto>> GetListAsync(int idWell, DateOnly? begin, DateOnly? end, CancellationToken cancellationToken);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Добавить новый рапорт
|
/// Добавить новый рапорт
|
||||||
@ -30,7 +30,7 @@ namespace AsbCloudApp.Services
|
|||||||
/// <param name="idUser"></param>
|
/// <param name="idUser"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> AddAsync(int idWell, DateTime startDate, int idUser, CancellationToken token);
|
Task<int> AddAsync(int idWell, DateOnly startDate, int idUser, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Сформировать файл рапорта
|
/// Сформировать файл рапорта
|
||||||
@ -39,7 +39,7 @@ namespace AsbCloudApp.Services
|
|||||||
/// <param name="date"></param>
|
/// <param name="date"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<Stream?> MakeReportAsync(int idWell, DateTime date, CancellationToken token);
|
Task<Stream?> MakeReportAsync(int idWell, DateOnly date, CancellationToken token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// изменить блок данных для суточного рапорта
|
/// изменить блок данных для суточного рапорта
|
||||||
@ -49,6 +49,6 @@ namespace AsbCloudApp.Services
|
|||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<int> UpdateBlockAsync(int idWell, DateTime startDate, ItemInfoDto dto, CancellationToken token);
|
Task<int> UpdateBlockAsync(int idWell, DateOnly startDate, ItemInfoDto dto, CancellationToken token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -258,4 +260,17 @@ namespace AsbCloudDb
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public class DateOnlyJsonConverter : JsonConverter<DateOnly>
|
||||||
|
{
|
||||||
|
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return DateOnly.FromDateTime(reader.GetDateTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
var isoDate = value.ToString("O");
|
||||||
|
writer.WriteStringValue(isoDate);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace AsbCloudDb.Model.DailyReport
|
namespace AsbCloudDb.Model.DailyReport
|
||||||
{
|
{
|
||||||
public class Head : ItemInfo
|
public class Head : ItemInfo
|
||||||
@ -26,7 +28,8 @@ namespace AsbCloudDb.Model.DailyReport
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// дата рапорта
|
/// дата рапорта
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime ReportDate { get; set; }
|
[JsonConverter(typeof(DateOnlyJsonConverter))]
|
||||||
|
public DateOnly ReportDate { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// глубина забоя на дату начала интервала
|
/// глубина забоя на дату начала интервала
|
||||||
|
@ -39,7 +39,7 @@ namespace AsbCloudInfrastructure.Services.DailyReport
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<DailyReportDto>> GetListAsync(int idWell, DateTime? begin, DateTime? end, CancellationToken token)
|
public async Task<IEnumerable<DailyReportDto>> GetListAsync(int idWell, DateOnly? begin, DateOnly? end, CancellationToken token)
|
||||||
{
|
{
|
||||||
var well = wellService.GetOrDefault(idWell);
|
var well = wellService.GetOrDefault(idWell);
|
||||||
if (well is null || well.Timezone is null)
|
if (well is null || well.Timezone is null)
|
||||||
@ -49,14 +49,12 @@ namespace AsbCloudInfrastructure.Services.DailyReport
|
|||||||
|
|
||||||
if (begin is not null)
|
if (begin is not null)
|
||||||
{
|
{
|
||||||
var beginDateOnly = ExtractDate(begin.Value, well);
|
query = query.Where(d => d.StartDate >= begin);
|
||||||
query = query.Where(d => d.StartDate >= beginDateOnly);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end is not null)
|
if (end is not null)
|
||||||
{
|
{
|
||||||
var endDateOnly = ExtractDate(end.Value, well);
|
query = query.Where(d => d.StartDate <= end);
|
||||||
query = query.Where(d => d.StartDate <= endDateOnly);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var entities = await query.OrderByDescending(e => e.StartDate)
|
var entities = await query.OrderByDescending(e => e.StartDate)
|
||||||
@ -89,23 +87,22 @@ namespace AsbCloudInfrastructure.Services.DailyReport
|
|||||||
return factOperations;
|
return factOperations;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> AddAsync(int idWell, DateTime startDate, int idUser, CancellationToken token)
|
public async Task<int> AddAsync(int idWell, DateOnly startDate, int idUser, CancellationToken token)
|
||||||
{
|
{
|
||||||
var well = wellService.GetOrDefault(idWell);
|
var well = wellService.GetOrDefault(idWell);
|
||||||
if (well is null)
|
if (well is null)
|
||||||
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
|
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
|
||||||
|
|
||||||
var startDateOnly = ExtractDate(startDate, well);
|
|
||||||
|
|
||||||
var hasEntity = await db.DailyReports
|
var hasEntity = await db.DailyReports
|
||||||
.AnyAsync(r => r.IdWell == idWell && r.StartDate == startDateOnly, token);
|
.AnyAsync(r => r.IdWell == idWell && r.StartDate == startDate, token);
|
||||||
if (hasEntity)
|
if (hasEntity)
|
||||||
throw new ArgumentInvalidException($"daily report on {startDateOnly} already exists", nameof(startDateOnly));
|
throw new ArgumentInvalidException($"daily report on {startDate} already exists", nameof(startDate));
|
||||||
|
|
||||||
var entity = new AsbCloudDb.Model.DailyReport.DailyReport
|
var entity = new AsbCloudDb.Model.DailyReport.DailyReport
|
||||||
{
|
{
|
||||||
IdWell = idWell,
|
IdWell = idWell,
|
||||||
StartDate = startDateOnly,
|
StartDate = startDate,
|
||||||
Info = new DailyReportInfo()
|
Info = new DailyReportInfo()
|
||||||
{
|
{
|
||||||
Head = CreateHeadDailyReportBlock(well, startDate, idUser)
|
Head = CreateHeadDailyReportBlock(well, startDate, idUser)
|
||||||
@ -116,14 +113,13 @@ namespace AsbCloudInfrastructure.Services.DailyReport
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> UpdateBlockAsync(int idWell, DateTime startDate, ItemInfoDto dto, CancellationToken token)
|
public async Task<int> UpdateBlockAsync(int idWell, DateOnly startDate, ItemInfoDto dto, CancellationToken token)
|
||||||
{
|
{
|
||||||
var well = wellService.GetOrDefault(idWell);
|
var well = wellService.GetOrDefault(idWell);
|
||||||
if (well is null)
|
if (well is null)
|
||||||
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
|
throw new ArgumentInvalidException("idWell doesn`t exist", nameof(idWell));
|
||||||
|
|
||||||
var startDateOnly = DateOnly.FromDateTime(startDate);
|
var entity = await db.DailyReports.FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate == startDate, token);
|
||||||
var entity = await db.DailyReports.FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate == startDateOnly, token);
|
|
||||||
|
|
||||||
if (entity is null)
|
if (entity is null)
|
||||||
throw new ArgumentInvalidException("Daily report doesn`t exist", nameof(startDate));
|
throw new ArgumentInvalidException("Daily report doesn`t exist", nameof(startDate));
|
||||||
@ -145,7 +141,7 @@ namespace AsbCloudInfrastructure.Services.DailyReport
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Stream?> MakeReportAsync(int idWell, DateTime date, CancellationToken token)
|
public async Task<Stream?> MakeReportAsync(int idWell, DateOnly date, CancellationToken token)
|
||||||
{
|
{
|
||||||
var stageIds = WellOperationCategory.WorkStages.Select(w => w.Id).ToArray();
|
var stageIds = WellOperationCategory.WorkStages.Select(w => w.Id).ToArray();
|
||||||
var wellOperationCategories = wellOperationRepository.GetCategories(true)
|
var wellOperationCategories = wellOperationRepository.GetCategories(true)
|
||||||
@ -161,14 +157,10 @@ namespace AsbCloudInfrastructure.Services.DailyReport
|
|||||||
return memoryStream;
|
return memoryStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<DailyReportDto?> GetOrDefaultAsync(int idWell, DateTime date, CancellationToken token)
|
private async Task<DailyReportDto?> GetOrDefaultAsync(int idWell, DateOnly date, CancellationToken token)
|
||||||
{
|
{
|
||||||
var dateOffset = date.Date;
|
|
||||||
var entity = await db.DailyReports
|
var entity = await db.DailyReports
|
||||||
.FirstOrDefaultAsync(r => r.IdWell == idWell &&
|
.FirstOrDefaultAsync(r => r.IdWell == idWell && r.StartDate == date, token);
|
||||||
r.StartDate.Year == dateOffset.Year &&
|
|
||||||
r.StartDate.DayOfYear == dateOffset.DayOfYear
|
|
||||||
, token);
|
|
||||||
|
|
||||||
if (entity is null)
|
if (entity is null)
|
||||||
throw new ArgumentInvalidException("Daily report doesn`t exist", nameof(date));
|
throw new ArgumentInvalidException("Daily report doesn`t exist", nameof(date));
|
||||||
@ -222,19 +214,6 @@ namespace AsbCloudInfrastructure.Services.DailyReport
|
|||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Приведение данных к формату DateOnly с учетом часового пояса скважины
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dateTime"></param>
|
|
||||||
/// <param name="well"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private DateOnly ExtractDate(DateTime dateTime, WellDto well)
|
|
||||||
{
|
|
||||||
var dateTimeOffset = dateTime.ToUtcDateTimeOffset(well!.Timezone.Hours);
|
|
||||||
var date = new DateOnly(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day);
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Создание блока "Заголовок" по умолчанию
|
/// Создание блока "Заголовок" по умолчанию
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -242,14 +221,14 @@ namespace AsbCloudInfrastructure.Services.DailyReport
|
|||||||
/// <param name="startDate"></param>
|
/// <param name="startDate"></param>
|
||||||
/// <param name="idUser"></param>
|
/// <param name="idUser"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private Head CreateHeadDailyReportBlock(WellDto well, DateTime startDate, int idUser)
|
private Head CreateHeadDailyReportBlock(WellDto well, DateOnly startDate, int idUser)
|
||||||
{
|
{
|
||||||
var customer = well.Companies.FirstOrDefault(company => company.IdCompanyType == 1);
|
var customer = well.Companies.FirstOrDefault(company => company.IdCompanyType == 1);
|
||||||
var contractor = well.Companies.FirstOrDefault(company => company.IdCompanyType == 2);
|
var contractor = well.Companies.FirstOrDefault(company => company.IdCompanyType == 2);
|
||||||
|
|
||||||
return new Head()
|
return new Head()
|
||||||
{
|
{
|
||||||
ReportDate = startDate.Date,
|
ReportDate = startDate,
|
||||||
WellName = well.Caption,
|
WellName = well.Caption,
|
||||||
ClusterName = well?.Cluster ?? string.Empty,
|
ClusterName = well?.Cluster ?? string.Empty,
|
||||||
Customer = customer?.Caption ?? string.Empty,
|
Customer = customer?.Caption ?? string.Empty,
|
||||||
|
@ -45,7 +45,7 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[ProducesResponseType(typeof(IEnumerable<DailyReportDto>), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(IEnumerable<DailyReportDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||||
public async Task<IActionResult> GetListAsync(int idWell, DateTime? begin, DateTime? end, CancellationToken token)
|
public async Task<IActionResult> GetListAsync(int idWell, DateOnly? begin, DateOnly? end, CancellationToken token)
|
||||||
{
|
{
|
||||||
var result = await dailyReportService.GetListAsync(idWell, begin, end, token);
|
var result = await dailyReportService.GetListAsync(idWell, begin, end, token);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
@ -60,7 +60,7 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||||
public async Task<IActionResult> AddAsync(int idWell, [Required] DateTime startDate, CancellationToken token)
|
public async Task<IActionResult> AddAsync(int idWell, [Required] DateOnly startDate, CancellationToken token)
|
||||||
{
|
{
|
||||||
if (!await UserHasAccesToWellAsync(idWell, token))
|
if (!await UserHasAccesToWellAsync(idWell, token))
|
||||||
return Forbid();
|
return Forbid();
|
||||||
@ -81,7 +81,7 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPut("{date}/head")]
|
[HttpPut("{date}/head")]
|
||||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||||
public Task<IActionResult> UpdateHeadAsync(int idWell, [Required] DateTime date, [Required] HeadDto dto, CancellationToken token)
|
public Task<IActionResult> UpdateHeadAsync(int idWell, [Required] DateOnly date, [Required] HeadDto dto, CancellationToken token)
|
||||||
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -94,7 +94,7 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPut("{date}/bha")]
|
[HttpPut("{date}/bha")]
|
||||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||||
public Task<IActionResult> UpdateBhaAsync(int idWell, [Required] DateTime date, [Required] BhaDto dto, CancellationToken token)
|
public Task<IActionResult> UpdateBhaAsync(int idWell, [Required] DateOnly date, [Required] BhaDto dto, CancellationToken token)
|
||||||
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -107,7 +107,7 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPut("{date}/noDrilling")]
|
[HttpPut("{date}/noDrilling")]
|
||||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||||
public Task<IActionResult> UpdateNoDrillingAsync(int idWell, [Required] DateTime date, [Required] NoDrillingDto dto, CancellationToken token)
|
public Task<IActionResult> UpdateNoDrillingAsync(int idWell, [Required] DateOnly date, [Required] NoDrillingDto dto, CancellationToken token)
|
||||||
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -120,7 +120,7 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPut("{date}/saub")]
|
[HttpPut("{date}/saub")]
|
||||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||||
public Task<IActionResult> UpdateSaubAsync(int idWell, [Required] DateTime date, [Required] SaubDto dto, CancellationToken token)
|
public Task<IActionResult> UpdateSaubAsync(int idWell, [Required] DateOnly date, [Required] SaubDto dto, CancellationToken token)
|
||||||
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -133,7 +133,7 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPut("{date}/sign")]
|
[HttpPut("{date}/sign")]
|
||||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||||
public Task<IActionResult> UpdateSignAsync(int idWell, [Required] DateTime date, [Required] SignDto dto, CancellationToken token)
|
public Task<IActionResult> UpdateSignAsync(int idWell, [Required] DateOnly date, [Required] SignDto dto, CancellationToken token)
|
||||||
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -144,7 +144,7 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
/// <param name="dto"></param>
|
/// <param name="dto"></param>
|
||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private async Task<IActionResult> UpdateReportBlockAsync(int idWell, DateTime date, ItemInfoDto dto, CancellationToken token)
|
private async Task<IActionResult> UpdateReportBlockAsync(int idWell, DateOnly date, ItemInfoDto dto, CancellationToken token)
|
||||||
{
|
{
|
||||||
if (!await UserHasAccesToWellAsync(idWell, token))
|
if (!await UserHasAccesToWellAsync(idWell, token))
|
||||||
return Forbid();
|
return Forbid();
|
||||||
@ -165,7 +165,7 @@ namespace AsbCloudWebApi.Controllers
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("{date}/excel")]
|
[HttpGet("{date}/excel")]
|
||||||
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
[ProducesResponseType(typeof(PhysicalFileResult), (int)System.Net.HttpStatusCode.OK)]
|
||||||
public async Task<IActionResult> DownloadAsync(int idWell, DateTime date, CancellationToken token)
|
public async Task<IActionResult> DownloadAsync(int idWell, DateOnly date, CancellationToken token)
|
||||||
{
|
{
|
||||||
if (!await UserHasAccesToWellAsync(idWell, token))
|
if (!await UserHasAccesToWellAsync(idWell, token))
|
||||||
return Forbid();
|
return Forbid();
|
||||||
|
48
AsbCloudWebApi/Converters/DateOnlyTypeConverter.cs
Normal file
48
AsbCloudWebApi/Converters/DateOnlyTypeConverter.cs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
namespace AsbCloudWebApi.Converters
|
||||||
|
{
|
||||||
|
#nullable enable
|
||||||
|
public class DateOnlyTypeConverter : TypeConverter
|
||||||
|
{
|
||||||
|
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
|
||||||
|
{
|
||||||
|
if (sourceType == typeof(string))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return base.CanConvertFrom(context, sourceType);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
|
||||||
|
{
|
||||||
|
if (value is string str)
|
||||||
|
{
|
||||||
|
return DateOnly.Parse(str);
|
||||||
|
}
|
||||||
|
return base.ConvertFrom(context, culture, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
|
||||||
|
{
|
||||||
|
if (destinationType == typeof(string))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return base.CanConvertTo(context, destinationType);
|
||||||
|
}
|
||||||
|
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
|
||||||
|
{
|
||||||
|
if (destinationType == typeof(string) && value is DateOnly date)
|
||||||
|
{
|
||||||
|
return date.ToString("O");
|
||||||
|
}
|
||||||
|
return base.ConvertTo(context, culture, value, destinationType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#nullable disable
|
||||||
|
}
|
@ -5,7 +5,7 @@ using System.Globalization;
|
|||||||
namespace AsbCloudWebApi.Converters
|
namespace AsbCloudWebApi.Converters
|
||||||
{
|
{
|
||||||
#nullable enable
|
#nullable enable
|
||||||
public class DateOnlyTypeConverter : TypeConverter
|
public class TimeOnlyTypeConverter : TypeConverter
|
||||||
{
|
{
|
||||||
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
|
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
|
||||||
{
|
{
|
||||||
|
@ -3,11 +3,9 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -19,6 +17,7 @@ namespace AsbCloudWebApi
|
|||||||
{
|
{
|
||||||
services.AddSwaggerGen(c =>
|
services.AddSwaggerGen(c =>
|
||||||
{
|
{
|
||||||
|
c.MapType<DateOnly>(() => new OpenApiSchema { Type = "string", Format = "date" });
|
||||||
c.CustomOperationIds(e =>
|
c.CustomOperationIds(e =>
|
||||||
{
|
{
|
||||||
return $"{e.ActionDescriptor.RouteValues["action"]}";
|
return $"{e.ActionDescriptor.RouteValues["action"]}";
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
using System.Collections.Generic;
|
using AsbCloudWebApi.Converters;
|
||||||
using System.Linq;
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Mvc
|
namespace Microsoft.AspNetCore.Mvc
|
||||||
{
|
{
|
||||||
public static class Extentions
|
public static class Extentions
|
||||||
{
|
{
|
||||||
@ -38,48 +39,12 @@ namespace Microsoft.AspNetCore.Mvc
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BadRequestBuilder BadRequestBuilder(this ControllerBase controller, string paramName, params string[] errors)
|
public static MvcOptions UseDateOnlyTimeOnlyStringConverters(this MvcOptions options)
|
||||||
=> new BadRequestBuilder(paramName, errors);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class BadRequestBuilder
|
|
||||||
{
|
|
||||||
private readonly Dictionary<string, List<string>> body;
|
|
||||||
|
|
||||||
private List<string> GetOrCreateNew(string paramName)
|
|
||||||
{
|
{
|
||||||
List<string> par;
|
TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyTypeConverter)));
|
||||||
if (body.ContainsKey(paramName))
|
return options;
|
||||||
par = body[paramName];
|
|
||||||
else
|
|
||||||
{
|
|
||||||
par = new List<string>();
|
|
||||||
body[paramName] = par;
|
|
||||||
}
|
|
||||||
return par;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BadRequestBuilder(string paramName, params string[] errors)
|
|
||||||
{
|
|
||||||
body = new();
|
|
||||||
body[paramName] = new List<string>(errors);
|
|
||||||
}
|
|
||||||
|
|
||||||
public BadRequestBuilder Add(string paramName, params string[] errors)
|
|
||||||
{
|
|
||||||
var par = GetOrCreateNew(paramName);
|
|
||||||
par.AddRange(errors);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public BadRequestObjectResult Build()
|
|
||||||
{
|
|
||||||
var o = body.Select(e => new { name = e.Key, errors = e.Value.ToArray() });
|
|
||||||
|
|
||||||
return new BadRequestObjectResult(o);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator BadRequestObjectResult(BadRequestBuilder d) => d.Build();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Hosting;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace AsbCloudWebApi
|
namespace AsbCloudWebApi
|
||||||
{
|
{
|
||||||
@ -31,6 +32,8 @@ namespace AsbCloudWebApi
|
|||||||
}))
|
}))
|
||||||
.AddProtoBufNet();
|
.AddProtoBufNet();
|
||||||
|
|
||||||
|
services.AddControllers(options => options.UseDateOnlyTimeOnlyStringConverters());
|
||||||
|
|
||||||
ProtobufModel.EnshureRegistered();
|
ProtobufModel.EnshureRegistered();
|
||||||
|
|
||||||
services.AddSwagger();
|
services.AddSwagger();
|
||||||
@ -90,6 +93,7 @@ namespace AsbCloudWebApi
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
Loading…
Reference in New Issue
Block a user