Ejemplo n.º 1
0
        public async Task <List <Goal> > GetGoals(long userId)
        {
            List <Goal> dbGoals = new List <Goal>();

            using (var context = new BudgetAppContext())
            {
                dbGoals = await context.Goals.Where(g => g.UserId == userId)
                          .ToListAsync();
            }
            return(dbGoals);
        }
Ejemplo n.º 2
0
        public async Task <DbUser> GetUserByEmail(string email)
        {
            using (var context = new BudgetAppContext())
            {
                var dbUser = await context.UserAccount.FirstOrDefaultAsync(u => u.Email == email);

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

                return(dbUser);
            }
        }
Ejemplo n.º 3
0
        public async Task <DbUser> GetUserByUserId(long userId)
        {
            using (var context = new BudgetAppContext())
            {
                var dbUser = await context.UserAccount.FirstOrDefaultAsync(u => u.Id == userId);

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

                return(dbUser);
            }
        }
Ejemplo n.º 4
0
        public async Task <List <DbUser> > GetAllUsers()
        {
            List <DbUser> dbUsers = new List <DbUser>();

            using (var context = new BudgetAppContext())
            {
                dbUsers = await context.UserAccount.ToListAsync();

                if (dbUsers.Count < 1)
                {
                    throw new Exception("No Users found");
                }
            }

            return(dbUsers);
        }
Ejemplo n.º 5
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();
            }
        }
Ejemplo n.º 6
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();
            }
        }
Ejemplo n.º 7
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);
            }
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
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();
            }
        }
Ejemplo n.º 10
0
        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();
            }
        }
Ejemplo n.º 11
0
 public ItemRepository(BudgetAppContext context)
 {
     _context = context;
 }
Ejemplo n.º 12
0
 public CategoryRepository(BudgetAppContext context)
 {
     _context = context;
 }