This solution should help me explain my desire

This commit is contained in:
Tracy Pearson
2020-10-19 12:55:29 -04:00
parent 4e9964435b
commit 92445d4e7a
18 changed files with 280 additions and 71 deletions

View File

@@ -4,5 +4,10 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\RepositoryLibrary\RepositoryLibrary.csproj" />
<ProjectReference Include="..\SharedLibrary\SharedLibrary.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RepositoryLibrary;
namespace ApiService.Controllers
{
[ApiController]
[Route("/api")]
public class RepositoryController : ControllerBase
{
private readonly IRepository _repsitory;
public RepositoryController(IRepository repository)
{
_repsitory = repository;
}
[HttpGet("lookup")]
public async Task<object> Lookup()
{
return await _repsitory.Lookup();
}
}
}

View File

@@ -1,39 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace ApiService.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}

View File

@@ -1,5 +1,4 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
@@ -8,23 +7,21 @@
"sslPort": 0
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ApiService": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "http://localhost:5000",
"launchUrl": "api/lookup",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"applicationUrl": "http://localhost:56143"
}
}
}
}

View File

@@ -1,14 +1,21 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using RepositoryLibrary;
using SharedLibrary.Models;
namespace ApiService
{
@@ -24,6 +31,7 @@ namespace ApiService
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IRepository, LocalRepository>();
services.AddControllers();
}
@@ -37,6 +45,30 @@ namespace ApiService
app.UseRouting();
app.Use(async (context, next) =>
{
try
{
await next();
}
catch (Exception ex)
{
var error = new RepositoryError
{
Information = ex.Message,
StackTrack = ex.StackTrace
};
var dc = new DataContractJsonSerializer(typeof(RepositoryError));
using (var ms = new MemoryStream())
{
dc.WriteObject(ms, error);
var response = new StringBuilder(Encoding.UTF8.GetString(ms.ToArray()));
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(response.ToString());
}
}
});
app.UseAuthorization();
app.UseEndpoints(endpoints =>

View File

@@ -1,15 +0,0 @@
using System;
namespace ApiService
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}
}