Skip to content

Instantly share code, notes, and snippets.

@evgomes
Created January 28, 2019 13:53
Show Gist options
  • Save evgomes/2217ebbbf115e89508940ec6a9212b49 to your computer and use it in GitHub Desktop.
Save evgomes/2217ebbbf115e89508940ec6a9212b49 to your computer and use it in GitHub Desktop.
CategoriesController after adding AutoMapper, from Supermarket API
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