public UserMessage CreateCardAccount(CreateCardAccountCommand command) { EnsureIsValid(command); try { var user = _deps.Users.SurelyFind(command.UserId); var cardVendor = _deps.CardVendors.SurelyFind(command.CardVendorId); var currency = _deps.Currencies.SurelyFind(command.CurrencyISOName); var cardAccount = _deps.CardAccountFactory.Create( currency, user, command.AccountExpirationDateUtc); _deps.CardAccounts.Create(cardAccount); var userCard = _deps.UserCardFactory.Create( cardAccount, cardVendor, user, command.CardholderFirstName, command.CardholderLastName, command.CardExpirationDateUtc); _deps.UserCards.Create(userCard); var shouldDeposit = command.InitialBalance > 0; var events = new List <ApplicationEvent>(); if (shouldDeposit) { var name = _deps.TransactionReferenceBook.ForCashDeposit(); var money = new Money(currency, command.InitialBalance); var deposit = cardAccount.Deposit(userCard, name.Code, name.Description, _bankSettings.Location, money, _deps.MoneyConverter); _deps.Transactions.Create(deposit); events.Add(new TransactionProgressEvent(Operation.Id, deposit.ToModel <TransactionModel>())); } Commit(); events.ForEach(Publish); return(UserMessage.ResourceFormat(() => Messages.AccountOpened, cardAccount.AccountNo)); } catch (ServiceException) { throw; } catch (Exception ex) { throw new ServiceException("Cannot create card account.", ex); } }
public IHttpActionResult Create(CreateCardAccountCommand command) { return(Ok(_cardAccountService.CreateCardAccount(command))); }