Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created October 12, 2017 13:17
Show Gist options
  • Save dcomartin/782ad05e27d613484eb4e1d2fe189c39 to your computer and use it in GitHub Desktop.
Save dcomartin/782ad05e27d613484eb4e1d2fe189c39 to your computer and use it in GitHub Desktop.
[Route("api/[controller]")]
public class CustomersController : Controller
{
private readonly CustomerRepository _customerRepository;
public CustomersController()
{
_customerRepository = new CustomerRepository();
}
// GET api/values/5
[HttpGet("{id}")]
public IActionResult Get(int id)
{
// Returns null if customer doesn't exist.
var customer = _customerRepository.GetOptionById(id);
return customer.Match<IActionResult>(
some: value => new JsonResult(new
{
CustomerId = value.CustomerId,
Name = value.Name
}),
none: () => new StatusCodeResult(404));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment