public void CreateAccount(Account account) { try { using (CapitalContext context = new CapitalContext()) { account.CreationDate = DateTime.Now; // Clear these objects to prevent duplicate entry. account.AccountType = null; account.Frequency = null; ValidationContext valContext = new ValidationContext(this, null, null); var errors = account.Validate(valContext); if (errors.Count() == 0) { if (context.Accounts.FirstOrDefault(x => x.AccountName == account.AccountName) != null) throw new ModelException("An account by that name already exists! Please use a different name."); context.Accounts.Add(account); context.SaveChanges(); } else throw new ModelException(errors); } } catch (ModelException ex) { throw ex; } catch (Exception ex) { LogError(account, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); } }
public void LogError(object[] dataObj, Exception exception, string functionName) { try { XmlSerializer xml = new XmlSerializer(dataObj.GetType()); StringWriter stream = new StringWriter(); xml.Serialize(stream, dataObj); CapitalLogError error = new CapitalLogError(); error.DataObject = stream.ToString(); error.EventDate = DateTime.Now; error.ExceptionMessage = exception.Message; error.FunctionName = functionName; using (CapitalContext context = new CapitalContext()) { context.Errors.Add(error); context.SaveChanges(); } } catch (Exception) { } }
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 List<AccountType> GetAccountTypes() { try { using (CapitalContext context = new CapitalContext()) { return context.AccountTypes.ToList(); } } catch (Exception) { return null; } }
public List<Frequency> GetFrequencies() { try { using (CapitalContext context = new CapitalContext()) { return context.Frequency.ToList(); } } catch { return null; } }
public void SendFeedback(Feedback userFeedback) { try { using (CapitalContext context = new CapitalContext()) { userFeedback.CreationDate = DateTime.Now; ValidationContext valContext = new ValidationContext(this, null, null); var errors = userFeedback.Validate(valContext); if (errors.Count() > 0) throw new ModelException(errors); context.Feedback.Add(userFeedback); context.SaveChanges(); int maxFeedback = context.Feedback.Max(x => x.FeedbackId); Feedback feedback = context.Feedback.Where(x => x.FeedbackId == maxFeedback).Include(x => x.User).FirstOrDefault(); if (feedback != null) { // Email MailMessage mail = new MailMessage(); SmtpClient smtpServer = new SmtpClient("mail.ilikeitbland.com"); mail.From = new MailAddress(feedback.User.Email); mail.To.Add("*****@*****.**"); mail.Subject = "CAPITAL - FEEDBACK"; mail.IsBodyHtml = true; mail.Body = "<b>" + feedback.ReportType + "</b>" + "<br />" + feedback.Message; smtpServer.Port = 25; smtpServer.Credentials = new System.Net.NetworkCredential("*****@*****.**", "$bland#42"); smtpServer.EnableSsl = false; smtpServer.Send(mail); } } } catch (ModelException ex) { throw ex; } catch (Exception ex) { LogError(userFeedback, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); } }
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; } }
//private TilePushNotificationMessage tilePushNotificationMessage = new TilePushNotificationMessage(MessageSendPriority.High); //private ToastPushNotificationMessage toastPushNotificationMessage = new ToastPushNotificationMessage(MessageSendPriority.High); public void Register(Registration registration) { try { using (CapitalContext context = new CapitalContext()) { registration.CreationDate = DateTime.Now; ValidationContext valContext = new ValidationContext(this, null, null); var errors = registration.Validate(valContext); if (errors.Count() > 0) throw new ModelException(errors); // Check for existing registration first. if (context.Registration.Where(x => x.UserId == registration.UserId).FirstOrDefault() != null) { // Update Registration reg = context.Registration.Where(x => x.UserId == registration.UserId).FirstOrDefault(); reg.URI = registration.URI; } else { // Insert Registration reg = new Registration(); reg.URI = registration.URI; reg.User = registration.User; reg.UserId = registration.UserId; reg.CreationDate = DateTime.Now; context.Registration.Add(reg); } context.SaveChanges(); } } catch (ModelException ex) { throw ex; } catch (Exception ex) { LogError(registration, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); } }
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; } }
public void SendTile(string name) { try { using (CapitalContext context = new CapitalContext()) { var subscribers = context.Registration.Include(u => u.User).Where(u => u.User.Email == "*****@*****.**").FirstOrDefault(); if (subscribers != null) { //http://localhost/CAPITALServices/Tiles/ApplicationIcon.png //tilePushNotificationMessage.BackgroundImageUri = new Uri(string.Format("http://services.ilikeitbland.com/Tiles/{0}", name)); ///tilePushNotificationMessage.SendAsync(new Uri(subscribers.URI, UriKind.Absolute), (result) => { }, (result) => { }); } } } catch(Exception ex) { LogError(name, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); } }
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 void UpdateStatement(Statement statement) { try { using (CapitalContext context = new CapitalContext()) { Statement target = context.Statements.Where(x => x.StatementId == statement.StatementId).FirstOrDefault(); if (target != null) { target.Balance = statement.Balance; target.PaidAmount = statement.PaidAmount; target.DueDate = statement.DueDate; if (statement.IsPaid) { target.IsPaid = statement.IsPaid; target.PaidDate = DateTime.Now; } context.SaveChanges(); } } } catch (Exception ex) { LogError(statement, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); } }
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 ShareAccount(Share share) { try { using (CapitalContext context = new CapitalContext()) { // Check for shared user. User user = context.Users.FirstOrDefault(x => x.UserId == share.UserId); if (user == null) throw new ModelException("That shared user does not exists."); // Does this user already have this account? Account account = context.Accounts.FirstOrDefault(x => x.UserId == share.UserId); if (account != null) throw new ModelException("Shared user is the owner of this account."); // Is this already shared with the target? Share existingShare = context.Share.FirstOrDefault(x => x.AccountId == share.AccountId && x.ShareId == user.UserId); if (existingShare != null) throw new ModelException("Account is already shared with this user."); ValidationContext valContext = new ValidationContext(this, null, null); var errors = share.Validate(valContext); if (errors.Count() > 0) throw new ModelException(errors); else { context.Share.Add(share); context.SaveChanges(); } } } catch (ModelException ex) { throw ex; } catch (Exception ex) { LogError(new object[] {share}, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); } }
public void DeleteAccount(Account account) { try { using (CapitalContext context = new CapitalContext()) { var target = context.Accounts.Where(x => x.AccountId == account.AccountId).FirstOrDefault(); if (target != null) { context.Accounts.Remove(target); context.SaveChanges(); } else throw new ModelException("That account does not exists."); } } catch (ModelException ex) { throw ex; } catch (Exception ex) { LogError(account, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); } }
public void UpdateAccount(Account account) { using (CapitalContext context = new CapitalContext()) { try { ValidationContext valContext = new ValidationContext(this, null, null); var errors = account.Validate(valContext); if (errors.Count() > 0) throw new ModelException(errors); // Get Account Account acc = context.Accounts.Where(x => x.AccountId == account.AccountId).FirstOrDefault(); // Update if (acc != null) { acc.Balance = account.Balance; acc.AccountName = account.AccountName; acc.FrequencyId = account.FrequencyId; acc.StartDate = account.StartDate; acc.DefaultPayment = account.DefaultPayment; // Update latest statement est. Payment amount. Statement statement = context.Statements.Where(x => x.AccountId == acc.AccountId && x.IsPaid == false).FirstOrDefault(); if (statement != null) statement.Balance = acc.DefaultPayment; // Save context.SaveChanges(); } } catch (ModelException ex) { throw ex; } catch (Exception ex) { LogError(account, ex, System.Reflection.MethodBase.GetCurrentMethod().Name); } } }
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); } }