/// <summary> /// Buy a new ticket. /// </summary> /// <param name="account">The account for which to buy the ticket</param> /// <param name="bus">The bus for which to buy the ticket</param> /// <returns></returns> public Ticket BuyTicket(Account account, Bus bus) { using (ReBusContainer db = new ReBusContainer()) { account = db.Accounts.Single(a => a.GUID == account.GUID); bus = db.Buses.Include("Line").Single(b => b.GUID == bus.GUID); var cost = db.TicketCost.Single().Cost; if (cost > account.Credit) { throw new InsufficientCreditException(); } var ticket = new Ticket {Account = account, Bus = bus, Created = DateTime.Now}; var transaction = new Transaction { Account = account, Amount = cost, Type = (int) TransactionType.Ticket, Created = DateTime.Now }; account.Credit -= cost; db.Tickets.AddObject(ticket); db.Transactions.AddObject(transaction); db.SaveChanges(); return ticket; } }
public static TransactionWebServiceModel FromModelObject(Transaction transaction) { if (transaction == null) return null; return new TransactionWebServiceModel() { Amount = transaction.Amount, Id = transaction.Id, Type = transaction.Type, Created = transaction.Created }; }