Beispiel #1
0
        /// <summary>
        /// Deletes a bill by user id and bill id
        /// </summary>
        /// <param name="userId">The user id</param>
        /// <param name="billId">The bill id</param>
        public void DeleteUserBillByBillId(int userId, int billId)
        {
            var bill = GetUserBillByBillId(userId, billId);

            if (bill == null)
            {
                return;
            }

            using var ctx = new MyBillsContext();
            var userBill = ctx.UserBills.FirstOrDefault(x => x.BillId == bill.Id);

            if (userBill == null)
            {
                return;
            }

            var userBillRecurrenceScheduleId = userBill.RecurrenceScheduleId;

            ctx.UserBills.RemoveRange(ctx.UserBills.Where(x => x.BillId == bill.Id).AsEnumerable());
            ctx.UserBillRecurrenceSchedule.RemoveRange(ctx.UserBillRecurrenceSchedule.Where(x => x.Id == userBillRecurrenceScheduleId).AsEnumerable());
            ctx.Bills.Attach(bill);
            ctx.Bills.Remove(bill);
            ctx.SaveChanges();
        }
Beispiel #2
0
        /// <summary>
        /// Generates a random password
        /// </summary>
        /// <returns></returns>
        private string GenerateRandomPassword()
        {
            var generatedPassword = String.Empty;

            try
            {
                var rnd    = new Random();
                var first  = rnd.Next(1, 200);
                var second = rnd.Next(1, 200);
                var idList = new List <int>
                {
                    first,
                    second
                };

                List <Words> randomWords;
                using (var ctx = new MyBillsContext())
                {
                    randomWords = ctx.Words.Where(e => idList.Contains(e.Id)).ToList();
                }

                generatedPassword = randomWords.Aggregate(generatedPassword, (current, word) => current + (word.Word + "_"));
            }
            catch (Exception ex)
            {
                _logRepository.WriteLog(LogLevel.Error, "User.GenerateRandomPassword", ex.Message, ex);
                generatedPassword = "******";
            }

            return(generatedPassword.TrimEnd('_'));
        }
Beispiel #3
0
        /// <summary>
        /// Gets the user by username
        /// </summary>
        /// <param name="username">The username</param>
        /// <returns></returns>
        private static User GetUserByUsername(string username)
        {
            User user;

            using (var ctx = new MyBillsContext())
            {
                user = ctx.Users.First(x => x.Username == username);
            }
            return(user);
        }
Beispiel #4
0
        /// <summary>
        /// Gets the user by username
        /// </summary>
        /// <param name="username">The username</param>
        /// <returns></returns>
        private static async Task <User> GetUserByUsernameAsync(string username)
        {
            User user;

            await using (var ctx = new MyBillsContext())
            {
                user = await ctx.Users.SingleAsync(x => x.Username == username);
            }
            return(user);
        }
Beispiel #5
0
        /// <summary>
        /// Gets the password hash by the passed username
        /// </summary>
        /// <param name="username">The username</param>
        /// <returns></returns>
        private static string GetPasswordHashByEmail(string username)
        {
            User user;

            using (var ctx = new MyBillsContext())
            {
                user = ctx.Users.First(x => x.Username == username);
            }
            return(user.PasswordHash);
        }
Beispiel #6
0
        /// <summary>
        /// Checks if user exists by username
        /// </summary>
        /// <param name="username">The username</param>
        /// <returns></returns>
        public bool FindUserByUsername(string username)
        {
            User user;

            using (var ctx = new MyBillsContext())
            {
                user = ctx.Users.FirstOrDefault(x => x.Username == username);
            }

            return(user != null);
        }
Beispiel #7
0
        /// <summary>
        /// Checks if user exists by email address
        /// </summary>
        /// <param name="emailAddress">The email address</param>
        /// <returns></returns>
        public bool FindUserByEmailAddress(string emailAddress)
        {
            User user;

            using (var ctx = new MyBillsContext())
            {
                user = ctx.Users.FirstOrDefault(x => x.Email == emailAddress);
            }

            return(user != null);
        }
Beispiel #8
0
        /// <summary>
        /// Gets the bill recurrence types
        /// </summary>
        /// <returns></returns>
        public async Task <List <RecurrenceType> > GetRecurrenceTypesAsync()
        {
            List <RecurrenceType> recurrenceTypeList;

            await using (var ctx = new MyBillsContext())
            {
                recurrenceTypeList = await ctx.RecurrenceType.OrderBy(x => x.Id).ToListAsync();
            }

            return(recurrenceTypeList);
        }
Beispiel #9
0
        /// <summary>
        /// Gets the user details by user id
        /// </summary>
        /// <param name="userId">The user id</param>
        /// <returns></returns>
        public async Task <UserDetail> GetUserDetailByUserIdAsync(int userId)
        {
            UserDetail userDetails;

            await using (var ctx = new MyBillsContext())
            {
                userDetails = await(from ud in ctx.UserDetails
                                    where ud.User.Id == userId
                                    select ud).FirstOrDefaultAsync();
            }

            return(userDetails);
        }
        /// <summary>
        /// Gets all bills for the user and month and year
        /// </summary>
        /// <param name="userId">The user id</param>
        /// <param name="month">The month</param>
        /// <param name="year">The year</param>
        /// <returns></returns>
        public List <UserBill> GetBillsByUserIdAndMonthYear(int userId, int month, int year)
        {
            List <UserBill> userBills;

            using (var ctx = new MyBillsContext())
            {
                userBills = ctx.UserBills
                            .Include(x => x.Bill)
                            .Where(x => x.UserId == userId && x.Month == month && x.Year == year && x.Bill.IsComplete == false)
                            .OrderBy(x => x.Day)
                            .ToList();
            }

            return(userBills);
        }
        /// <summary>
        /// Marks a bill a paid
        /// </summary>
        /// <param name="billId">The bill id</param>
        /// <param name="userId">The user id</param>
        /// <param name="day">The bill day</param>
        /// <param name="month">The bill month</param>
        /// <param name="year">The bill year</param>
        public void MarkBillAsPaid(int billId, int userId, int day, int month, int year)
        {
            using var ctx = new MyBillsContext();
            var bill = ctx.UserBills.SingleOrDefault(x => x.BillId == billId && x.User.Id == userId && x.Day == day && x.Month == month && x.Year == year);

            if (bill == null)
            {
                return;
            }

            var newValue = !bill.IsPaid;

            bill.IsPaid = newValue;

            ctx.SaveChanges();
        }
Beispiel #12
0
        /// <summary>
        /// Creates a new bill
        /// </summary>
        /// <param name="bill">The new bill to create</param>
        /// <returns></returns>
        public Bill CreateNewBill(Bill bill)
        {
            using var ctx = new MyBillsContext();
            var newBill = new Bill
            {
                Name       = bill.Name,
                Amount     = bill.Amount,
                IsAutoPaid = bill.IsAutoPaid,
                IsComplete = bill.IsComplete
            };

            ctx.Bills.Add(newBill);

            ctx.SaveChanges();

            return(newBill);
        }
        /// <summary>
        /// Creates a new recurrence schedule
        /// </summary>
        /// <param name="recurrenceTypeId">The recurrence type id</param>
        /// <param name="schedule">The recurrence schedule</param>
        /// <returns></returns>
        public RecurrenceSchedule CreateNewRecurrenceSchedule(int recurrenceTypeId, string schedule)
        {
            using var ctx = new MyBillsContext();
            var recurrenceType = ctx.RecurrenceType.FirstOrDefault(x => x.Id == recurrenceTypeId);

            var userBillRecurrenceSchedule = new RecurrenceSchedule
            {
                RecurrenceType   = recurrenceType,
                RecurrenceTypeId = recurrenceTypeId,
                Schedule         = schedule
            };

            ctx.UserBillRecurrenceSchedule.Add(userBillRecurrenceSchedule);

            ctx.SaveChanges();

            return(userBillRecurrenceSchedule);
        }
Beispiel #14
0
        /// <summary>
        /// Gets user bills by user id
        /// </summary>
        /// <param name="userId">The user id</param>
        /// <returns></returns>
        public List <Bill> GetUserBills(int userId)
        {
            List <Bill> filteredList;

            using (var ctx = new MyBillsContext())
            {
                var bills = (from ub in ctx.UserBills
                             join bill in ctx.Bills on ub.Bill equals bill
                             where ub.User.Id == userId
                             select bill).ToList();

                //Filter out dupes
                filteredList = bills.GroupBy(x => x.Id)
                               .Select(grp => grp.First())
                               .ToList();
            }

            return(filteredList);
        }
        /// <summary>
        /// Create a new user bill
        /// </summary>
        /// <param name="userId">The user id</param>
        /// <param name="bill">The bill model</param>
        /// <param name="model">The recurrence model</param>
        /// <param name="recurrenceSchedule">The recurrence schedule</param>
        public void CreateNewUserBill(int userId, Bill bill, IRecurrenceModel model, RecurrenceSchedule recurrenceSchedule)
        {
            using var ctx = new MyBillsContext();
            var billDetail = new UserBillDetail
            {
                UserId               = userId,
                BillId               = bill.Id,
                Month                = DateTime.Today.Month,
                Year                 = DateTime.Today.Year,
                RecurrenceTypeName   = recurrenceSchedule.RecurrenceType.Name,
                RecurrenceTypeId     = recurrenceSchedule.RecurrenceTypeId,
                Schedule             = recurrenceSchedule.Schedule,
                RecurrenceScheduleId = recurrenceSchedule.Id
            };

            CreateUserBills(ctx, model, billDetail);

            ctx.SaveChanges();
        }
Beispiel #16
0
        /// <summary>
        /// Add the user details to the registered user
        /// </summary>
        /// <param name="user">The <see cref="User"></see></param>
        /// <param name="friendlyName">The friendly name of the user</param>
        public void AddDetailsToUser(User user, string friendlyName)
        {
            try
            {
                using var ctx = new MyBillsContext();
                var userAccount = ctx.Users.Single(x => x.Id == user.Id);
                var ud          = new UserDetail
                {
                    User      = userAccount,
                    FirstName = friendlyName
                };

                ctx.UserDetails.Add(ud);
                ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                _logRepository.WriteLog(LogLevel.Error, "UserRepository.AddDetailsToUser", ex.Message, ex);
            }
        }
Beispiel #17
0
        /// <summary>
        /// Get a user bill entity by user id and bill id
        /// </summary>
        /// <param name="userId">The user id</param>
        /// <param name="billId">The bill id</param>
        /// <returns></returns>
        public Bill GetUserBillByBillId(int userId, int billId)
        {
            List <Bill> filteredList;

            using (var ctx = new MyBillsContext())
            {
                var bills = (from ub in ctx.UserBills
                             join userBill in ctx.Bills on ub.Bill equals userBill
                             join user in ctx.Users on ub.User.Id equals userId
                             where ub.Bill.Id == billId
                             select userBill).ToList();

                //Filter out dupes
                filteredList = bills.GroupBy(x => x.Id)
                               .Select(grp => grp.First())
                               .ToList();
            }

            return(filteredList.FirstOrDefault());
        }
        /// <summary>
        /// Gets the user bill set model by user Id
        /// </summary>
        /// <param name="userId">The user id</param>
        /// <returns></returns>
        public UserBillSet GetBillsByUserIdConsolidated(int userId)
        {
            UserBillSet ubs;

            using (var ctx = new MyBillsContext())
            {
                var userBillDetails = (from ub in ctx.UserBills
                                       join bill in ctx.Bills on ub.Bill equals bill
                                       join ubrs in ctx.UserBillRecurrenceSchedule on ub.RecurrenceSchedule equals ubrs
                                       join rt in ctx.RecurrenceType on ubrs.RecurrenceType equals rt
                                       where ub.User.Id == userId
                                       select new UserBillDetail
                {
                    UserId = userId,
                    Bill = bill,
                    BillId = bill.Id,
                    BillName = bill.Name,
                    Amount = bill.Amount,
                    Month = ub.Month,
                    Year = ub.Year,
                    IsComplete = bill.IsComplete,
                    IsAutoPaid = bill.IsAutoPaid,
                    RecurrenceTypeName = rt.Name,
                    RecurrenceTypeId = ubrs.RecurrenceTypeId,
                    Schedule = ubrs.Schedule
                }
                                       ).ToList();

                var filteredList = userBillDetails.GroupBy(x => x.BillId)
                                   .Select(grp => grp.First())
                                   .ToList();

                ubs = new UserBillSet()
                {
                    UserId      = userId,
                    BillDetails = filteredList
                };
            }

            return(ubs);
        }
Beispiel #19
0
        /// <summary>
        /// Creates a new user
        /// </summary>
        /// <param name="newUser">The <see cref="User"></see></param>
        #region Private Methods
        private void CreateUser(User newUser)
        {
            try
            {
                string newPass;
                if (newUser.PasswordHash.Trim() == string.Empty)
                {
                    var randomWordPass = GenerateRandomPassword();
                    newPass = Authentication.Compute(randomWordPass);
                }
                else
                {
                    newPass = newUser.PasswordHash;
                }

                var user = new User
                {
                    Username     = newUser.Email,
                    Email        = newUser.Email,
                    PasswordHash = newPass,
                    CreatedDate  = DateTime.Now,
                    UpdatedDate  = DateTime.Now,
                };

                using (var ctx = new MyBillsContext())
                {
                    ctx.Entry(newUser).State = EntityState.Unchanged;
                    ctx.Users.Add(user);
                    ctx.SaveChanges();

                    newUser.Id = user.Id;
                }

                _logRepository.WriteLog(LogLevel.Debug, "UserRepository.CreateUser", $"New User Created - {user.Email}");
            }
            catch (Exception ex)
            {
                _logRepository.WriteLog(LogLevel.Error, "UserRepository.CreateUser", ex.Message, ex);
            }
        }
Beispiel #20
0
        /// <summary>
        /// Write a new log to the database
        /// </summary>
        /// <param name="level">The log level</param>
        /// <param name="method">The source method</param>
        /// <param name="msg">The log message</param>
        /// <param name="ex">The exception to log</param>
        /// <param name="userName">The username that created the message to log</param>
        public void WriteLog(LogLevel level, string method, string msg, Exception ex = null, string userName = null)
        {
            string stackTrace = null;

            if (ex != null)
            {
                stackTrace = ex.ToString();
            }

            using var ctx = new MyBillsContext();
            ctx.Log.Add(new Log
            {
                LogLevel      = level,
                TimeStamp     = DateTime.Now,
                CurrentMethod = method,
                ErrorMessage  = msg,
                StackTrace    = stackTrace,
                UserName      = userName
            });

            ctx.SaveChanges();
        }
        /// <summary>
        /// Get the recurrence schedule
        /// </summary>
        /// <param name="recTypeId">The recurrence type id</param>
        /// <param name="recModel">The recurrence model</param>
        /// <returns></returns>
        public RecurrenceSchedule GetRecSchedule(int recTypeId, IRecurrenceModel recModel)
        {
            var recModelFormat = recModel.Format;

            RecurrenceSchedule recurrenceSchedule;

            using (var ctx = new MyBillsContext())
            {
                recurrenceSchedule = (from ubrs in ctx.UserBillRecurrenceSchedule
                                      where ubrs.RecurrenceTypeId == recTypeId && ubrs.Schedule == recModelFormat
                                      select ubrs).FirstOrDefault();
            }

            return(recurrenceSchedule ?? (new RecurrenceSchedule
            {
                RecurrenceType = new RecurrenceType {
                    Id = 0, Name = recModel.Name, Type = recModel.Name
                },
                RecurrenceTypeId = recTypeId,
                Schedule = recModel.Format,
            }));
        }
        /// <summary>
        /// Generates recurring bills for the month and year
        /// </summary>
        /// <param name="userId">The user id</param>
        /// <param name="month">The month</param>
        /// <param name="year">The year</param>
        /// <returns></returns>
        public List <UserBill> GenerateRecurringBills(int userId, int month, int year)
        {
            List <UserBillDetail> userBillDetails;

            using (var ctx = new MyBillsContext())
            {
                //Get bills by user
                userBillDetails = (from ub in ctx.UserBills
                                   join bill in ctx.Bills on ub.Bill equals bill
                                   join ubrs in ctx.UserBillRecurrenceSchedule on ub.RecurrenceSchedule equals ubrs
                                   join rt in ctx.RecurrenceType on ubrs.RecurrenceType equals rt
                                   where ub.User.Id == userId && !bill.IsComplete
                                   select new UserBillDetail
                {
                    UserId = userId,
                    BillId = bill.Id,
                    Month = month,
                    Year = year,
                    RecurrenceTypeName = rt.Name,
                    RecurrenceTypeId = ubrs.RecurrenceTypeId,
                    Schedule = ubrs.Schedule,
                    RecurrenceScheduleId = ub.RecurrenceScheduleId
                }).Distinct().ToList();
            }

            //Add new user bill with user and bill for the month and year
            foreach (var billDetail in userBillDetails)
            {
                var recModel = GetRecurrenceModel(billDetail.RecurrenceTypeName, billDetail.Schedule);

                CreateNewUserBill(billDetail, recModel);
            }

            var userBills = GetBillsByUserIdAndMonthYear(userId, month, year);

            return(userBills);
        }
Beispiel #23
0
 /// <summary>
 /// Updates an existing bill
 /// </summary>
 /// <param name="bill">The bill to update</param>
 public void UpdateBill(Bill bill)
 {
     using var ctx         = new MyBillsContext();
     ctx.Entry(bill).State = EntityState.Modified;
     ctx.SaveChanges();
 }