public Validation ValidateNewBidder(Bidder bidder, ref IAuctionTransaction trans) { var val = new Validation(); if (string.IsNullOrEmpty(bidder.Name)) { val.AddError("Name is empty"); } if (string.IsNullOrEmpty(bidder.Phone)) { val.AddError("Phone number is empty"); } else if (!PhoneNumberIsFormattedCorrectly(bidder.Phone)) { val.AddError("Phone number is not in the correct format"); } if (!_eventRepo.EventExists(bidder.EventId, ref trans)) { val.AddError("Must be in valid auction"); } return val; }
public Validation UpdateBidder(ref Bidder eventBidder) { var val = new Validation(); var trans = _factory.BuildTransaction("UpdateBidder"); try { val = _validator.ValidateExistingBidder(eventBidder, ref trans); if(val.IsValid) { _repo.Update(ref eventBidder, ref trans); } trans.Commit(); } catch(Exception e) { trans.Rollback(); val.AddError(string.Format("Unable to update bidder: {0}", e.Message)); } finally { trans.Dispose(); } return val; }
public Validation CreateBidder(ref Bidder eventBidder, long currentUserId) { var val = new Validation(); var trans = _factory.BuildTransaction("CreateBidder"); try { val = _validator.ValidateNewBidder(eventBidder, ref trans); if(val.IsValid) { eventBidder.Number = _repo.GetByEvent(eventBidder.EventId, ref trans).Select(b => b.Number).Max() + 1; eventBidder.CreatedBy = currentUserId; _repo.Insert(ref eventBidder, ref trans); } trans.Commit(); } catch(Exception e) { trans.Rollback(); val.AddError(string.Format("Unable to create bidder: {0}", e.Message)); } finally { trans.Dispose(); } return val; }
public Validation ValidateExistingBidder(Bidder bidder, ref IAuctionTransaction trans) { var val = new Validation(); if (bidder.Id <= 0) { val.AddError("Bidder must have an id"); } if (string.IsNullOrEmpty(bidder.Name)) { val.AddError("Name is empty"); } if (string.IsNullOrEmpty(bidder.Phone)) { val.AddError("Phone number is empty"); } else if (!PhoneNumberIsFormattedCorrectly(bidder.Phone)) { val.AddError("Phone number is not in the correct format"); } if (!_eventRepo.EventExists(bidder.EventId, ref trans)) { val.AddError("Must be in valid auction"); } else { if (bidder.Id > 0) { var bidderInSystem = _bidderRepo.GetByNumberAndEvent(bidder.Number, bidder.EventId, ref trans); if (bidderInSystem.Id != bidder.Id) { val.AddError("Invalid bidder number"); } } } return val; }