Created
May 10, 2013 17:30
-
-
Save Maarten88/5555989 to your computer and use it in GitHub Desktop.
This file contains 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
public class CreateNewBid : Command<Bid> | |
{ | |
Logger logger = LogManager.GetCurrentClassLogger(); | |
public Guid UserId { set; private get; } | |
public int LotId { set; private get; } | |
public Decimal Price { set; get; } | |
public override Bid Execute(IDomainContextImpl context) | |
{ | |
Bid bid = null; | |
Lot lot = null; | |
var bidSuccess = false; | |
using (var transaction = context.DataContext.BeginTransaction()) | |
{ | |
try | |
{ | |
lot = new SingleLot(LotId).Execute(context.DataContext); | |
if (lot.Available > 0) | |
{ | |
lot.Available--; | |
lot.Reserved++; | |
context.DataContext.Update<Lot>(lot); | |
bidSuccess = true; | |
} | |
bid = new Bid() | |
{ | |
AuctionId = lot.AuctionId, | |
TimeStamp = DateTime.UtcNow, | |
IsWinningBid = bidSuccess, | |
LotId = lot.Id, | |
Price = lot.CurrentPrice == null ? lot.StartPrice : lot.CurrentPrice > Price ? lot.CurrentPrice.Value : Price, | |
UserId = UserId | |
}; | |
var id = context.DataContext.Insert<Bid>(bid); | |
bid.Id = id.InsertedId<int>(); | |
transaction.Commit(); | |
logger.Info("Transaction Lot succesfull"); | |
} | |
catch (Exception e) | |
{ | |
bidSuccess = false; | |
bid.IsWinningBid = false; | |
transaction.Rollback(); | |
logger.WarnException("Updating lot unsuccesfull, bid failed, transaction rolled back", e); | |
} | |
} | |
// communicate this update to the client | |
if (bidSuccess) | |
{ | |
context.DomainEvents.Publish<AvailableUpdatedEvent>(new AvailableUpdatedEvent() { LotId = this.LotId, NewAvailable = lot.Available }); | |
} | |
return bid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment