Exemple #1
0
        /// <summary>
        /// convert to user entity then save changes.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <bool> UpdateUserAsync(int id, User user)
        {
            // make sure the user can be converted
            var userEntity = DbEntityConverter.ToUserEntity(user);

            var entities = _context.UserEntities;

            if (id < 1 || !entities.Any() || id > entities.Max(u => u.Id))
            {
                throw new ArgumentException($"{id} is not a valid id.");
            }

            UserEntity entity = await entities.FindAsync(id);

            // assign all the values
            {
                entity.Status    = userEntity.Status;
                entity.FirstName = userEntity.FirstName;
                if (!userEntity.Email.IsNullOrEmpty())
                {
                    entity.Email = userEntity.Email;
                }
                entity.LastName = userEntity.LastName;
            }
            // save changes.
            _context.SaveChanges();
            return(true);
        }
        public async Task <bool> UpdateAsync(Comment comment)
        {
            try {
                comment.NullCheck(nameof(comment));

                var target = await _context.CommentEntities.FindAsync(comment.Id);

                if (target is null)
                {
                    return(false);
                }

                var entity = DbEntityConverter.ToCommentEntity(comment);

                target.Content   = entity.Content;
                target.CreatedAt = entity.CreatedAt;
                target.PostId    = entity.PostId;
                target.UserId    = entity.UserId;
                target.ParentId  = entity.ParentId;

                await _context.SaveChangesAsync();
            } catch {
                return(false);
            }

            return(true);
        }
Exemple #3
0
        /// <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());
        }
Exemple #4
0
        /// <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);
        }
Exemple #5
0
        /// <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);
        }
Exemple #6
0
        /// <summary>
        /// try to create a user if it goes through return a true otherwise return a flase
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <int> CreateUser(User user)
        {
            try {
                var newUser = DbEntityConverter.ToUserEntity(user); // convert
                await _context.AddAsync(newUser);

                await _context.SaveChangesAsync();

                return(newUser.Id);
            } catch {
                return(-1);
            }
        }
Exemple #7
0
        /// <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);
        }
        public async Task <int> CreateAsync(Comment comment)
        {
            try {
                comment.NullCheck(nameof(comment));

                var entity = DbEntityConverter.ToCommentEntity(comment);
                await _context.CommentEntities.AddAsync(entity);

                await _context.SaveChangesAsync();

                return(entity.Id);
            } catch {
                return(-1);
            }
        }
Exemple #9
0
        public async Task <List <Post> > GetAllPostsAsync()
        {
            var entity = await _context.PostEntities
                         .Include(e => e.User)
                         .Include(e => e.Comments)
                         .ThenInclude(c => c.User)
                         .Include(e => e.Likes)
                         .ThenInclude(c => c.User)
                         .ToListAsync();

            var posts = entity
                        .Select(e => DbEntityConverter.ToPost(e))
                        .ToList();

            return(posts);
        }
Exemple #10
0
        public async Task <int> CreatePostAsync(Post post)
        {
            try
            {
                var newPost = DbEntityConverter.ToPostEntity(post);
                await _context.AddAsync(newPost);

                await _context.SaveChangesAsync();

                return(newPost.Id);
            }
            catch
            {
                return(-1);
            }
        }
        public async Task <IEnumerable <Comment> > GetCommentsByUserIdAsync(int userId)
        {
            var comments = _context.CommentEntities
                           .Include(c => c.Post)
                           .Include(c => c.User)
                           .Include(c => c.ChildrenComments)
                           .ThenInclude(c => c.Post)
                           .ThenInclude(c => c.User);

            if (await _context.UserEntities.FindAsync(userId) is null || !comments.Any())
            {
                return(new List <Comment>());
            }

            return(comments
                   .Where(c => c.UserId == userId)
                   .Select(c => DbEntityConverter.ToComment(c)));
        }
Exemple #12
0
        public async Task <Post> GetPostByIdAsync(int id)
        {
            var posts = await _context.PostEntities
                        .Include(p => p.User)
                        .Include(p => p.Comments)
                        .ThenInclude(c => c.User)
                        .Include(p => p.Likes)
                        .ToListAsync();

            if (!posts.Any())
            {
                return(null);
            }

            var post = posts.FirstOrDefault(p => p.Id == id);

            return(DbEntityConverter.ToPost(post));
        }
        public async Task <IEnumerable <Comment> > GetCommentsByIdsAsync(ICollection <int> ids)
        {
            var comments = await _context.CommentEntities
                           .Include(c => c.Post)
                           .Include(c => c.User)
                           .Include(c => c.ChildrenComments)
                           .ThenInclude(c => c.Post)
                           .ThenInclude(c => c.User)
                           .ToListAsync();

            if (!ids.Any() || !comments.Any())
            {
                return(new List <Comment>());
            }

            return(comments
                   .Where(c => ids.Contains(c.Id))
                   .Select(c => DbEntityConverter.ToComment(c)));
        }
        public async Task <Comment> GetCommentByIdAsync(int userId)
        {
            var comments = await _context.CommentEntities
                           .Include(c => c.Post)
                           .Include(c => c.User)
                           .Include(c => c.ChildrenComments)
                           .ThenInclude(c => c.Post)
                           .ThenInclude(c => c.User)
                           .ToListAsync();

            var item = comments.FirstOrDefault(c => c.Id == userId);

            if (!comments.Any())
            {
                return(null);
            }

            return(DbEntityConverter.ToComment(item));
        }
        public async Task <IEnumerable <Comment> > GetAllAsync()
        {
            var comments = await _context.CommentEntities
                           .Include(c => c.Post)
                           .Include(c => c.User)
                           .Include(c => c.ChildrenComments)
                           .ThenInclude(c => c.Post)
                           .ThenInclude(c => c.User)
                           .ToListAsync();

            if (!comments.Any())
            {
                return(new List <Comment>());
            }

            return(comments
                   .Select(c => DbEntityConverter.ToComment(c))
                   .ToList());
        }
Exemple #16
0
        public async Task <List <Post> > GetPostsByUserIdAsync(int id)
        {
            var entity = await _context.PostEntities
                         .Where(e => e.UserId == id)
                         .Include(e => e.User)
                         .Include(e => e.Comments)
                         .ThenInclude(c => c.User)
                         .Include(e => e.Likes)
                         .ThenInclude(c => c.User)
                         .ToListAsync();

            try {
                var posts = entity.Select(e => DbEntityConverter.ToPost(e)).Reverse().ToList();
                return(posts);
            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
Exemple #17
0
        /// <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);
        }
Exemple #18
0
        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);
        }