From bf77e2a2c73de69c3ee2a9e5773905ee19891a82 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 30 Mar 2023 12:57:32 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B2=20AddSwaggerGen=20c=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=BE=D0=B2=D0=BE=D0=B3=D0=BE=20=D1=84=D0=BE=D1=80=D0=BC?= =?UTF-8?q?=D0=B0=D1=82=D0=B0=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20DateOnly?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/DailyReportController.cs | 8 +-- .../Converters/DateOnlyTypeConverter.cs | 48 ++++++++++++++++++ AsbCloudWebApi/DependencyInjection.cs | 3 +- AsbCloudWebApi/Extentions.cs | 49 +++---------------- AsbCloudWebApi/Startup.cs | 4 ++ 5 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 AsbCloudWebApi/Converters/DateOnlyTypeConverter.cs diff --git a/AsbCloudWebApi/Controllers/DailyReportController.cs b/AsbCloudWebApi/Controllers/DailyReportController.cs index 9f357e59..c02b8ff7 100644 --- a/AsbCloudWebApi/Controllers/DailyReportController.cs +++ b/AsbCloudWebApi/Controllers/DailyReportController.cs @@ -45,9 +45,9 @@ namespace AsbCloudWebApi.Controllers /// [HttpGet] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetListAsync(int idWell, DateTime? begin, DateTime? end, CancellationToken token) + public async Task 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 /// [HttpPut("{date}/bha")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] - public Task UpdateBhaAsync(int idWell, [Required] DateTime date, [Required] BhaDto dto, CancellationToken token) - => UpdateReportBlockAsync(idWell, date, dto, token); + public Task UpdateBhaAsync(int idWell, [Required] DateOnly? date, [Required] BhaDto dto, CancellationToken token) + => UpdateReportBlockAsync(idWell, DateTime.Now, dto, token); /// /// Сохранение изменений набора данных для формирования рапорта (безметражные работы) diff --git a/AsbCloudWebApi/Converters/DateOnlyTypeConverter.cs b/AsbCloudWebApi/Converters/DateOnlyTypeConverter.cs new file mode 100644 index 00000000..df826075 --- /dev/null +++ b/AsbCloudWebApi/Converters/DateOnlyTypeConverter.cs @@ -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 +} diff --git a/AsbCloudWebApi/DependencyInjection.cs b/AsbCloudWebApi/DependencyInjection.cs index f25a5494..25b66545 100644 --- a/AsbCloudWebApi/DependencyInjection.cs +++ b/AsbCloudWebApi/DependencyInjection.cs @@ -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(() => new OpenApiSchema { Type = "string", Format = "date" }); c.CustomOperationIds(e => { return $"{e.ActionDescriptor.RouteValues["action"]}"; diff --git a/AsbCloudWebApi/Extentions.cs b/AsbCloudWebApi/Extentions.cs index 1179c25e..aff16969 100644 --- a/AsbCloudWebApi/Extentions.cs +++ b/AsbCloudWebApi/Extentions.cs @@ -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> body; - - private List GetOrCreateNew(string paramName) + public static MvcOptions UseDateOnlyTimeOnlyStringConverters(this MvcOptions options) { - List par; - if (body.ContainsKey(paramName)) - par = body[paramName]; - else - { - par = new List(); - 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(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(); } } diff --git a/AsbCloudWebApi/Startup.cs b/AsbCloudWebApi/Startup.cs index 96d408a5..14d8db0f 100644 --- a/AsbCloudWebApi/Startup.cs +++ b/AsbCloudWebApi/Startup.cs @@ -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)