Adding unit tests, refactoring file access to a service, adding CatalogImageMissingException.
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
|
||||
namespace ApplicationCore.Exceptions
|
||||
{
|
||||
public class CatalogImageMissingException : Exception
|
||||
{
|
||||
public CatalogImageMissingException(string message,
|
||||
Exception innerException = null)
|
||||
: base(message, innerException: innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,5 +14,4 @@ namespace ApplicationCore.Interfaces
|
||||
{
|
||||
T Parse(IPrincipal principal);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
16
src/ApplicationCore/Interfaces/IImageService.cs
Normal file
16
src/ApplicationCore/Interfaces/IImageService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using ApplicationCore.Entities;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Security.Principal;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ApplicationCore.Interfaces
|
||||
{
|
||||
|
||||
public interface IImageService
|
||||
{
|
||||
byte[] GetImageBytesById(int id);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
22
src/Infrastructure/FileSystem/LocalFileImageService.cs
Normal file
22
src/Infrastructure/FileSystem/LocalFileImageService.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using ApplicationCore.Interfaces;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using System.IO;
|
||||
|
||||
namespace Infrastructure.FileSystem
|
||||
{
|
||||
public class LocalFileImageService : IImageService
|
||||
{
|
||||
private readonly IHostingEnvironment _env;
|
||||
|
||||
public LocalFileImageService(IHostingEnvironment env)
|
||||
{
|
||||
_env = env;
|
||||
}
|
||||
public byte[] GetImageBytesById(int id)
|
||||
{
|
||||
var contentRoot = _env.ContentRootPath + "//Pics";
|
||||
var path = Path.Combine(contentRoot, id + ".png");
|
||||
return File.ReadAllBytes(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,5 +14,8 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
|
||||
<PackageReference Include="StructureMap.Microsoft.DependencyInjection" Version="1.3.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ApplicationCore\ApplicationCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -5,18 +5,24 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationCore.Interfaces;
|
||||
using ApplicationCore.Exceptions;
|
||||
|
||||
namespace Microsoft.eShopWeb.Controllers
|
||||
{
|
||||
public class CatalogController : Controller
|
||||
{
|
||||
private readonly IHostingEnvironment _env;
|
||||
private readonly ICatalogService _catalogSvc;
|
||||
private readonly ICatalogService _catalogService;
|
||||
private readonly IImageService _imageService;
|
||||
|
||||
public CatalogController(IHostingEnvironment env, ICatalogService catalogSvc)
|
||||
public CatalogController(IHostingEnvironment env,
|
||||
ICatalogService catalogService,
|
||||
IImageService imageService)
|
||||
{
|
||||
_env = env;
|
||||
_catalogSvc = catalogSvc;
|
||||
_catalogService = catalogService;
|
||||
_imageService = imageService;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,13 +30,13 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page)
|
||||
{
|
||||
var itemsPage = 10;
|
||||
var catalog = await _catalogSvc.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);
|
||||
var catalog = await _catalogService.GetCatalogItems(page ?? 0, itemsPage, BrandFilterApplied, TypesFilterApplied);
|
||||
|
||||
var vm = new CatalogIndex()
|
||||
{
|
||||
CatalogItems = catalog.Data,
|
||||
Brands = await _catalogSvc.GetBrands(),
|
||||
Types = await _catalogSvc.GetTypes(),
|
||||
Brands = await _catalogService.GetBrands(),
|
||||
Types = await _catalogService.GetTypes(),
|
||||
BrandFilterApplied = BrandFilterApplied ?? 0,
|
||||
TypesFilterApplied = TypesFilterApplied ?? 0,
|
||||
PaginationInfo = new PaginationInfo()
|
||||
@@ -53,13 +59,18 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
// GET: /<controller>/pic/{id}
|
||||
public IActionResult GetImage(int id)
|
||||
{
|
||||
var contentRoot = _env.ContentRootPath + "//Pics";
|
||||
var path = Path.Combine(contentRoot, id + ".png");
|
||||
Byte[] b = System.IO.File.ReadAllBytes(path);
|
||||
return File(b, "image/png");
|
||||
|
||||
byte[] imageBytes;
|
||||
try
|
||||
{
|
||||
imageBytes = _imageService.GetImageBytesById(id);
|
||||
}
|
||||
catch (CatalogImageMissingException ex)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return File(imageBytes, "image/png");
|
||||
}
|
||||
|
||||
|
||||
public IActionResult Error()
|
||||
{
|
||||
return View();
|
||||
|
||||
@@ -11,6 +11,8 @@ using Infrastructure.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using ApplicationCore.Interfaces;
|
||||
using Infrastructure.FileSystem;
|
||||
|
||||
namespace Microsoft.eShopWeb
|
||||
{
|
||||
@@ -65,6 +67,7 @@ namespace Microsoft.eShopWeb
|
||||
services.AddScoped<ICatalogService, CachedCatalogService>();
|
||||
services.AddScoped<CatalogService>();
|
||||
services.Configure<CatalogSettings>(Configuration);
|
||||
services.AddSingleton<IImageService, LocalFileImageService>();
|
||||
services.AddMvc();
|
||||
|
||||
_services = services;
|
||||
|
||||
Reference in New Issue
Block a user