Created
January 28, 2019 13:53
-
-
Save evgomes/2217ebbbf115e89508940ec6a9212b49 to your computer and use it in GitHub Desktop.
CategoriesController after adding AutoMapper, from Supermarket API
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
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using AutoMapper; | |
using Microsoft.AspNetCore.Mvc; | |
using Supermarket.API.Domain.Models; | |
using Supermarket.API.Domain.Services; | |
using Supermarket.API.Resources; | |
namespace Supermarket.API.Controllers | |
{ | |
[Route("/api/[controller]")] | |
public class CategoriesController : Controller | |
{ | |
private readonly ICategoryService _categoryService; | |
private readonly IMapper _mapper; | |
public CategoriesController(ICategoryService categoryService, IMapper mapper) | |
{ | |
_categoryService = categoryService; | |
_mapper = mapper; | |
} | |
[HttpGet] | |
public async Task<IEnumerable<CategoryResource>> GetAllAsync() | |
{ | |
var categories = await _categoryService.ListAsync(); | |
var resources = _mapper.Map<IEnumerable<Category>, IEnumerable<CategoryResource>>(categories); | |
return resources; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment