Skip to content

Instantly share code, notes, and snippets.

@aramkoukia
Created January 20, 2018 22:42
Show Gist options
  • Save aramkoukia/6980ca0ddef3ba1e9be42abca0272c99 to your computer and use it in GitHub Desktop.
Save aramkoukia/6980ca0ddef3ba1e9be42abca0272c99 to your computer and use it in GitHub Desktop.
Product Command Handlers
using System;
using Products.Common.Dto;
using Products.Service.MicroServices.Products.Commands;
using MicroServices.Common.Repository;
namespace Products.Service.MicroServices.Products.Handlers
{
public class ProductCommandHandlers
{
private readonly IRepository repository;
public ProductCommandHandlers(IRepository repository)
{
this.repository = repository;
}
public void Handle(CreateProduct message)
{
// Validation
ProductDto existingProduct = null;
// Process
var series = new Products.Domain.Product(message.Id, message.Name, message.Description, message.Price);
repository.Save(series);
}
public void Handle(AlterProduct message)
{
var product = repository.GetById<Products.Domain.Product>(message.Id);
int committedVersion = message.OriginalVersion;
if (!String.Equals(product.Name, message.NewTitle, StringComparison.OrdinalIgnoreCase))
{
product.ChangeName(message.NewTitle, committedVersion++);
}
if (!String.Equals(product.Description, message.NewDescription, StringComparison.OrdinalIgnoreCase))
{
product.ChangeDescription(message.NewDescription, committedVersion++);
}
if (message.NewPrice != product.Price)
{
product.ChangePrice(message.NewPrice, committedVersion);
}
repository.Save(product);
}
}
}
@MRdNk
Copy link

MRdNk commented Feb 24, 2019

Should ...
product.ChangePrice(message.NewPrice, committedVersion);

be...
product.ChangePrice(message.NewPrice, committedVersion++);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment