Finished Day 1 and set up Swagger to handle log messages

master
Alexander Sigler 3 years ago
parent 6de240c87a
commit 537305704b

@ -20,9 +20,9 @@ namespace AOC2021.Controllers
[HttpPost] [HttpPost]
[Consumes("text/plain")] [Consumes("text/plain")]
[Route("day1")] [Route("day1")]
public AOCResponse Day(AOCVersion version, [FromBody] string input) public AOCResponse Day(AOCVersion version, [FromBody] string input, bool IgnoreLogMessages = false)
{ {
AOCRequest request = new AOCRequest() { Input = input, Version = version }; AOCRequest request = new AOCRequest() { Input = input, Version = version, IgnoreLogMessages = IgnoreLogMessages };
var splitRoute = this.ControllerContext.HttpContext.Request.Path.ToString().Split("/"); var splitRoute = this.ControllerContext.HttpContext.Request.Path.ToString().Split("/");
string requestedRoute = splitRoute[splitRoute.Length - 1]; //Get the last endpoint value. string requestedRoute = splitRoute[splitRoute.Length - 1]; //Get the last endpoint value.
return GetAOCDay(requestedRoute).ExecuteDay(request); return GetAOCDay(requestedRoute).ExecuteDay(request);
@ -30,17 +30,17 @@ namespace AOC2021.Controllers
private AOCDay GetAOCDay(string route) private AOCDay GetAOCDay(string route)
{ {
IAOCService day = null; AOCDay day = null;
var type = typeof(AOCDay); var type = typeof(AOCDay);
var types = AppDomain.CurrentDomain.GetAssemblies() var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes()) .SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract); .Where(p => type.IsAssignableFrom(p) && !p.IsInterface && !p.IsAbstract);
foreach (var x in types) foreach (var x in types)
{ {
Console.WriteLine(x.Name.ToLower());
if (x.Name.ToLower() == route.ToLower()) if (x.Name.ToLower() == route.ToLower())
{ {
day = (IAOCService)Activator.CreateInstance(x); day = (AOCDay) (IAOCService)Activator.CreateInstance(x);
day.SetLogger(this._logger);
} }

@ -1,4 +1,5 @@
using AOC2021.Models; using AOC2021.Models;
using AOC2021.Helper;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -8,16 +9,46 @@ namespace AOC2021.Days
{ {
public class Day1 : AOCDay public class Day1 : AOCDay
{ {
protected override AOCResponse ExecutePartA() protected override AOCResponse ExecutePartA()
{ {
Console.WriteLine(this._request.Input); var input = this._request.Input.Split("\n");
return null; int increase = 0;
for (int i = 0; i < input.Length-1; i++)
{
var curret = input[i].ToInt();
var next = input[i + 1].ToInt();
if (next > curret)
{
increase++;
Log("Increase");
}
}
this._response.Answer = increase.ToString();
return this._response;
} }
protected override AOCResponse ExecutePartB() protected override AOCResponse ExecutePartB()
{ {
throw new NotImplementedException(); var input = this._request.Input.Split("\n");
var list = new List<int>();
for (int i = 0; i < input.Length-2; i++)
{
var a = input[i].ToInt();
var b = input[i + 1].ToInt();
var c = input[i + 2].ToInt();
list.Add(a + b + c);
}
list.ForEach(x => Log(x.ToString()));
int increase = 0;
for (int i = 0; i < list.Count-1; i++)
{
if (list[i + 1] > list[i])
increase++;
}
this._response.Answer = increase.ToString();
return this._response;
} }
} }
} }

@ -0,0 +1,12 @@
using System;
namespace AOC2021.Helper
{
public static class AOCExtensions
{
public static int ToInt(this string str)
{
return Convert.ToInt32(str);
}
}
}

@ -1,26 +1,64 @@
 
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
namespace AOC2021.Models namespace AOC2021.Models
{ {
public abstract class AOCDay : IAOCService public abstract class AOCDay : IAOCService
{ {
protected AOCRequest _request; protected AOCRequest _request;
protected ILogger<AOCDay> _logger; protected AOCResponse _response;
protected ILogger _logger;
private List<string> _debugMessages;
public AOCDay()
{
this._response = new AOCResponse()
{
Status = false
};
this._debugMessages = new List<string>();
}
public AOCResponse ExecuteDay(AOCRequest request) public AOCResponse ExecuteDay(AOCRequest request)
{ {
_request = request; _request = request;
switch (request.Version) try
{
switch (request.Version)
{
case AOCVersion.A:
this._response = ExecutePartA();
break;
case AOCVersion.B:
this._response = ExecutePartB();
break;
}
this._response.Status = true;
}
catch (Exception e)
{ {
case AOCVersion.A: this._response.Answer = e.Message;
return ExecutePartA(); this._response.Status = false;
case AOCVersion.B: this._response.StackTrace = e.StackTrace;
return ExecutePartB();
} }
return new AOCResponse() { Answer = "AOCDay failed to get a response", Status = false }; this._response.Debug = this._debugMessages;
return this._response;
} }
protected abstract AOCResponse ExecutePartA(); protected abstract AOCResponse ExecutePartA();
protected abstract AOCResponse ExecutePartB(); protected abstract AOCResponse ExecutePartB();
public void SetLogger(ILogger logger)
{
this._logger = logger;
}
protected void Log(string message, params object[] args)
{
if (!this._request.IgnoreLogMessages)
_logger.LogInformation(message, args);
_debugMessages.Add(string.Format(message, args));
}
} }
} }

@ -11,5 +11,7 @@ namespace AOC2021.Models
public AOCVersion Version { get; set; } public AOCVersion Version { get; set; }
[DataMember] [DataMember]
public string Input { get; set; } public string Input { get; set; }
[DataMember]
public bool IgnoreLogMessages { get; set; }
} }
} }

@ -9,11 +9,15 @@ namespace AOC2021.Models
[Serializable] [Serializable]
public class AOCResponse public class AOCResponse
{ {
[DataMember]
public DateTime Date { get { return DateTime.Now; } }
[DataMember]
public string Answer { get; set; }
[DataMember] [DataMember]
public bool Status { get; set; } public bool Status { get; set; }
[DataMember] [DataMember]
public IEnumerable<string> Debug { get; set; } public IEnumerable<string> Debug { get; set; }
[DataMember] [DataMember]
public string Answer { get; set; } public string StackTrace { get; set; }
} }
} }

@ -21,6 +21,11 @@ namespace AOC2021
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();
}).ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddDebug();
}); });
} }
} }

Loading…
Cancel
Save