-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from AEMLoviji/feat/support-CRUD-for-products-…
…entity Feat/support crud for products entity
- Loading branch information
Showing
12 changed files
with
158 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;} | ||
} | ||
} |