mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-14 01:25:38 -05:00
Replace calendar service with new .NET version
This commit is contained in:
27
Calendar/Service/Controllers/CalendarController.cs
Normal file
27
Calendar/Service/Controllers/CalendarController.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using ChrisKaczor.HomeMonitor.Calendar.Service.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Controllers;
|
||||
|
||||
[Route("calendar")]
|
||||
[ApiController]
|
||||
public class CalendarController(IConfiguration configuration, HttpClient httpClient) : ControllerBase
|
||||
{
|
||||
[HttpGet("upcoming")]
|
||||
public async Task<ActionResult<IEnumerable<CalendarEntry>>> GetUpcoming([FromQuery] int? days)
|
||||
{
|
||||
var data = await httpClient.GetStringAsync(configuration["Calendar:PersonalUrl"]);
|
||||
|
||||
var calendar = Ical.Net.Calendar.Load(data);
|
||||
|
||||
var start = DateTimeOffset.Now.Date;
|
||||
var end = start.AddDays(days ?? 1);
|
||||
|
||||
var calendarEntries = calendar
|
||||
.GetOccurrences(start, end)
|
||||
.Select(o => new CalendarEntry(o))
|
||||
.OrderBy(ce => ce.Start);
|
||||
|
||||
return Ok(calendarEntries);
|
||||
}
|
||||
}
|
||||
35
Calendar/Service/Controllers/HolidayController.cs
Normal file
35
Calendar/Service/Controllers/HolidayController.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using ChrisKaczor.HomeMonitor.Calendar.Service.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ChrisKaczor.HomeMonitor.Calendar.Service.Controllers;
|
||||
|
||||
[Route("events")]
|
||||
[ApiController]
|
||||
public class HolidayController(IConfiguration configuration, HttpClient httpClient) : ControllerBase
|
||||
{
|
||||
[HttpGet("next")]
|
||||
public async Task<ActionResult<IEnumerable<CalendarEntry>>> GetNext([FromQuery] string timezone = "Etc/UTC")
|
||||
{
|
||||
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timezone);
|
||||
|
||||
var data = await httpClient.GetStringAsync(configuration["Calendar:HolidayUrl"]);
|
||||
|
||||
var calendar = Ical.Net.Calendar.Load(data);
|
||||
|
||||
var start = DateTimeOffset.Now.Date;
|
||||
var end = start.AddYears(1);
|
||||
|
||||
var calendarEntries = calendar
|
||||
.GetOccurrences(start, end)
|
||||
.Select(o => new CalendarEntry(o))
|
||||
.OrderBy(ce => ce.Start);
|
||||
|
||||
var nextCalendarEntry = calendarEntries.First();
|
||||
|
||||
var holidayEntry = new HolidayEntry(nextCalendarEntry, timeZoneInfo);
|
||||
|
||||
var holidayResponse = new HolidayResponse(holidayEntry, timeZoneInfo);
|
||||
|
||||
return Ok(holidayResponse);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user