diff --git a/API/API.csproj b/API/API.csproj index 5bc8cfe..dda2eff 100644 --- a/API/API.csproj +++ b/API/API.csproj @@ -6,6 +6,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/API/Controllers/ProductsController.cs b/API/Controllers/ProductsController.cs index 374c947..fcd46f9 100644 --- a/API/Controllers/ProductsController.cs +++ b/API/Controllers/ProductsController.cs @@ -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 _productsRepo; private readonly IGenericRepository _productBrandRepo; private readonly IGenericRepository _productTypeRepo; - public ProductsController(IGenericRepository productsRepo, IGenericRepository productBrandRepo, IGenericRepository productTypeRepo) + private readonly IMapper _mapper; + public ProductsController(IGenericRepository productsRepo, IGenericRepository productBrandRepo, IGenericRepository productTypeRepo, IMapper mapper) { + _mapper = mapper; _productTypeRepo = productTypeRepo; _productBrandRepo = productBrandRepo; _productsRepo = productsRepo; @@ -21,18 +25,19 @@ namespace API.Controllers } [HttpGet] - public async Task>> GetProducts() + public async Task>> GetProducts() { var spec = new ProductsWithTypesAndBrandsSpecification(); var products = await _productsRepo.ListAsync(spec); - return Ok(products); + return Ok(_mapper.Map, IReadOnlyList>(products)); } [HttpGet("{id}")] - public async Task> GetProduct(int id) + public async Task> GetProduct(int id) { var spec = new ProductsWithTypesAndBrandsSpecification(id); - return await _productsRepo.GetEntityWithSpec(spec); + var product = await _productsRepo.GetEntityWithSpec(spec); + return _mapper.Map(product); } [HttpGet("brands")] diff --git a/API/Dtos/ProductToReturnDto.cs b/API/Dtos/ProductToReturnDto.cs new file mode 100644 index 0000000..7a204d8 --- /dev/null +++ b/API/Dtos/ProductToReturnDto.cs @@ -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; } + } +} \ No newline at end of file diff --git a/API/Helpers/MappingProfiles.cs b/API/Helpers/MappingProfiles.cs new file mode 100644 index 0000000..1edaa9d --- /dev/null +++ b/API/Helpers/MappingProfiles.cs @@ -0,0 +1,16 @@ +using API.Dtos; +using AutoMapper; +using Core.Entities; + +namespace API.Helpers +{ + public class MappingProfiles : Profile + { + public MappingProfiles() + { + CreateMap() + .ForMember(d => d.ProductBrand, o => o.MapFrom(s => s.ProductBrand.Name)) + .ForMember(d => d.ProductType, o => o.MapFrom(s => s.ProductType.Name)); + } + } +} \ No newline at end of file diff --git a/API/Startup.cs b/API/Startup.cs index aa263cb..8d9d6fc 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -1,3 +1,4 @@ +using API.Helpers; using Core.Interfaces; using Infrastructure.Data; using Microsoft.EntityFrameworkCore; @@ -22,6 +23,7 @@ namespace API services.AddDbContext(x => x.UseSqlite(_config.GetConnectionString("DefaultConnection"))); services.AddScoped(); services.AddScoped(typeof(IGenericRepository<>), (typeof(GenericRepository<>))); + services.AddAutoMapper(typeof(MappingProfiles)); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPIv5", Version = "v1" });