forked from ddrilling/AsbCloudServer
Настройка в AddSwaggerGen cтрокового формата данных для DateOnly
This commit is contained in:
parent
7fdb47e4cc
commit
bf77e2a2c7
@ -45,9 +45,9 @@ namespace AsbCloudWebApi.Controllers
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[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, null, null, token);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
@ -94,8 +94,8 @@ namespace AsbCloudWebApi.Controllers
|
||||
/// <returns></returns>
|
||||
[HttpPut("{date}/bha")]
|
||||
[ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)]
|
||||
public Task<IActionResult> UpdateBhaAsync(int idWell, [Required] DateTime date, [Required] BhaDto dto, CancellationToken token)
|
||||
=> UpdateReportBlockAsync(idWell, date, dto, token);
|
||||
public Task<IActionResult> UpdateBhaAsync(int idWell, [Required] DateOnly? date, [Required] BhaDto dto, CancellationToken token)
|
||||
=> UpdateReportBlockAsync(idWell, DateTime.Now, dto, token);
|
||||
|
||||
/// <summary>
|
||||
/// Сохранение изменений набора данных для формирования рапорта (безметражные работы)
|
||||
|
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
|
||||
}
|
@ -3,11 +3,9 @@ using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -19,6 +17,7 @@ namespace AsbCloudWebApi
|
||||
{
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.MapType<DateOnly>(() => new OpenApiSchema { Type = "string", Format = "date" });
|
||||
c.CustomOperationIds(e =>
|
||||
{
|
||||
return $"{e.ActionDescriptor.RouteValues["action"]}";
|
||||
|
@ -1,9 +1,10 @@
|
||||
using AsbCloudApp.Data;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AsbCloudWebApi.Converters;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
namespace Microsoft.AspNetCore.Mvc
|
||||
{
|
||||
public static class Extentions
|
||||
{
|
||||
@ -38,48 +39,12 @@ namespace Microsoft.AspNetCore.Mvc
|
||||
});
|
||||
}
|
||||
|
||||
public static BadRequestBuilder BadRequestBuilder(this ControllerBase controller, string paramName, params string[] errors)
|
||||
=> new BadRequestBuilder(paramName, errors);
|
||||
}
|
||||
|
||||
public class BadRequestBuilder
|
||||
{
|
||||
private readonly Dictionary<string, List<string>> body;
|
||||
|
||||
private List<string> GetOrCreateNew(string paramName)
|
||||
public static MvcOptions UseDateOnlyTimeOnlyStringConverters(this MvcOptions options)
|
||||
{
|
||||
List<string> par;
|
||||
if (body.ContainsKey(paramName))
|
||||
par = body[paramName];
|
||||
else
|
||||
{
|
||||
par = new List<string>();
|
||||
body[paramName] = par;
|
||||
}
|
||||
return par;
|
||||
TypeDescriptor.AddAttributes(typeof(DateOnly), new TypeConverterAttribute(typeof(DateOnlyTypeConverter)));
|
||||
return options;
|
||||
}
|
||||
|
||||
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.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace AsbCloudWebApi
|
||||
{
|
||||
@ -31,6 +32,8 @@ namespace AsbCloudWebApi
|
||||
}))
|
||||
.AddProtoBufNet();
|
||||
|
||||
services.AddControllers(options => options.UseDateOnlyTimeOnlyStringConverters());
|
||||
|
||||
ProtobufModel.EnshureRegistered();
|
||||
|
||||
services.AddSwagger();
|
||||
@ -90,6 +93,7 @@ namespace AsbCloudWebApi
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
|
Loading…
Reference in New Issue
Block a user