public User Login(User user) { try { using (CapitalContext context = new CapitalContext()) { var checkUser = context.Users .Where(x => x.Email.ToLower().Equals(user.Email.ToLower()) && x.Password.Equals(user.Password)) .FirstOrDefault(); if (checkUser != null) { if (user.Devices != null) { if (user.Devices.Count > 0) { Device temp = user.Devices.SingleOrDefault(); if (context.Devices.Where(x => x.UserId == checkUser.UserId && x.UniqueDeviceId == temp.UniqueDeviceId).FirstOrDefault() == null) { // New Phone temp.UserId = checkUser.UserId; temp.CreationDate = DateTime.Now; context.Devices.Add(temp); context.SaveChanges(); } } } checkUser.LastAccessDate = DateTime.Now; context.SaveChanges(); } else { throw new ModelException("Invalid Email Address and/or Password!"); } return checkUser; } } catch (ModelException ex) { throw ex; } catch (Exception ex) { LogError(user, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); throw ex; } }
public User CreateUser(User user) { try { using (CapitalContext context = new CapitalContext()) { if (context.Users.Where(x => x.Email.ToLower() == user.Email.ToLower()).FirstOrDefault() == null) { user.CreationDate = DateTime.Now; user.LastAccessDate = DateTime.Now; ValidationContext valContext = new ValidationContext(this, null, null); var errors = user.Validate(valContext); if (errors.Count() == 0) { context.Users.Add(user); context.SaveChanges(); return context.Users.FirstOrDefault(x => x.Email == user.Email); } else throw new ModelException(errors); } else { throw new ModelException("Email Address Already Exists!"); } } } catch (DbEntityValidationException ex) { throw new ModelException(ex); } catch (DbUnexpectedValidationException ex) { throw new ModelException(ex); } catch (ModelException ex) { throw ex; } catch (Exception ex) { LogError(user, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); throw ex; } }
public IEnumerable<Account> GetAccounts(User source) { try { AccountManagement accountManagement = new AccountManagement(); return accountManagement.GetAccounts(source); } catch (ModelException ex) { CapitalError error = new CapitalError(ex); throw new FaultException<CapitalError>(error, error.Message); } catch (Exception) { return null; } }
public User Login(User user) { try { UserManagement userManagement = new UserManagement(); return userManagement.Login(user); } catch (ModelException ex) { CapitalError error = new CapitalError(ex); throw new FaultException<CapitalError>(error, error.Message); } catch (Exception) { return null; } }
public IEnumerable<Account> GetAccounts(User source) { try { using (CapitalContext context = new CapitalContext()) { // Compare Accounts for that UserID List<Account> accounts = context.Accounts.Where(x => x.UserId == source.UserId).OrderBy(x => x.AccountName).ToList(); foreach (Account acc in accounts) { //acc.Balance = acc.Balance - (context.Statements.Where(x => x.AccountId == acc.AccountId && x.IsPaid == true).AsEnumerable().Sum(x => x.PaidAmount)); acc.IsShare = false; acc.PermissionLevel = 0; } // LINK SHARED ACCOUNTS var shares = context.Share.Where(x => x.UserId == source.UserId); foreach(Share share in shares) { Account account = context.Accounts.FirstOrDefault(x => x.AccountId == share.AccountId); if (account != null) { account.IsShare = true; account.PermissionLevel = share.PermissionLevelId; accounts.Add(account); } } return accounts; } } catch (Exception ex) { LogError(source, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); return null; } }
private void AgeAccounts(User user, CapitalContext context) { try { IEnumerable<Account> accounts = context.Accounts.Where(x => x.UserId == user.UserId).Include(x => x.Frequency).AsEnumerable(); foreach (Account acc in accounts) { int? count = context.Statements.Where(x => x.Account.AccountId == acc.AccountId).Count(); if (count == 0) { Statement statement = new Statement(); statement.AccountId = acc.AccountId; statement.Balance = acc.DefaultPayment; statement.IsPaid = false; statement.CreationDate = DateTime.Now; statement.PaidAmount = 0; statement.PaidDate = null; // int freqDays = context.Frequency.Where(x => x.FrequencyId == context.Accounts.Where(y => y.AccountId == acc.AccountId).FirstOrDefault().FrequencyId).FirstOrDefault().Days; statement.DueDate = acc.StartDate; context.Statements.Add(statement); } else { int id = context.Statements.Where(x => x.Account.UserId == user.UserId).Max(x => x.StatementId); Statement latestStatement = context.Statements.Where(x => x.StatementId == id).FirstOrDefault(); TimeSpan difference = DateTime.Now - (DateTime)latestStatement.DueDate; // Make sure the existing unPaid statement doesn't fall within the 7 day period //if (difference.Days >= (acc.Frequency.Days - 7) && difference.Days > 7) if (latestStatement.IsPaid) { // Get Average of previous payments decimal sumPayments = (decimal)context.Statements.Where(x => x.AccountId == acc.AccountId && x.IsPaid == true).AsEnumerable().Sum(x => x.PaidAmount); int totalPayments = (int)context.Statements.Where(x => x.AccountId == acc.AccountId && x.IsPaid == true).Count(); Statement statement = new Statement(); statement.AccountId = acc.AccountId; //statement.Balance = acc.Payment; if (totalPayments != 0) statement.Balance = (double)Math.Round(sumPayments / totalPayments, 2); else statement.Balance = latestStatement.Balance; statement.IsPaid = false; statement.CreationDate = DateTime.Now; statement.PaidAmount = 0; statement.PaidDate = null; Frequency freq = context.Accounts.Where(x => x.AccountId == acc.AccountId).FirstOrDefault().Frequency; int freqDays = context.Accounts.Where(x => x.AccountId == acc.AccountId).FirstOrDefault().Frequency.Days; statement.DueDate = DateTime.Now.AddDays(freqDays); context.Statements.Add(statement); MessengerUtil messenger = new MessengerUtil(); messenger.SendToast(user, string.Format("New {0} Statement!", statement.Account.AccountName), ""); } } context.SaveChanges(); } } catch (Exception ex) { LogError(user, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); throw new Exception("Error Aging Account", ex); } }
public Statement GetStatementById(User user, int statementId) { try { using (CapitalContext context = new CapitalContext()) { Statement statement = context.Statements.Include(x => x.Account).Where(x => x.Account.UserId == user.UserId && x.StatementId == statementId).FirstOrDefault(); Statement result = statement; result.AccountName = statement.Account.AccountName; statement.Account = null; return result; } } catch(Exception ex) { LogError(new object[] { user, statementId }, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); return null; } }
public IEnumerable<Statement> GetStatements(User user) { try { using (var transaction = TransactionUtils.CreateTransactionScope()) { using (CapitalContext context = new CapitalContext()) { // Age Accounts AgeAccounts(user, context); // Get Statements List<Statement> statementCollection = new List<Statement>(); var statements = context.Statements.Where(x => x.IsPaid == false && x.Account.UserId == user.UserId).AsEnumerable().OrderBy(x => x.DueDate); foreach (Statement statement in statements) { statement.AccountName = statement.Account.AccountName; statement.Account = null; statementCollection.Add(statement); } var recentStatements = context.Statements.Where(x => x.IsPaid == true && x.Account.UserId == user.UserId && SqlFunctions.DateDiff("DAY", x.PaidDate, DateTime.Now).Value <= 30).Include(x => x.Account).AsEnumerable(); foreach (Statement statement in recentStatements) { statement.AccountName = statement.Account.AccountName; statement.Account = null; statementCollection.Add(statement); } transaction.Complete(); return statementCollection; } } } catch (Exception ex) { LogError(user, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); return null; } }
public void SendToast(User user, string title, string subTitle) { try { using (CapitalContext context = new CapitalContext()) { Registration registration = context.Registration.FirstOrDefault(x => x.UserId == user.UserId); if (registration != null) { //toastPushNotificationMessage.Title = title; //toastPushNotificationMessage.SubTitle = subTitle; //toastPushNotificationMessage.SendAsync(new Uri(registration.URI, UriKind.Absolute), (result) => { }, (result) => { }); } } } catch(Exception ex) { LogError(new object[] { user, title, subTitle }, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); } }
public void SendToast(User user, string title, string subTitle) { try { MessengerUtil messenger = new MessengerUtil(); messenger.SendToast(user, title, subTitle); } catch (ModelException ex) { CapitalError error = new CapitalError(ex); throw new FaultException<CapitalError>(error, error.Message); } catch (Exception) { return; } }
public Statement GetStatementById(User user, int id) { try { AccountManagement accountManagement = new AccountManagement(); return accountManagement.GetStatementById(user, id); } catch (ModelException ex) { CapitalError error = new CapitalError(ex); throw new FaultException<CapitalError>(error, error.Message); } catch (Exception) { return null; } }