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()); } }
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); } }
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 }); }
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); } }
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); } }
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 }); }
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()); }
private void SeedDbContext() { using (ShowNTellDbContext context = GetDbContext()) { Seed(context); context.SaveChanges(); } }
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); } }
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()); }
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)); } }
public async Task <Comment> Create(Comment comment) { using (ShowNTellDbContext context = _contextFactory.CreateDbContext()) { context.Comments.Add(comment); await context.SaveChangesAsync(); return(comment); } }
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); }
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); }
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()); } }
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); }
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()); } }
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); }
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)); } }
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); } }
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()); } }
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); }
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(); } }
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); } }
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); }
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); } }
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); } }
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); } }
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); } }