using System.ComponentModel.DataAnnotations; using Cleanuparr.Infrastructure.Models; using Quartz; namespace Cleanuparr.Infrastructure.Utilities; /// /// Utility for converting user-friendly schedule formats to Quartz cron expressions /// public static class CronExpressionConverter { /// /// Converts a JobSchedule to a Quartz cron expression /// /// The job schedule to convert /// A valid Quartz cron expression /// Thrown when the schedule has invalid values public static string ConvertToCronExpression(JobSchedule schedule) { if (schedule == null) throw new ArgumentNullException(nameof(schedule)); // Validate the schedule using predefined valid values if (!ScheduleOptions.IsValidValue(schedule.Type, schedule.Every)) { var validValues = string.Join(", ", ScheduleOptions.GetValidValues(schedule.Type)); throw new ValidationException($"Invalid value for {schedule.Type}: {schedule.Every}. Valid values are: {validValues}"); } // Cron format: Seconds Minutes Hours Day-of-month Month Day-of-week Year return schedule.Type switch { ScheduleUnit.Seconds => $"0/{schedule.Every} * * ? * * *", // Every n seconds ScheduleUnit.Minutes => $"0 0/{schedule.Every} * ? * * *", // Every n minutes ScheduleUnit.Hours => $"0 0 0/{schedule.Every} ? * * *", // Every n hours _ => throw new ArgumentException($"Invalid schedule unit: {schedule.Type}") }; } /// /// Validates a cron expression string to ensure it's valid for Quartz.NET /// /// The cron expression to validate /// True if valid, false otherwise public static bool IsValidCronExpression(string cronExpression) { if (string.IsNullOrWhiteSpace(cronExpression)) { return false; } return CronExpression.IsValidExpression(cronExpression); } }