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); } }
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> 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); } }
public async Task <Comment> Create(Comment comment) { using (ShowNTellDbContext context = _contextFactory.CreateDbContext()) { context.Comments.Add(comment); await context.SaveChangesAsync(); return(comment); } }
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 <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 <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> DeleteByUri(string uri) { using (ShowNTellDbContext context = _contextFactory.CreateDbContext()) { bool success = false; ImagePost storedImagePost = await context.ImagePosts .FirstOrDefaultAsync(p => p.ImageUri == uri); if (storedImagePost != null) { context.ImagePosts.Remove(storedImagePost); await context.SaveChangesAsync(); success = true; } return(success); } }
public async Task <ImagePost> Create(ImagePost imagePost) { using (ShowNTellDbContext context = _contextFactory.CreateDbContext()) { ICollection <ImagePostTag> mergedTags = null; if (imagePost.Tags != null && imagePost.Tags.Count > 0) { mergedTags = await GetMergedNewAndExistingTagsFromContext(imagePost.Tags, context); imagePost.Tags = ConvertImagePostTagsForSave(mergedTags); } context.ImagePosts.Add(imagePost); await context.SaveChangesAsync(); imagePost.Tags = mergedTags; return(imagePost); } }
public async Task <Follow> FollowUser(string userUsername, string followerEmail) { using (ShowNTellDbContext context = _contextFactory.CreateDbContext()) { User user = await context.Users.FirstOrDefaultAsync(u => u.Username == userUsername); if (user == null) { throw new EntityNotFoundException <string>(userUsername, typeof(User)); } if (user.Email == followerEmail) { throw new OwnProfileFollowException(followerEmail); } Follow newFollow = new Follow() { UserEmail = user.Email, FollowerEmail = followerEmail }; try { context.Follows.Add(newFollow); await context.SaveChangesAsync(); } catch (Exception) { throw new InvalidOperationException(); } newFollow.User = user; return(newFollow); } }