public Bus GetBus(Guid bus) { using (var db = new ReBusContainer()) { return db.Buses.Include("Line").SingleOrDefault(b => b.GUID == bus); } }
public Account GetAccount(Guid guid) { using (var db = new ReBusContainer()) { return db.Accounts.SingleOrDefault(a => a.GUID == guid); } }
public Subscription GetSubscription(Guid subscription) { using (var db = new ReBusContainer()) { return db.Subscriptions.Include("Account").Include("Lines").SingleOrDefault(s => s.GUID == subscription); } }
public IEnumerable<Line> GetLines() { using (var db = new ReBusContainer()) { return db.Lines.ToList(); } }
public IDictionary<int, decimal> GetSubscriptionPrices() { using (var db = new ReBusContainer()) { return db.SubscriptionCosts.ToDictionary(s => s.Lines, s => s.Cost); } }
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; } }
public Ticket GetTicket(Guid ticket) { using (var db = new ReBusContainer()) { return db.Tickets.Include("Account").Include("Bus").Include("Bus.Line").SingleOrDefault(t => t.GUID == ticket); } }
/// <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 decimal GetTicketPrice() { using (var db = new ReBusContainer()) { return db.TicketCost.Single().Cost; } }
public Account AddFunds(Account account, decimal amount) { using (var db = new ReBusContainer()) { account = db.Accounts.Single(a => a.GUID == account.GUID); account.Credit += amount; return account; } }
public IEnumerable<Transaction> GetTransactionHistory(Account userAccount) { using (ReBusContainer repository = new ReBusContainer()) { repository.AttachTo("Accounts", userAccount); repository.Refresh(RefreshMode.StoreWins, userAccount); return userAccount.Transactions; } }
/// <summary> /// Get the currently active ticket of an account. /// </summary> /// <param name="account">The account for which to get the active ticket</param> /// <returns></returns> public Ticket GetActiveTicket(Account account) { using (var db = new ReBusContainer()) { DateTime validity = DateTime.Now - new TimeSpan(0, 0, 45); return db.Tickets.Include("Account").Include("Bus").Include("Bus.Line") .Where(t => t.AccountGUID == account.GUID) .Where(t => t.Created >= validity) .OrderByDescending(t => t.Created) .FirstOrDefault(); } }
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; } }
private Account AuthenticateUser(string userName, string password, bool isTicketController) { using (var repository = new ReBusContainer()) { return (from currentUser in repository.Accounts where currentUser.Username == userName && currentUser.PasswordHash == password && currentUser.IsTicketController == isTicketController select currentUser).FirstOrDefault<Account>(); } }
private bool UsernameIsUnique(string userName) { using (var repository = new ReBusContainer()) { return (from currentUser in repository.Accounts where currentUser.Username == userName select currentUser).FirstOrDefault<Account>() == null; } }
/// <summary> /// Get older tickets. /// </summary> /// <param name="account">The account for which to get the tickets</param> /// <param name="before">The date before which to get the tickets</param> /// <param name="limit">The max number of thickets to fetch</param> /// <returns></returns> public IEnumerable<Ticket> GetHistory(Account account, DateTime before, int limit) { using (var db = new ReBusContainer()) { return db.Tickets.Include("Account").Include("Bus").Include("Bus.Line") .Where(t => t.AccountGUID == account.GUID) .Where(t => t.Created < before) .OrderByDescending(t => t.Created) .Take(limit).ToList(); } }
/// <summary> /// Get the tickets that have been added after a given date (used to refresh the list with new values). /// </summary> /// <param name="account">The account for which to get the tickets</param> /// <param name="after">The date of the last ticket the client has</param> /// <returns></returns> public IEnumerable<Ticket> GetNewTickets(Account account, DateTime after) { using (var db = new ReBusContainer()) { return db.Tickets.Include("Account").Include("Bus").Include("Bus.Line") .Where(t => t.AccountGUID == account.GUID) .Where(t => t.Created > after) .OrderByDescending(t => t.Created).ToList(); } }