From f9750ef039bcb17c266878e478e221202eedf320 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Wed, 29 May 2024 03:46:16 +0300 Subject: [PATCH] feat: add endpoint for schedule --- .../ScheduleConfigurationRequest.cs | 29 +++++++++++++++++++ .../Configuration/SetupController.cs | 28 ++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 ApiDto/Requests/Configuration/ScheduleConfigurationRequest.cs diff --git a/ApiDto/Requests/Configuration/ScheduleConfigurationRequest.cs b/ApiDto/Requests/Configuration/ScheduleConfigurationRequest.cs new file mode 100644 index 0000000..ca6e950 --- /dev/null +++ b/ApiDto/Requests/Configuration/ScheduleConfigurationRequest.cs @@ -0,0 +1,29 @@ +using Mirea.Api.Dto.Common; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; + +namespace Mirea.Api.Dto.Requests.Configuration; + +/// +/// Represents a request to configure the schedule settings. +/// +public class ScheduleConfigurationRequest +{ + /// + /// Gets or sets the cron expression for updating the schedule. + /// + public string? CronUpdateSchedule { get; set; } + + /// + /// Gets or sets the start date of the term. + /// + [Required] + public DateOnly StartTerm { get; set; } + + /// + /// Gets or sets the pair period times, keyed by pair number. + /// + [Required] + public required IDictionary PairPeriod { get; set; } +} \ No newline at end of file diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index d5eb17e..7abd5ee 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -1,5 +1,4 @@ -using System; -using System.Security.Cryptography; +using Cronos; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Data.Sqlite; @@ -17,6 +16,10 @@ using Npgsql; using StackExchange.Redis; using System; using System.Data; +using System.IO; +using System.Runtime.InteropServices; +using System.Security.Cryptography; +using System.Text.Json; namespace Mirea.Api.Endpoint.Controllers.Configuration; @@ -240,6 +243,27 @@ public class SetupController(ISetupToken setupToken, IMaintenanceModeNotConfigur return true; } + + [HttpPost("SetSchedule")] + [TokenAuthentication] + [BadRequestResponse] + public ActionResult SetSchedule([FromBody] ScheduleConfigurationRequest request) + { + var general = GeneralConfig; + general.ScheduleSettings = new ScheduleSettings + { + // every 6 hours + CronUpdateSchedule = request.CronUpdateSchedule ?? "0 */6 * * *", + StartTerm = request.StartTerm, + PairPeriod = request.PairPeriod.ConvertFromDto() + }; + + if (!CronExpression.TryParse(general.ScheduleSettings.CronUpdateSchedule, CronFormat.Standard, out _)) + throw new ControllerArgumentException("The Cron task could not be parsed. Check the format of the entered data."); + + GeneralConfig = general; + + return true; }