コード例 #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();
        }
コード例 #2
0
        /// <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();
        }
コード例 #3
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);
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
        /// <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();
        }
コード例 #6
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);
            }
        }
コード例 #7
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);
            }
        }
コード例 #8
0
ファイル: LogRepository.cs プロジェクト: jaypowley/MyBillsApp
        /// <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();
        }
コード例 #9
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();
 }