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.ToString("yyyy-MM-dd")); var to = (toDate == DateOnly.MaxValue) ? "" : (toDate.ToString("yyyy-MM-dd")); var hyphen = from != "" && to != "" ? "-" : ""; return $"{from}{hyphen}{to}"; } public static Expression> Conditional(Expression> 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)); } }