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); }
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); } }
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); } }
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); }
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(); } }
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(); } }
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); } }
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); } }
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(); } }
public ItemRepository(BudgetAppContext context) { _context = context; }
public CategoryRepository(BudgetAppContext context) { _context = context; }