70 lines
1.5 KiB
C#
70 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace MileageTraker.Web.ViewModels
|
|
{
|
|
public class DateQueryViewModel
|
|
{
|
|
public int? Year { get; set; }
|
|
public int? Month { get; set; }
|
|
public int? MonthRange { get; set; }
|
|
|
|
public string YearMonthStart
|
|
{
|
|
get { return string.Format("{0}-{1:00}", Year, Month); }
|
|
}
|
|
|
|
public string YearMonthEnd
|
|
{
|
|
get
|
|
{
|
|
var dateTime = new DateTime(Year.Value, Month.Value, 1);
|
|
var time = dateTime.AddMonths(MonthRange.Value - 1);
|
|
return string.Format("{0}-{1:00}", time.Year, time.Month);
|
|
}
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
var v = new List<string>();
|
|
|
|
if (Year.HasValue && Month.HasValue)
|
|
{
|
|
var str = YearMonthStart;
|
|
|
|
if (MonthRange.HasValue && MonthRange > 1)
|
|
str += "to" + YearMonthEnd;
|
|
|
|
v.Add(str);
|
|
}
|
|
|
|
return String.Join("_", v).Replace(' ', '-');
|
|
}
|
|
|
|
public virtual bool HasParameters()
|
|
{
|
|
return Year.HasValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use the given list of valid dates, set the default options
|
|
/// </summary>
|
|
/// <param name="validYearMonths"></param>
|
|
public virtual void SetDefaultParameters(IList<DateTime> validYearMonths)
|
|
{
|
|
if (!HasParameters())
|
|
{
|
|
Year = validYearMonths.First().Year;
|
|
Month = validYearMonths.First().Month;
|
|
MonthRange = 1;
|
|
}
|
|
if (Year.HasValue && !Month.HasValue)
|
|
{
|
|
var validLogMonths = validYearMonths.Where(dt => dt.Year == Year).ToList();
|
|
Month = validLogMonths.Min(dt => dt.Month);
|
|
MonthRange = validLogMonths.Max(dt => dt.Month) - Month + 1;
|
|
}
|
|
}
|
|
}
|
|
} |