add some shared types

This commit is contained in:
honzapatCZ 2024-09-03 12:03:43 +02:00
parent 77a536ee79
commit abfa14de23
5 changed files with 141 additions and 0 deletions

34
Models/Api/Error.cs Normal file
View File

@ -0,0 +1,34 @@
using System.Text.Json.Serialization;
using NejCommon.Models;
namespace NejCommon.Models
{
/// <summary>
/// There was an error
/// </summary>
public partial class Error
{
public Error()
{
}
public Error(string message, decimal? statusCode = null, object? _Error = null)
{
this.Message = message;
this.StatusCode = statusCode;
this._Error = _Error;
}
public string Message { get; set; } = null!;
public decimal? StatusCode { get; set; }
/// <summary>
/// Gets or Sets _Error
/// </summary>
[JsonPropertyName("error")]
public object? _Error { get; set; }
}
}

29
Models/Api/Pagination.cs Normal file
View File

@ -0,0 +1,29 @@
using System.ComponentModel.DataAnnotations;
namespace NejCommon.Models;
public class Pagination
{
public int Offset { get; set; } = 0;
const int maxCount = 50;
private int _count = 10;
[Range(1, 100)]
public int Count
{
get
{
return _count;
}
set
{
_count = (value > maxCount) ? maxCount : value;
}
}
}
public class PaginationResponse<TType>{
public int TotalCount { get; set; }
public int Offset { get; set; }
public int Count { get; set; }
public IAsyncEnumerable<TType> Data { get; set; } = default!;
}

43
Models/DateOnlyFrame.cs Normal file
View File

@ -0,0 +1,43 @@
using System.Linq.Expressions;
using Microsoft.AspNetCore.Mvc;
namespace NejAccountingAPI.Models;
public class DateOnlyFrame
{
public DateOnlyFrame()
{
}
public DateOnlyFrame(DateOnly fromDate, DateOnly toDate)
{
this.fromDate = fromDate;
this.toDate = toDate;
}
private DateOnly _fromDate = DateOnly.MinValue;
private DateOnly _toDate = DateOnly.MaxValue;
public DateOnly fromDate { get { return _fromDate; } set { _fromDate = value; Invalid = false; } }
public DateOnly toDate { get { return _toDate; } set { _toDate = value; Invalid = false; } }
[FromServices]
public bool Invalid { get; private set;} = true;
public override string ToString()
{
var from = (fromDate == DateOnly.MinValue) ? "" : (fromDate.ToShortDateString());
var to = (toDate == DateOnly.MaxValue) ? "" : (toDate.ToShortDateString());
var hyphen = from != "" && to != "" ? "-" : "";
return $"{from}{hyphen}{to}";
}
public static Expression<Func<TType, bool>> Conditional<TType>(Expression<Func<TType, DateOnly>> dateAcessor, DateOnlyFrame frame)
{
return e => dateAcessor.Compile()(e) >= frame.fromDate && dateAcessor.Compile()(e) <= frame.toDate;
}
public DateTimeFrame ToDateTimeFrame()
{
return new DateTimeFrame(fromDate.ToDateTime(TimeOnly.MinValue), toDate.ToDateTime(TimeOnly.MaxValue));
}
}

35
Models/DateTimeFrame.cs Normal file
View File

@ -0,0 +1,35 @@
using System.Linq.Expressions;
using NejCommon.Utils;
namespace NejAccountingAPI.Models;
public class DateTimeFrame
{
public DateTimeFrame(){
}
public DateTimeFrame(DateTime fromDate, DateTime toDate)
{
FromDate = fromDate;
ToDate = toDate;
}
public DateTime FromDate { get; set; } = DateTime.MinValue;
public DateTime ToDate { get; set; } = DateTime.MaxValue;
public override string ToString()
{
var from = (FromDate == DateTime.MinValue) ? "" : ("-" + FromDate.ToShortDateString());
var to = (ToDate == DateTime.MaxValue) ? "" : ("-" + ToDate.ToShortDateString());
return $"{from}{to}";
}
public static Expression<Func<TType, bool>> Conditional<TType>(Expression<Func<TType, DateTime>> dateAcessor, DateTimeFrame frame)
{
return e => dateAcessor.Compile()(e) >= frame.FromDate && dateAcessor.Compile()(e) <= frame.ToDate;
}
public DateOnlyFrame ToDateOnlyFrame()
{
return new DateOnlyFrame(FromDate.ToDateOnly(), ToDate.ToDateOnly());
}
}