Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Created September 6, 2016 00:37
Show Gist options
  • Save vkhorikov/5eebbd8031c69f7fe92bc9fe3ed2f60c to your computer and use it in GitHub Desktop.
Save vkhorikov/5eebbd8031c69f7fe92bc9fe3ed2f60c to your computer and use it in GitHub Desktop.
Promote method with a possibility to fail while saving data to the DB
[HttpPost]
[Route("customers/{id}/promotion")]
public HttpResponseMessage Promote(long id)
{
return _customerRepository.GetById(id)
.ToResult("Customer with such Id is not found: " + id)
.Ensure(customer => customer.CanBePromoted(), "The customer has the highest status possible")
.OnSuccess(customer => customer.Promote())
.OnSuccess(customer => _unitOfWork.Flush().Map(() => customer))
.OnSuccess(customer => _emailGateway.SendPromotionNotification(customer.PrimaryEmail, customer.Status)
.OnFailure(() => _unitOfWork.RollBack()))
.OnBoth(result => result.IsSuccess ? Ok() : Error(result.Error));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment