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 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 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); } } }
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 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 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; } }