NejCommon.NET/Models/DateOnlyFrame.cs
2024-09-15 19:47:20 +02:00

43 lines
1.4 KiB
C#

using System.Linq.Expressions;
using Microsoft.AspNetCore.Mvc;
namespace NejCommon.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));
}
}