All checks were successful
continuous-integration/drone/push Build is passing
83 lines
3.1 KiB
C#
83 lines
3.1 KiB
C#
using AdventOfCode.Common;
|
|
using AdventOfCode.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace AdventOfCode.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("[controller]")]
|
|
public class AdventOfCodeController : ControllerBase
|
|
{
|
|
private readonly ILogger<AdventOfCodeController> _logger;
|
|
|
|
public AdventOfCodeController(ILogger<AdventOfCodeController> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Consumes("text/plain")]
|
|
public AOCResponse Day(int year, int day, AOCVersion version, [FromBody] string input, bool IgnoreLogMessages = false)
|
|
{
|
|
_logger.LogInformation($"Recieving a request for {year} day {day} version {version} with {(IgnoreLogMessages ? "no" : "")}logs");
|
|
AOCRequest request = new AOCRequest() { Input = input, Version = version, IgnoreLogMessages = IgnoreLogMessages };
|
|
var aocDay = GetAOCDay(year, day);
|
|
if (aocDay == null)
|
|
{
|
|
return new AOCResponse() { Status = false, StackTrace = $"Year {2023} Day {day} part {version} is not available at this time!" };
|
|
}
|
|
var resp = aocDay.ExecuteDay(request);
|
|
return resp;
|
|
}
|
|
|
|
private AOCDay GetAOCDay(int year, int day)
|
|
{
|
|
AOCDay aocDay = null;
|
|
var type = typeof(AOCDay);
|
|
var types = AppDomain.CurrentDomain.GetAssemblies()
|
|
.SelectMany(s => s.GetTypes())
|
|
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract);
|
|
foreach (var x in types)
|
|
{
|
|
var aocAttribute = (AOCAttribute) Attribute.GetCustomAttribute(x, typeof(AOCAttribute));
|
|
if (aocAttribute != null)
|
|
{
|
|
if (aocAttribute.Year == year && aocAttribute.Day == day)
|
|
{
|
|
aocDay = (AOCDay)(IAOCService)Activator.CreateInstance(x);
|
|
aocDay.SetLogger(this._logger);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var ns = x.Namespace;
|
|
var className = x.Name;
|
|
var dayParsed = 0;
|
|
_ = int.TryParse(Regex.Replace(className, "[^0-9.]", ""), out dayParsed);
|
|
|
|
var match = Regex.Match(ns, @"\d{4}");
|
|
if (match.Success)
|
|
{
|
|
var yearParsed = 0;
|
|
_ = int.TryParse(match.Value, out yearParsed);
|
|
if (yearParsed == year && dayParsed == day)
|
|
{
|
|
aocDay = (AOCDay)(IAOCService)Activator.CreateInstance(x);
|
|
aocDay.SetLogger(this._logger);
|
|
break;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if (aocDay != null) break; //Means we found a match and it was created!
|
|
}
|
|
return aocDay;
|
|
}
|
|
}
|
|
}
|