Skip to content

Commit

Permalink
new update 09.04.2021
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafademircan71 committed Apr 9, 2021
1 parent 6d25d55 commit aff7256
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 12 deletions.
4 changes: 3 additions & 1 deletion API.Core/Interfaces/IGenericRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using API.Core.DbModels;
using API.Core.Spesicifications;
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -11,6 +12,7 @@ public interface IGenericRepository<TEntity>
{
Task<TEntity> GetByIdAsync(int id);
Task<IReadOnlyList<TEntity>> ListAllAsync();

Task<TEntity> GetEntityWithSpec(ISpesification<TEntity> spec);
Task<IReadOnlyList<TEntity>> ListAsync(ISpesification<TEntity> spec);
}
}
6 changes: 5 additions & 1 deletion API.Core/Spesicifications/BaseSpesification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ namespace API.Core.Spesicifications
{
public class BaseSpesification<TEntity> : ISpesification<TEntity>
{
public BaseSpesification()
{

}
public BaseSpesification(Expression<Func<TEntity,bool>> criteria)
{
Creteria = Creteria;
Creteria = criteria;
}
public Expression<Func<TEntity, bool>> Creteria { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using API.Core.DbModels;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace API.Core.Spesicifications
{
public class ProductsWithProductTypeAndBrandSpecification:BaseSpesification<Product>
{
public ProductsWithProductTypeAndBrandSpecification()
{
AddInlcude(x => x.ProductBrand);
AddInlcude(x => x.ProductType);
}
public ProductsWithProductTypeAndBrandSpecification(int id)
:base(x=>x.Id==id)
{
AddInlcude(x => x.ProductBrand);
AddInlcude(x => x.ProductType);
}
}
}
24 changes: 24 additions & 0 deletions API.Infrastructure/Data/SpesificationEveluator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using API.Core.DbModels;
using API.Core.Spesicifications;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace API.Infrastructure.Data
{
public class SpesificationEveluator<TEntiy>
where TEntiy:BaseEntity
{
public static IQueryable<TEntiy> GetQuery(IQueryable<TEntiy> inputQuery,ISpesification<TEntiy> spec)
{
var query = inputQuery;

if (spec.Creteria !=null)
{
query = query.Where(spec.Creteria);
}

query = spec.Includes.Aggregate(query, (current, include) => current.Include(include));
return query;
}
}
}
19 changes: 18 additions & 1 deletion API.Infrastructure/İmplements/GenericRepository.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using API.Core.DbModels;
using API.Core.Interfaces;
using API.Core.Spesicifications;
using API.Infrastructure.Data;
using API.Infrastructure.DataContext;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -21,9 +24,23 @@ public async Task<TEntity> GetByIdAsync(int id)
return await _ctx.Set<TEntity>().FindAsync(id);
}

public async Task<IReadOnlyList<TEntity>> ListAllAsync()
public async Task<IReadOnlyList<TEntity>> ListAllAsync()
{
return await _ctx.Set<TEntity>().ToListAsync();
}
public async Task<TEntity> GetEntityWithSpec(ISpesification<TEntity> spec)
{
return await ApplySpesification(spec).FirstOrDefaultAsync();
}

public async Task<IReadOnlyList<TEntity>> ListAsync(ISpesification<TEntity> spec)
{
return await ApplySpesification(spec).ToListAsync();
}

private IQueryable<TEntity> ApplySpesification(ISpesification<TEntity> spec)
{
return SpesificationEveluator<TEntity>.GetQuery(_ctx.Set<TEntity>().AsQueryable(), spec);
}
}
}
2 changes: 2 additions & 0 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>

Expand Down
28 changes: 21 additions & 7 deletions API/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using API.Core.Interfaces;
using API.Core.Spesicifications;
using API.Dtos;
using System.Linq;
using AutoMapper;

namespace API.Controllers
{
Expand All @@ -15,25 +19,34 @@ public class ProductsController : ControllerBase
private readonly IGenericRepository<Product> _productRepo;
private readonly IGenericRepository<ProductBrand> _brandRepo;
private readonly IGenericRepository<ProductType> _typeRepo;
private readonly IMapper _mapper;

public ProductsController(IGenericRepository<Product> productRepo,IGenericRepository<ProductBrand> brandRepo,IGenericRepository<ProductType> typeRepo)
public ProductsController(IGenericRepository<Product> productRepo,
IGenericRepository<ProductBrand> brandRepo,IGenericRepository<ProductType> typeRepo,IMapper mapper)
{
_productRepo = productRepo;
_brandRepo = brandRepo;
_typeRepo = typeRepo;
_mapper = mapper;
}

[HttpGet]
public async Task<ActionResult<List<Product>>> GetProducts()
public async Task<ActionResult<List<ProductToReturnDto>>> GetProducts()
{
var data = await _productRepo.ListAllAsync();
return Ok(data);

var spec = new ProductsWithProductTypeAndBrandSpecification();
var data = await _productRepo.ListAsync(spec);
// return Ok(data);
return Ok(_mapper.Map<IReadOnlyList<Product>, IReadOnlyList<ProductToReturnDto>>(data));
}
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetProduct(int id)
public async Task<ActionResult<ProductToReturnDto>> GetProduct(int id)
{
return await _productRepo.GetByIdAsync(id);
var spec = new ProductsWithProductTypeAndBrandSpecification(id);

//return await _productRepo.GetEntityWithSpec(spec);

var product = await _productRepo.GetEntityWithSpec(spec);
return _mapper.Map<Product, ProductToReturnDto>(product);
}
[HttpGet("brands")]
public async Task<ActionResult<IReadOnlyList<ProductBrand>>> GetProductBrands()
Expand All @@ -45,5 +58,6 @@ public async Task<ActionResult<IReadOnlyList<ProductBrand>>> GetProductTypes()
{
return Ok(await _typeRepo.ListAllAsync());
}

}
}
14 changes: 14 additions & 0 deletions API/Dtos/ProductToReturnDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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; }

}
}
17 changes: 17 additions & 0 deletions API/Helpers/MappingProfiles.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using API.Core.DbModels;
using API.Dtos;
using AutoMapper;

namespace API.Helpers
{
public class MappingProfiles : Profile
{
public MappingProfiles()
{
CreateMap<Product, ProductToReturnDto>()
.ForMember(x => x.ProductBrand, o => o.MapFrom(s => s.ProductBrand.Name))
.ForMember(x => x.ProductType, o => o.MapFrom(s => s.ProductType.Name))
.ForMember(x=>x.PictureUrl,o=>o.MapFrom<ProductUrlResolver>());
}
}
}
29 changes: 29 additions & 0 deletions API/Helpers/ProductUrlResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using API.Core.DbModels;
using API.Dtos;
using AutoMapper;
using AutoMapper.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace API.Helpers
{
public class ProductUrlResolver : IValueResolver<Product, ProductToReturnDto, string>
{
private readonly Microsoft.Extensions.Configuration.IConfiguration _config;

public ProductUrlResolver(Microsoft.Extensions.Configuration.IConfiguration config)
{
_config = config;
}
public string Resolve(Product source, ProductToReturnDto destination, string destMember, ResolutionContext context)
{
if (!string.IsNullOrEmpty(source.PictureUrl))
{
return _config["ApiUrl"] + source.PictureUrl;
}
return null;
}
}
}
4 changes: 3 additions & 1 deletion API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using API.Core.Interfaces;
using API.Helpers;
using API.Infrastructure.DataContext;
using API.Infrastructure.Ýmplements;
using Microsoft.AspNetCore.Builder;
Expand All @@ -25,7 +26,8 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IProductRepository, ProductRepository>();
services.AddScoped(typeof(IGenericRepository<>),(typeof(GenericRepository<>)));
services.AddScoped(typeof(IGenericRepository<>),(typeof(GenericRepository<>)));
services.AddAutoMapper(typeof(MappingProfiles));
services.AddControllers();
services.AddDbContext<StoreContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//services.AddSwaggerGen(c =>
Expand Down
3 changes: 2 additions & 1 deletion API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server = DESKTOP-NKKGRR6;DataBase = StoreDataBase;Trusted_Connection=True;MultipleActiveResultSets=True"
}
},
"ApiUrl": "https://localhost:44322/"
}

0 comments on commit aff7256

Please sign in to comment.