Ejemplo n.º 1
0
 public async Task <IEnumerable <ImagePost> > GetFeed(string userEmail)
 {
     using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
     {
         return(await context.Users
                .Include(u => u.Following)
                .ThenInclude(f => f.User)
                .ThenInclude(u => u.ImagePosts)
                .ThenInclude(p => p.Likes)
                .ThenInclude(c => c.User)
                .Include(u => u.Following)
                .ThenInclude(f => f.User)
                .ThenInclude(u => u.ImagePosts)
                .ThenInclude(p => p.Comments)
                .ThenInclude(c => c.User)
                .Include(u => u.Following)
                .ThenInclude(f => f.User)
                .ThenInclude(u => u.ImagePosts)
                .ThenInclude(p => p.User)
                .Where(u => u.Email == userEmail)
                .SelectMany(u => u.Following.SelectMany(f => f.User.ImagePosts))
                .OrderByDescending(p => p.DateCreated)
                .ToListAsync());
     }
 }
Ejemplo n.º 2
0
        public async Task <bool> UnfollowUser(string userUsername, string followerEmail)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                bool success = false;

                try
                {
                    User user = await context.Users.FirstOrDefaultAsync(u => u.Username == userUsername);

                    if (user != null)
                    {
                        Follow existingFollow = new Follow()
                        {
                            UserEmail     = user.Email,
                            FollowerEmail = followerEmail
                        };

                        context.Entry(existingFollow).State = EntityState.Deleted;
                        await context.SaveChangesAsync();

                        success = true;
                    }
                }
                catch (Exception)
                {
                    success = false;
                }

                return(success);
            }
        }
Ejemplo n.º 3
0
        protected override void Seed(ShowNTellDbContext context)
        {
            context.Users.Add(new User()
            {
                Email    = EXISTING_USER_EMAIL,
                Username = EXISTING_USER_USERNAME
            });

            for (int i = 0; i < EXISTING_USER_IMAGE_POST_COUNT; i++)
            {
                context.ImagePosts.Add(new ImagePost()
                {
                    UserEmail = EXISTING_USER_EMAIL
                });
            }

            // Add an alternate user image post.
            context.Users.Add(new User()
            {
                Email = ALTERNATE_USER_EMAIL
            });

            context.ImagePosts.Add(new ImagePost()
            {
                UserEmail = ALTERNATE_USER_EMAIL
            });
        }
Ejemplo n.º 4
0
        public async Task <bool> UnlikeImagePost(int imagePostId, string userEmail)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                bool success = false;

                try
                {
                    Like like = new Like()
                    {
                        ImagePostId = imagePostId,
                        UserEmail   = userEmail
                    };

                    context.Entry(like).State = EntityState.Deleted;
                    await context.SaveChangesAsync();

                    success = true;
                }
                catch (Exception)
                {
                    success = false;
                }

                return(success);
            }
        }
Ejemplo n.º 5
0
        public async Task <bool> Delete(int commentId)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                bool success = false;

                Comment comment = new Comment()
                {
                    Id = commentId
                };

                try
                {
                    context.Remove(comment);

                    await context.SaveChangesAsync();

                    success = true;
                }
                catch (System.Exception)
                {
                    success = false;
                }

                return(success);
            }
        }
Ejemplo n.º 6
0
        protected override void Seed(ShowNTellDbContext context)
        {
            context.Users.Add(new User
            {
                Email    = EXISTING_USER_EMAIL_1,
                Username = EXISTING_USER_USERNAME_1
            });

            context.Users.Add(new User
            {
                Email    = EXISTING_USER_EMAIL_2,
                Username = EXISTING_USER_USERNAME_2
            });

            context.Users.Add(new User
            {
                Email    = EXISTING_FOLLOW_USER_EMAIL,
                Username = EXISTING_FOLLOW_USER_USERNAME
            });

            context.Users.Add(new User
            {
                Email    = EXISTING_FOLLOW_FOLLOWER_EMAIL,
                Username = EXISTING_FOLLOW_FOLLOWER_USERNAME
            });

            context.Follows.Add(new Follow()
            {
                UserEmail     = EXISTING_FOLLOW_USER_EMAIL,
                FollowerEmail = EXISTING_FOLLOW_FOLLOWER_EMAIL
            });
        }
Ejemplo n.º 7
0
        protected override void Seed(ShowNTellDbContext context)
        {
            context.ImagePosts.Add(new ImagePost());
            context.ImagePosts.Add(new ImagePost());
            context.ImagePosts.Add(new ImagePost());

            context.Users.Add(GetExistingUser());
        }
Ejemplo n.º 8
0
 private void SeedDbContext()
 {
     using (ShowNTellDbContext context = GetDbContext())
     {
         Seed(context);
         context.SaveChanges();
     }
 }
Ejemplo n.º 9
0
        public async Task <bool> IsAuthor(int id, string email)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                ImagePost storedImagePost = await context.ImagePosts.FindAsync(id);

                return(storedImagePost != null && storedImagePost.UserEmail == email);
            }
        }
Ejemplo n.º 10
0
 protected override void Seed(ShowNTellDbContext context)
 {
     context.Users.Add(GetValidUser());
     context.Users.Add(GetInvalidUser());
     context.Tags.Add(new Tag()
     {
         Content = _existingTagContent
     });
     context.ImagePosts.AddRange(GetImagePosts());
 }
Ejemplo n.º 11
0
 public async Task <User> GetByEmail(string email)
 {
     using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
     {
         return(await context.Users
                .Include(u => u.Following)
                .ThenInclude(f => f.User)
                .FirstOrDefaultAsync(u => u.Email == email));
     }
 }
Ejemplo n.º 12
0
        public async Task <Comment> Create(Comment comment)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                context.Comments.Add(comment);
                await context.SaveChangesAsync();

                return(comment);
            }
        }
Ejemplo n.º 13
0
        public async Task Delete_WithExistingComment_ReturnsTrue()
        {
            Comment            comment = new Comment();
            ShowNTellDbContext context = GetDbContext();

            context.Add(comment);
            context.SaveChanges();

            bool actual = await _commentService.Delete(comment.Id);

            Assert.IsTrue(actual);
        }
Ejemplo n.º 14
0
        public async Task GetRandom_WithExistingImagePosts_ReturnsImagePost()
        {
            using (ShowNTellDbContext context = GetDbContext())
            {
                context.ImagePosts.Add(new ImagePost());
                context.SaveChanges();
            }

            ImagePost actual = await _randomImagePostService.GetRandom();

            Assert.IsNotNull(actual);
        }
Ejemplo n.º 15
0
 public async Task <IEnumerable <ImagePost> > GetImagePosts(string username)
 {
     using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
     {
         return(await context.ImagePosts
                .Include(p => p.User)
                .Include(p => p.Likes)
                .Include(p => p.Comments)
                .ThenInclude(c => c.User)
                .Where(p => p.User.Username == username)
                .ToListAsync());
     }
 }
Ejemplo n.º 16
0
        public async Task IsCommentOwner_WithExistingCommentNotOwnedByUser_ReturnsFalse()
        {
            string             userEmail = "*****@*****.**";
            Comment            comment   = new Comment();
            ShowNTellDbContext context   = GetDbContext();

            context.Add(comment);
            context.SaveChanges();

            bool actual = await _commentService.IsCommentOwner(comment.Id, userEmail);

            Assert.IsFalse(actual);
        }
Ejemplo n.º 17
0
 public async Task <ImagePost> GetById(int id)
 {
     using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
     {
         return(await context.ImagePosts
                .Include(p => p.User)
                .Include(p => p.Likes)
                .Include(p => p.Comments)
                .ThenInclude(c => c.User)
                .Include(p => p.Tags)
                .ThenInclude(t => t.Tag)
                .FirstOrDefaultAsync(p => p.Id == id));
     }
 }
 public async Task <ImagePost> GetRandom()
 {
     using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
     {
         return(await context.ImagePosts
                .Include(p => p.User)
                .Include(p => p.Likes)
                .Include(p => p.Comments)
                .ThenInclude(c => c.User)
                .Include(p => p.Tags)
                .ThenInclude(t => t.Tag)
                .OrderBy(p => Guid.NewGuid())
                .FirstOrDefaultAsync());
     }
 }
Ejemplo n.º 19
0
        public async Task Update_WithExistingComment_ReturnsCommentWithNewContent()
        {
            Comment            comment = new Comment();
            ShowNTellDbContext context = GetDbContext();

            context.Add(comment);
            context.SaveChanges();
            string expectedContent = "new content";

            Comment actualComment = await _commentService.Update(comment.Id, expectedContent);

            string actualContent = actualComment.Content;

            Assert.AreEqual(expectedContent, actualContent);
        }
Ejemplo n.º 20
0
 public async Task <User> GetProfile(string username)
 {
     using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
     {
         return(await context.Users
                .Include(u => u.ImagePosts)
                .ThenInclude(p => p.Likes)
                .Include(u => u.ImagePosts)
                .ThenInclude(p => p.Comments)
                .Include(u => u.Followers)
                .ThenInclude(f => f.Follower)
                .Include(u => u.Following)
                .ThenInclude(f => f.User)
                .FirstOrDefaultAsync(u => u.Username == username));
     }
 }
Ejemplo n.º 21
0
        public async Task <User> Create(User user)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                try
                {
                    context.Users.Add(user);
                    await context.SaveChangesAsync();
                }
                catch (Exception ex)
                {
                    throw new ArgumentException(ex.Message, "email", ex);
                }

                return(user);
            }
        }
Ejemplo n.º 22
0
 public async Task <IEnumerable <ImagePost> > SearchImagePosts(string query)
 {
     using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
     {
         return(await context.ImagePosts
                .Include(p => p.User)
                .Include(p => p.Likes)
                .Include(p => p.Comments)
                .ThenInclude(c => c.User)
                .Include(p => p.Tags)
                .ThenInclude(t => t.Tag)
                .Where(p => p.UserEmail.Contains(query) ||
                       p.Description.Contains(query) ||
                       p.Tags.Select(t => t.Tag.Content).Any(t => t.Contains(query)))
                .ToListAsync());
     }
 }
Ejemplo n.º 23
0
        public async Task GetByEmail_WithExistingEmail_ReturnsUserWithEmail()
        {
            string             expectedEmail = "*****@*****.**";
            ShowNTellDbContext context       = _contextFactory.CreateDbContext();

            context.Add(new User()
            {
                Email = expectedEmail
            });
            context.SaveChanges();

            User user = await _userService.GetByEmail(expectedEmail);

            string actualEmail = user.Email;

            Assert.AreEqual(expectedEmail, actualEmail);
        }
Ejemplo n.º 24
0
        public void Seed()
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                context.Database.Migrate();

                if (context.Users.Count() == 0)
                {
                    context.Users.Add(new User()
                    {
                        Email    = "*****@*****.**",
                        Username = "******"
                    });
                }

                context.SaveChanges();
            }
        }
Ejemplo n.º 25
0
        public async Task <Comment> Update(int id, string content)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                Comment comment = await context.Comments.FirstOrDefaultAsync(c => c.Id == id);

                if (comment == null)
                {
                    throw new EntityNotFoundException <int>(id, typeof(Comment));
                }

                comment.Content = content;

                context.Update(comment);
                await context.SaveChangesAsync();

                return(comment);
            }
        }
Ejemplo n.º 26
0
        public async Task CanDelete_WithExistingCommentOwnedByUser_ReturnsTrue()
        {
            string  userEmail = "*****@*****.**";
            Comment comment   = new Comment()
            {
                User = new User {
                    Email = userEmail
                },
                ImagePost = new ImagePost()
            };
            ShowNTellDbContext context = GetDbContext();

            context.Add(comment);
            context.SaveChanges();

            bool actual = await _commentService.CanDelete(comment.Id, userEmail);

            Assert.IsTrue(actual);
        }
Ejemplo n.º 27
0
        public async Task <Like> LikeImagePost(int imagePostId, string userEmail)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                ImagePost existingImagePost = await context.ImagePosts
                                              .Include(p => p.Likes)
                                              .FirstOrDefaultAsync(p => p.Id == imagePostId);

                if (existingImagePost == null)
                {
                    throw new EntityNotFoundException <int>(imagePostId);
                }

                User existingUser = await context.Users.FindAsync(userEmail);

                if (existingUser == null)
                {
                    throw new EntityNotFoundException <string>(userEmail);
                }

                if (existingImagePost.UserEmail == userEmail)
                {
                    throw new OwnImagePostLikeException(existingImagePost, userEmail);
                }

                if (existingImagePost.Likes.Any(l => l.UserEmail == userEmail))
                {
                    throw new DuplicateLikeException(existingImagePost, userEmail);
                }

                Like like = new Like()
                {
                    ImagePostId = imagePostId,
                    UserEmail   = userEmail,
                    DateCreated = DateTime.Now
                };

                context.Likes.Add(like);
                await context.SaveChangesAsync();

                return(like);
            }
        }
Ejemplo n.º 28
0
        public async Task <bool> Delete(int id)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                bool success = false;

                ImagePost storedImagePost = await context.ImagePosts.FindAsync(id);

                if (storedImagePost != null)
                {
                    context.ImagePosts.Remove(storedImagePost);
                    await context.SaveChangesAsync();

                    success = true;
                }

                return(success);
            }
        }
Ejemplo n.º 29
0
        public async Task <ImagePost> Update(int id, string description, IEnumerable <Tag> tags)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                ImagePost storedImagePost = await context.ImagePosts
                                            .Include(p => p.Tags)
                                            .ThenInclude(t => t.Tag)
                                            .FirstOrDefaultAsync(p => p.Id == id);

                if (storedImagePost == null)
                {
                    throw new EntityNotFoundException <int>(id);
                }

                // Update the description.
                storedImagePost.Description = description;

                // Update the tags.
                ICollection <ImagePostTag> mergedTags = null;
                if (tags != null)
                {
                    // Remove all old tags.
                    context.Set <ImagePostTag>().RemoveRange(storedImagePost.Tags);

                    // Set the new tags.
                    mergedTags = await GetMergedNewAndExistingTagsFromContext(tags, context);

                    storedImagePost.Tags = ConvertImagePostTagsForSave(mergedTags);
                }

                context.Update(storedImagePost);
                await context.SaveChangesAsync();

                // Set the tags to the fully populated tags.
                if (mergedTags != null)
                {
                    storedImagePost.Tags = mergedTags;
                }

                return(storedImagePost);
            }
        }
Ejemplo n.º 30
0
        public async Task <bool> IsCommentOwner(int commentId, string userEmail)
        {
            using (ShowNTellDbContext context = _contextFactory.CreateDbContext())
            {
                bool success = false;

                try
                {
                    Comment comment = await context.Comments.FirstOrDefaultAsync(c => c.Id == commentId);

                    success = comment.UserEmail == userEmail;
                }
                catch (System.Exception)
                {
                    success = false;
                }

                return(success);
            }
        }