diff --git a/Models/Api/Error.cs b/Models/Api/Error.cs new file mode 100644 index 0000000..6712670 --- /dev/null +++ b/Models/Api/Error.cs @@ -0,0 +1,34 @@ +using System.Text.Json.Serialization; + +using NejCommon.Models; + +namespace NejCommon.Models +{ + /// + /// There was an error + /// + 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; } + + /// + /// Gets or Sets _Error + /// + [JsonPropertyName("error")] + public object? _Error { get; set; } + + } +} diff --git a/Models/ModelBinder.cs b/Models/Api/ModelBinder.cs similarity index 100% rename from Models/ModelBinder.cs rename to Models/Api/ModelBinder.cs diff --git a/Models/Api/Pagination.cs b/Models/Api/Pagination.cs new file mode 100644 index 0000000..c4cf5f6 --- /dev/null +++ b/Models/Api/Pagination.cs @@ -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{ + public int TotalCount { get; set; } + public int Offset { get; set; } + public int Count { get; set; } + public IAsyncEnumerable Data { get; set; } = default!; +} \ No newline at end of file diff --git a/Models/DateOnlyFrame.cs b/Models/DateOnlyFrame.cs new file mode 100644 index 0000000..bd63724 --- /dev/null +++ b/Models/DateOnlyFrame.cs @@ -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> 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)); + } +} \ No newline at end of file diff --git a/Models/DateTimeFrame.cs b/Models/DateTimeFrame.cs new file mode 100644 index 0000000..c1d61ce --- /dev/null +++ b/Models/DateTimeFrame.cs @@ -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> Conditional(Expression> 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()); + } +} \ No newline at end of file