Exemplo n.º 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;
            }
        }
Exemplo n.º 2
0
        public Account Register(string userName, string password, string firstName, string lastName)
        {
            if (String.IsNullOrEmpty(userName) ||
                String.IsNullOrEmpty(password) ||
                String.IsNullOrEmpty(firstName) ||
                String.IsNullOrEmpty(lastName))
            {
                throw new NotAllFieldsFilledException();
            }

            if (!UsernameIsUnique(userName))
            {
                throw new UserAlreadyExistsException();
            }

            using (ReBusContainer repository = new ReBusContainer())
            {
                Account userAccount = new Account();

                userAccount.Username = userName;
                userAccount.PasswordHash = password;
                userAccount.FirstName = firstName;
                userAccount.LastName = lastName;
                userAccount.Credit = 0;

                repository.Accounts.AddObject(userAccount);
                repository.SaveChanges();

                return userAccount;
            }
        }