Incorporated AutoMapper
This commit is contained in:
parent
8bca3f1f9d
commit
ea9c8dbb26
@ -6,6 +6,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="11.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
@ -2,6 +2,8 @@ using Core.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Core.Interfaces;
|
||||
using Core.Specifications;
|
||||
using API.Dtos;
|
||||
using AutoMapper;
|
||||
|
||||
namespace API.Controllers
|
||||
{
|
||||
@ -12,8 +14,10 @@ namespace API.Controllers
|
||||
private readonly IGenericRepository<Product> _productsRepo;
|
||||
private readonly IGenericRepository<ProductBrand> _productBrandRepo;
|
||||
private readonly IGenericRepository<ProductType> _productTypeRepo;
|
||||
public ProductsController(IGenericRepository<Product> productsRepo, IGenericRepository<ProductBrand> productBrandRepo, IGenericRepository<ProductType> productTypeRepo)
|
||||
private readonly IMapper _mapper;
|
||||
public ProductsController(IGenericRepository<Product> productsRepo, IGenericRepository<ProductBrand> productBrandRepo, IGenericRepository<ProductType> productTypeRepo, IMapper mapper)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_productTypeRepo = productTypeRepo;
|
||||
_productBrandRepo = productBrandRepo;
|
||||
_productsRepo = productsRepo;
|
||||
@ -21,18 +25,19 @@ namespace API.Controllers
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<List<Product>>> GetProducts()
|
||||
public async Task<ActionResult<IReadOnlyList<ProductToReturnDto>>> GetProducts()
|
||||
{
|
||||
var spec = new ProductsWithTypesAndBrandsSpecification();
|
||||
var products = await _productsRepo.ListAsync(spec);
|
||||
return Ok(products);
|
||||
return Ok(_mapper.Map<IReadOnlyList<Product>, IReadOnlyList<ProductToReturnDto>>(products));
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<ActionResult<Product>> GetProduct(int id)
|
||||
public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id)
|
||||
{
|
||||
var spec = new ProductsWithTypesAndBrandsSpecification(id);
|
||||
return await _productsRepo.GetEntityWithSpec(spec);
|
||||
var product = await _productsRepo.GetEntityWithSpec(spec);
|
||||
return _mapper.Map<Product, ProductToReturnDto>(product);
|
||||
}
|
||||
|
||||
[HttpGet("brands")]
|
||||
|
18
API/Dtos/ProductToReturnDto.cs
Normal file
18
API/Dtos/ProductToReturnDto.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace API.Dtos
|
||||
{
|
||||
public class ProductToReturnDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public decimal Price { get; set; }
|
||||
public string PictureUrl { get; set; }
|
||||
public string ProductType { get; set; }
|
||||
public string ProductBrand { get; set; }
|
||||
}
|
||||
}
|
16
API/Helpers/MappingProfiles.cs
Normal file
16
API/Helpers/MappingProfiles.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using API.Dtos;
|
||||
using AutoMapper;
|
||||
using Core.Entities;
|
||||
|
||||
namespace API.Helpers
|
||||
{
|
||||
public class MappingProfiles : Profile
|
||||
{
|
||||
public MappingProfiles()
|
||||
{
|
||||
CreateMap<Product, ProductToReturnDto>()
|
||||
.ForMember(d => d.ProductBrand, o => o.MapFrom(s => s.ProductBrand.Name))
|
||||
.ForMember(d => d.ProductType, o => o.MapFrom(s => s.ProductType.Name));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using API.Helpers;
|
||||
using Core.Interfaces;
|
||||
using Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@ -22,6 +23,7 @@ namespace API
|
||||
services.AddDbContext<StoreContext>(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection")));
|
||||
services.AddScoped<iProductRepository, ProductRepository>();
|
||||
services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>)));
|
||||
services.AddAutoMapper(typeof(MappingProfiles));
|
||||
services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPIv5", Version = "v1" });
|
||||
|
Loading…
Reference in New Issue
Block a user