Пример #1
0
        /// <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;
            }
        }
Пример #2
0
        public static BusWebServiceModel FromDataModel(Bus bus)
        {
            if (bus == null)
                return null;

            return new BusWebServiceModel
            {
                GUID = bus.GUID,
                Line = LineWebServiceModel.FromDataModel(bus.Line)
            };
        }
Пример #3
0
        /// <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 TicketWebServiceModel BuyTicket(AccountWebServiceModel account, BusWebServiceModel bus)
        {
            var dmAccount = new Account() {GUID = account.GUID};
            var dmBus = new Bus() {GUID = bus.GUID};

            TicketWebServiceModel ticket = null;

            try
            {
                ticket = TicketWebServiceModel.FromDataModel(ticketService.BuyTicket(dmAccount, dmBus), account);
            }
            catch (Exception)
            {
            }

            return ticket;
        }
Пример #4
0
        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;
            }
        }
Пример #5
0
        private void FixupBus(Bus previousValue)
        {
            if (previousValue != null && previousValue.Tickets.Contains(this))
            {
                previousValue.Tickets.Remove(this);
            }

            if (Bus != null)
            {
                if (!Bus.Tickets.Contains(this))
                {
                    Bus.Tickets.Add(this);
                }
                if (BusGUID != Bus.GUID)
                {
                    BusGUID = Bus.GUID;
                }
            }
        }