Exemple #1
0
        public async Task DeleteUserAccount(long userId)
        {
            using (var context = new BudgetAppContext())
            {
                var user = await context.UserAccount.FirstOrDefaultAsync(u => u.Id == userId);

                if (user == null)
                {
                    throw new Exception("User not found");
                }

                context.UserAccount.Remove(user);

                await context.SaveChangesAsync();
            }
        }
Exemple #2
0
        public async Task UpdateUserPassword(long userId, byte[] password)
        {
            using (var context = new BudgetAppContext())
            {
                var dbUser = await context.UserAccount.FirstOrDefaultAsync(u => u.Id == userId);

                if (dbUser == null)
                {
                    throw new Exception("User not found");
                }

                dbUser.Password = password;
                dbUser.Updated  = DateTime.Now;
                await context.SaveChangesAsync();
            }
        }
Exemple #3
0
        public async Task <long> CreateNewUserAccount(DbUser user)
        {
            using (var context = new BudgetAppContext())
            {
                if (user == null)
                {
                    throw new Exception("User not provided");
                }

                context.UserAccount.Add(user);

                await context.SaveChangesAsync();

                return(user.Id);
            }
        }
Exemple #4
0
        public async Task <long> UpdateStatus(long userId, bool status)
        {
            using (var context = new BudgetAppContext())
            {
                var dbUser = await context.UserAccount.FirstOrDefaultAsync(u => u.Id == userId);

                if (dbUser == null)
                {
                    throw new Exception("User not found");
                }

                dbUser.Status  = status;
                dbUser.Updated = DateTime.Now;
                await context.SaveChangesAsync();

                return(dbUser.Id);
            }
        }
Exemple #5
0
        public async Task UpdateName(long userId, string firstName, string lastName)
        {
            using (var context = new BudgetAppContext())
            {
                var dbUser = await context.UserAccount.FirstOrDefaultAsync(u => u.Id == userId);

                if (dbUser == null)
                {
                    throw new Exception("User not found");
                }

                dbUser.FirstName = firstName;
                dbUser.LastName  = lastName;

                dbUser.Updated = DateTime.Now;
                await context.SaveChangesAsync();
            }
        }
        public async Task UpsertGoals(List <Goal> goals)
        {
            using (var context = new BudgetAppContext())
            {
                var goalComparer = new GoalComparer();
                var dbGoals      = await context.Goals.Where(g => g.UserId == goals[0].UserId).ToListAsync();

                var goalsToAdd    = goals.Except(dbGoals, goalComparer).ToList();
                var goalsToUpdate = goals.Intersect(dbGoals, goalComparer).ToList();
                var goalsToDelete = dbGoals.Except(goals, goalComparer).ToList();

                foreach (var goal in goalsToAdd)
                {
                    goal.CreatedDate = DateTime.Now;
                    goal.UpdatedDate = DateTime.Now;
                    context.Add(goal);
                }

                foreach (var goal in goalsToUpdate)
                {
                    var dbGoal = await context.Goals.FirstOrDefaultAsync(g => g.Id == goal.Id);

                    dbGoal.UserId       = goal.UserId;
                    dbGoal.GoalName     = goal.GoalName;
                    dbGoal.GoalSummary  = goal.GoalSummary;
                    dbGoal.StartDate    = goal.StartDate;
                    dbGoal.EndDate      = goal.EndDate;
                    dbGoal.Amount       = goal.Amount;
                    dbGoal.TargetAmount = goal.TargetAmount;
                    dbGoal.UpdatedDate  = DateTime.Now;
                }

                context.Goals.RemoveRange(goalsToDelete);

                await context.SaveChangesAsync();
            }
        }