Created
September 6, 2016 00:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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