/// <summary> /// Get a collection of users from a collection of their ids /// </summary> /// <param name="ids"></param> /// <returns></returns> public async Task <IEnumerable <User> > GetUsersByIdsAsync(IEnumerable <int> ids) { var userEntities = _context.UserEntities; var userIds = userEntities .Select(u => u.Id) .ToList(); if (!ids.All(id => userIds.Contains(id))) { throw new ArgumentException("Not all ids requested are present."); } var users = await userEntities .Include(u => u.Followees) .ThenInclude(u => u.Followee) .Include(u => u.Followers) .ThenInclude(u => u.Follower) .Include(u => u.Posts) .ToListAsync(); if (!ids.Any() || !users.Any()) { return(new List <User>()); } return(users .Where(u => ids.Contains(u.Id)) .Select(u => DbEntityConverter.ToUser(u)) .ToList()); }
/// <summary> /// get's users by a string that contains anything in this string /// </summary> /// <param name="name"></param> /// <returns></returns> public async Task <IEnumerable <User> > GetUserByName(string name) { // loser the string of both name and FirstName. also works for last name var entity = await _context.UserEntities.Where(n => n.FirstName.ToLower().Contains(name.ToLower()) || n.LastName.ToLower().Contains(name.ToLower())).ToListAsync(); var users = entity.Select(e => DbEntityConverter.ToUser(e)); // turn into a list. return(users); }
/// <summary> /// Get all users /// </summary> /// <returns></returns> public IEnumerable <User> GetAllUsers() { var entity = _context.UserEntities .Include(u => u.Followees) .ThenInclude(u => u.Followee) .Include(u => u.Followers) .ThenInclude(u => u.Follower) .Include(u => u.Posts) .ToList(); var users = entity.Select(e => DbEntityConverter.ToUser(e)); return(users); }
/// <summary> /// Get all users asyncronously /// </summary> /// <returns></returns> public async Task <IEnumerable <User> > GetAllUsersAsync() { var entities = await _context.UserEntities .Include(u => u.Followees) .ThenInclude(u => u.Followee) .Include(u => u.Followers) .ThenInclude(u => u.Follower) .Include(u => u.Posts) .ToListAsync(); var users = entities.Select(e => DbEntityConverter.ToUser(e)); // turn into a list. return(users); }
/// <summary> /// Get user by the id that they pass into the conroller /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <User> GetUserByIdAsync(int id) { var entities = _context.UserEntities; if (id < 1 || !entities.Any() || id > entities.Max(u => u.Id)) { throw new ArgumentException($"{id} is not a valid id."); } var entity = await entities .Where(u => u.Id == id) .Include(u => u.Followees) .ThenInclude(u => u.Followee) .Include(u => u.Followers) .ThenInclude(u => u.Follower) .FirstOrDefaultAsync(); var user = DbEntityConverter.ToUser(entity); // turn inato a user return(user); }
public async Task <User> GetUserByEmailAsync(string email) { var entities = _context.UserEntities; email.EnforceEmailCharacters(nameof(email)); if (!entities.Any()) { throw new ArgumentException($"{email} does not belong to any user"); } var entity = await entities .Where(e => e.Email == email) .Include(u => u.Followees) .ThenInclude(u => u.Followee) .Include(u => u.Followers) .ThenInclude(u => u.Follower) .Include(u => u.Posts) .FirstOrDefaultAsync(); var user = DbEntityConverter.ToUser(entity); // turn into a user return(user); }