diff --git a/Management.Test/Features/SemesterPlannerTests.cs b/Management.Test/Features/SemesterPlannerTests.cs index 7f05b54..a20c51b 100644 --- a/Management.Test/Features/SemesterPlannerTests.cs +++ b/Management.Test/Features/SemesterPlannerTests.cs @@ -49,4 +49,39 @@ public class SemesterPlannerTests semester.Months.Count().Should().Be(2); } + [Test] + public void TestSemesterGetsCorrectMonths() + { + var canvasTerm = new EnrollmentTermModel( + Id: 1, + Name: "one", + StartAt: new DateTime(2022, 1, 1), + EndAt: new DateTime(2022, 2, 1) + ); + + var semester = new SemesterPlanner(canvasTerm); + + semester.Months.First().Month.Should().Be(1); + semester.Months.Last().Month.Should().Be(2); + } + + + [Test] + public void TestMonthsCanWrapYears() + { + var canvasTerm = new EnrollmentTermModel( + Id: 1, + Name: "one", + StartAt: new DateTime(2022, 12, 1), + EndAt: new DateTime(2023, 1, 1) + ); + + var semester = new SemesterPlanner(canvasTerm); + + semester.Months.First().Month.Should().Be(12); + semester.Months.First().Year.Should().Be(2022); + + semester.Months.Last().Month.Should().Be(1); + semester.Months.Last().Year.Should().Be(2023); + } } \ No newline at end of file diff --git a/Management/Features/Calendar/CalendarMonth.cs b/Management/Features/Calendar/CalendarMonth.cs index d71c8c4..8678c58 100644 --- a/Management/Features/Calendar/CalendarMonth.cs +++ b/Management/Features/Calendar/CalendarMonth.cs @@ -1,8 +1,7 @@ public class CalendarMonth { - private DateOnly firstDay { get; } - private int year { get => firstDay.Year; } - private int month { get => firstDay.Month; } + public int Year { get; } + public int Month { get; } public IEnumerable> Weeks { get => @@ -17,10 +16,10 @@ public class CalendarMonth get { var weeks = new List>(); - var weeksInMonth = WeeksInMonth(year, month); - var daysInMonth = DateTime.DaysInMonth(year, month); + var weeksInMonth = WeeksInMonth(Year, Month); + var daysInMonth = DateTime.DaysInMonth(Year, Month); - var firstDayOfMonth = new DateTime(year, month, 1).DayOfWeek; + var firstDayOfMonth = new DateTime(Year, Month, 1).DayOfWeek; var currentDay = 1; for (int i = 0; i < weeksInMonth; i++) @@ -36,7 +35,7 @@ public class CalendarMonth } else { - thisWeek.Add(new DateTime(year, month, currentDay)); + thisWeek.Add(new DateTime(Year, Month, currentDay)); currentDay++; } } @@ -47,7 +46,7 @@ public class CalendarMonth { if (currentDay <= daysInMonth) { - thisWeek.Add(new DateTime(year, month, currentDay)); + thisWeek.Add(new DateTime(Year, Month, currentDay)); currentDay++; } else @@ -77,7 +76,7 @@ public class CalendarMonth public CalendarMonth(int year, int month) { - firstDay = new DateOnly(year, month, 1); - + Year = year; + Month = month; } } \ No newline at end of file diff --git a/Management/Features/Calendar/SemesterPlanner.cs b/Management/Features/Calendar/SemesterPlanner.cs index 2ee45eb..60a4755 100644 --- a/Management/Features/Calendar/SemesterPlanner.cs +++ b/Management/Features/Calendar/SemesterPlanner.cs @@ -5,11 +5,20 @@ public class SemesterPlanner public IEnumerable Months { get; } public SemesterPlanner(EnrollmentTermModel canvasTerm) { - var monthsInTerm = - 1 + ((canvasTerm.EndAt?.Year - canvasTerm.StartAt?.Year) * 12) - + canvasTerm.EndAt?.Month - canvasTerm.StartAt?.Month - ?? throw new Exception($"Canvas Term must have a start and end date. Term: {canvasTerm.Id}, start: {canvasTerm.StartAt}, end: {canvasTerm.EndAt}"); + var start = canvasTerm.StartAt ?? throw new Exception($"Canvas Term must have a start date. Term: {canvasTerm.Id}"); + var end = canvasTerm.EndAt ?? throw new Exception($"Canvas Term must have a end date. Term: {canvasTerm.Id}"); - Months = Enumerable.Range(0, monthsInTerm).Select(_ => new CalendarMonth(2022, 1)); + var monthsInTerm = + 1 + ((end.Year - start.Year) * 12) + + end.Month - start.Month; + + Months = Enumerable + .Range(0, monthsInTerm) + .Select(monthDiff => + { + var month = ((start.Month + monthDiff - 1) % 12) + 1; + var year = start.Year + ((start.Month + monthDiff - 1) / 12); + return new CalendarMonth(year, month); + }); } } \ No newline at end of file