PI2024-23 #3

Merged
s464958 merged 2 commits from PI2024-23 into master 2024-06-16 01:16:02 +02:00
7 changed files with 67 additions and 2 deletions

View File

@ -22,6 +22,8 @@ using NHibernate.Driver;
using FirmTracker_Server.Controllers; using FirmTracker_Server.Controllers;
using FirmTracker_Server.nHibernate.Products; using FirmTracker_Server.nHibernate.Products;
using FirmTracker_Server.nHibernate; using FirmTracker_Server.nHibernate;
using FirmTracker_Server.Utilities.Converters;
using FirmTracker_Server.Utilities.Swagger;
namespace FirmTracker_Server namespace FirmTracker_Server
{ {
@ -60,10 +62,18 @@ namespace FirmTracker_Server
.AllowAnyHeader() .AllowAnyHeader()
.AllowAnyMethod()); .AllowAnyMethod());
}); });
builder.Services.AddControllers(); builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter());
});
;
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen(c =>
{
c.SchemaFilter<SwaggerDateTimeSchemaFilter>();
});
var app = builder.Build(); var app = builder.Build();
var configSwagger = new ConfigurationBuilder() var configSwagger = new ConfigurationBuilder()

View File

@ -0,0 +1,28 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace FirmTracker_Server.Utilities.Converters
{
public class DateTimeConverter : JsonConverter<DateTime>
{
private const string DateFormat = "yyyy-MM-ddTHH:mm";
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (DateTime.TryParseExact(reader.GetString(), DateFormat, null, System.Globalization.DateTimeStyles.None, out var date))
{
return date;
}
}
throw new JsonException("Invalid date format");
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(DateFormat));
}
}
}

View File

@ -0,0 +1,18 @@
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace FirmTracker_Server.Utilities.Swagger
{
public class SwaggerDateTimeSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
if (context.Type == typeof(DateTime) || context.Type == typeof(DateTime?))
{
schema.Format = "yyyy-MM-ddTHH:mm";
schema.Example = new OpenApiString(DateTime.Now.ToString("yyyy-MM-ddTHH:mm"));
}
}
}
}

View File

@ -16,12 +16,14 @@
*/ */
using FirmTracker_Server.nHibernate.Reports; using FirmTracker_Server.nHibernate.Reports;
using System.ComponentModel.DataAnnotations;
namespace FirmTracker_Server.nHibernate.Expenses namespace FirmTracker_Server.nHibernate.Expenses
{ {
public class Expense public class Expense
{ {
public virtual int Id { get; set; } public virtual int Id { get; set; }
[DataType(DataType.DateTime)]
public virtual DateTime Date { get; set; } public virtual DateTime Date { get; set; }
public virtual decimal Value { get; set; } public virtual decimal Value { get; set; }
public virtual string Description { get; set; } public virtual string Description { get; set; }

View File

@ -18,13 +18,16 @@
using FirmTracker_Server.nHibernate.Expenses; using FirmTracker_Server.nHibernate.Expenses;
using FirmTracker_Server.nHibernate.Transactions; using FirmTracker_Server.nHibernate.Transactions;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
namespace FirmTracker_Server.nHibernate.Reports namespace FirmTracker_Server.nHibernate.Reports
{ {
public class Report public class Report
{ {
public virtual int Id { get; set; } public virtual int Id { get; set; }
[DataType(DataType.DateTime)]
public virtual DateTime FromDate { get; set; } public virtual DateTime FromDate { get; set; }
[DataType(DataType.DateTime)]
public virtual DateTime ToDate { get; set; } public virtual DateTime ToDate { get; set; }
public virtual decimal TotalIncome { get; set; } public virtual decimal TotalIncome { get; set; }
public virtual decimal TotalExpenses { get; set; } public virtual decimal TotalExpenses { get; set; }

View File

@ -2,6 +2,7 @@
using FirmTracker_Server.nHibernate.Reports; using FirmTracker_Server.nHibernate.Reports;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
namespace FirmTracker_Server.nHibernate.Transactions namespace FirmTracker_Server.nHibernate.Transactions
@ -9,6 +10,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
public class Transaction public class Transaction
{ {
public virtual int Id { get; set; } public virtual int Id { get; set; }
[DataType(DataType.DateTime)]
public virtual DateTime Date { get; set; } public virtual DateTime Date { get; set; }
public virtual int EmployeeId { get; set; } public virtual int EmployeeId { get; set; }
public virtual IList<TransactionProduct> TransactionProducts { get; set; } = new List<TransactionProduct>(); public virtual IList<TransactionProduct> TransactionProducts { get; set; } = new List<TransactionProduct>();

View File

@ -2,6 +2,7 @@
using FirmTracker_Server.nHibernate.Reports; using FirmTracker_Server.nHibernate.Reports;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
namespace FirmTracker_Server.nHibernate.Transactions namespace FirmTracker_Server.nHibernate.Transactions
@ -9,6 +10,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
public class Transaction2 public class Transaction2
{ {
public virtual int Id { get; set; } public virtual int Id { get; set; }
[DataType(DataType.DateTime)]
public virtual DateTime Date { get; set; } public virtual DateTime Date { get; set; }
public virtual int EmployeeId { get; set; } public virtual int EmployeeId { get; set; }
public virtual IList<TransactionWithProducts> TransactionProducts { get; set; } = new List<TransactionWithProducts>(); public virtual IList<TransactionWithProducts> TransactionProducts { get; set; } = new List<TransactionWithProducts>();