Skip to content

Instantly share code, notes, and snippets.

@krotkiewicz
Forked from aramkoukia/ProductsController.cs
Created February 29, 2020 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krotkiewicz/5e1fdde917cca8c5f80b8e3286871bf6 to your computer and use it in GitHub Desktop.
Save krotkiewicz/5e1fdde917cca8c5f80b8e3286871bf6 to your computer and use it in GitHub Desktop.
Products Controller
using System;
using System.Web.Http;
using System.Net;
using System.Net.Http;
using Products.Service.DataTransferObjects.Commands;
using Products.Service.MicroServices.Products.Commands;
using Products.Service.MicroServices.Products.Handlers;
using MicroServices.Common.Exceptions;
namespace Products.Service.Controllers
{
public class ProductsController : ApiController
{
private readonly ProductCommandHandlers handler;
public ProductsController()
: this(ServiceLocator.ProductCommands)
{}
public ProductsController(ProductCommandHandlers handler)
{
this.handler = handler;
}
[HttpPost]
public IHttpActionResult Post(CreateProductCommand cmd)
{
if (string.IsNullOrWhiteSpace(cmd.Name))
{
var response = new HttpResponseMessage(HttpStatusCode.Forbidden)
{
Content = new StringContent("code must be supplied in the body"),
ReasonPhrase = "Missing product code"
};
throw new HttpResponseException(response);
}
try
{
var command = new CreateProduct(Guid.NewGuid(), cmd.Name, cmd.Description, cmd.Price);
handler.Handle(command);
var link = new Uri(string.Format("http://localhost:8181/api/products/{0}", command.Id));
return Created<CreateProduct>(link, command);
}
catch (AggregateNotFoundException)
{
return NotFound();
}
catch (AggregateDeletedException)
{
return Conflict();
}
}
[HttpPut]
[Route("api/products/{id:guid}")]
public IHttpActionResult Put(Guid id, AlterProductCommand cmd)
{
if (string.IsNullOrWhiteSpace(cmd.Name))
{
var response = new HttpResponseMessage(HttpStatusCode.Forbidden)
{
Content = new StringContent("code must be supplied in the body"),
ReasonPhrase = "Missing product code"
};
throw new HttpResponseException(response);
}
try
{
var command = new AlterProduct(id, cmd.Version, cmd.Name, cmd.Description, cmd.Price);
handler.Handle(command);
return Ok(command);
}
catch (AggregateNotFoundException)
{
return NotFound();
}
catch (AggregateDeletedException)
{
return Conflict();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment