/// <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 TicketWebServiceModel FromDataModel(Ticket ticket, AccountWebServiceModel accountWebServiceModel) { if (ticket == null) return null; return new TicketWebServiceModel { Account = accountWebServiceModel, Created = ticket.Created, GUID = ticket.GUID, Bus = BusWebServiceModel.FromDataModel(ticket.Bus) }; }
public int ValidateTicket(Ticket ticket, Bus bus) { using (ReBusContainer repository = new ReBusContainer()) { ticket = repository.Tickets.Single(t => t.GUID == ticket.GUID); bus = repository.Buses.Single(b => b.GUID == bus.GUID); if (ticket == null || ticket.Bus == null || bus == null) { return 3; } if (ticket.Created.AddHours(1.5) < DateTime.Now) { return 0; } if (ticket.Bus.GUID != bus.GUID) { return 2; } return 1; } }
public static TicketWebServiceModel FromDataModel(Ticket ticket) { return FromDataModel(ticket, AccountWebServiceModel.FromModelObject(ticket == null ? null : ticket.Account)); }