Skip to content

Commit

Permalink
Merge pull request #12 from AEMLoviji/feat/support-CRUD-for-products-…
Browse files Browse the repository at this point in the history
…entity

Feat/support crud for products entity
  • Loading branch information
aemloviji authored Oct 26, 2019
2 parents 7b5577f + 3d788ae commit 45c830e
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 4 deletions.
33 changes: 33 additions & 0 deletions Minimarket.API/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Minimarket.API.Domain.Models;
using Minimarket.API.ViewModels;
using Minimarket.API.Domain.Services;
using Minimarket.API.Extensions;
using AutoMapper;

namespace Minimarket.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProductsController : Controller
{
private readonly IProductService _productService;
private readonly IMapper _mapper;

public ProductsController(IProductService productService, IMapper mapper)
{
_productService = productService;
_mapper = mapper;
}

[HttpGet]
public async Task<IEnumerable<ProductViewModel>> ListAsync()
{
var products = await _productService.ListAsync();
var resources = _mapper.Map<IEnumerable<Product>, IEnumerable<ProductViewModel>>(products);
return resources;
}
}
}
20 changes: 20 additions & 0 deletions Minimarket.API/Domain/Db/Contexts/AppDbContext .cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.Entity<Product>().Property(p => p.Name).IsRequired().HasMaxLength(50);
builder.Entity<Product>().Property(p => p.QuantityInPackage).IsRequired();
builder.Entity<Product>().Property(p => p.UnitOfMeasurement).IsRequired();

builder.Entity<Product>().HasData
(
new Product
{
Id = 100,
Name = "Apple",
QuantityInPackage = 1,
UnitOfMeasurement = UnitOfMeasurement.Unity,
CategoryId = 1000
},
new Product
{
Id = 101,
Name = "Kinder surprise",
QuantityInPackage = 1,
UnitOfMeasurement = UnitOfMeasurement.Unity,
CategoryId = 1002,
}
);
}
}
}
11 changes: 11 additions & 0 deletions Minimarket.API/Domain/Repositories/IProductRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Minimarket.API.Domain.Models;

namespace Minimarket.API.Domain.Repositories
{
public interface IProductRepository
{
Task<IEnumerable<Product>> ListAsync();
}
}
18 changes: 18 additions & 0 deletions Minimarket.API/Domain/Repositories/ProductRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Minimarket.API.Domain.Db.Contexts;
using Minimarket.API.Domain.Models;

namespace Minimarket.API.Domain.Repositories
{
public class ProductRepository : Repository, IProductRepository
{
public ProductRepository(AppDbContext context) : base(context) { }

public async Task<IEnumerable<Product>> ListAsync() =>
await _context.Products.Include(p => p.Category)
.ToListAsync();
}
}
2 changes: 1 addition & 1 deletion Minimarket.API/Domain/Services/CategoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CategoryService : ICategoryService

public CategoryService(ICategoryRepository categoryRepository, IUnitOfWork unitOfWork)
{
this._categoryRepository = categoryRepository;
_categoryRepository = categoryRepository;
_unitOfWork = unitOfWork;
}

Expand Down
12 changes: 12 additions & 0 deletions Minimarket.API/Domain/Services/IProductService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Minimarket.API.Domain.Models;
using Minimarket.API.Domain.Services.Response;

namespace Minimarket.API.Domain.Services
{
public interface IProductService
{
Task<IEnumerable<Product>> ListAsync();
}
}
26 changes: 26 additions & 0 deletions Minimarket.API/Domain/Services/ProductService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Minimarket.API.Domain.Models;
using Minimarket.API.Domain.Repositories;
using Minimarket.API.Domain.Services.Response;

namespace Minimarket.API.Domain.Services
{
public class ProductService : IProductService
{
private readonly IProductRepository _productRepository;
private readonly IUnitOfWork _unitOfWork;

public ProductService(IProductRepository productRepository, IUnitOfWork unitOfWork)
{
_productRepository = productRepository;
_unitOfWork = unitOfWork;
}

public async Task<IEnumerable<Product>> ListAsync()
{
return await _productRepository.ListAsync();
}
}
}
16 changes: 16 additions & 0 deletions Minimarket.API/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Reflection;
using System.ComponentModel;

namespace Minimarket.API.Extensions
{
public static class EnumExtensions
{
public static string ToDescriptionString<TEnum>(this TEnum @enum)
{
FieldInfo info = @enum.GetType().GetField(@enum.ToString());
var attributes = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);

return attributes?[0].Description ?? @enum.ToString();
}
}
}
4 changes: 4 additions & 0 deletions Minimarket.API/Mappers/ModelToViewModelProfile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;
using Minimarket.API.Domain.Models;
using Minimarket.API.ViewModels;
using Minimarket.API.Extensions;

namespace Minimarket.API.Mappers
{
Expand All @@ -9,6 +10,9 @@ public class ModelToViewModelProfile : Profile
public ModelToViewModelProfile()
{
CreateMap<Category, CategoryViewModel>();
CreateMap<Product, ProductViewModel>()
.ForMember(src => src.UnitOfMeasurement,
opt => opt.MapFrom(src => src.UnitOfMeasurement.ToDescriptionString()));
}
}
}
8 changes: 5 additions & 3 deletions Minimarket.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Minimarket.API.Domain.Db.Contexts;
using Minimarket.API.Domain.Repositories;
using Minimarket.API.Domain.Services;
using AutoMapper;
using AutoMapper;

namespace Minimarket.API
{
Expand All @@ -23,7 +23,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddAutoMapper(typeof(Startup));

services.AddDbContext<AppDbContext>(options =>
Expand All @@ -32,8 +32,10 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddScoped<ICategoryRepository, CategoryRepository>();
services.AddScoped<ICategoryService, CategoryService>();
services.AddScoped<IProductRepository, ProductRepository>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped<ICategoryService, CategoryService>();
services.AddScoped<IProductService, ProductService>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
12 changes: 12 additions & 0 deletions Minimarket.API/ViewModels/ProductViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace Minimarket.API.ViewModels
{
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int QuantityInPackage { get; set; }
public string UnitOfMeasurement { get; set; }
public CategoryViewModel Category {get;set;}
}
}

0 comments on commit 45c830e

Please sign in to comment.