public virtual IComment PostComment(string content, DateTime at, IUser byUser, string fromIPAddress) { Check.Argument.IsNotEmpty(content, "content"); Check.Argument.IsNotInFuture(at, "at"); Check.Argument.IsNotNull(byUser, "byUser"); Check.Argument.IsNotEmpty(fromIPAddress, "fromIPAddress"); StoryComment comment = new StoryComment { Id = Guid.NewGuid(), HtmlBody = content.Trim(), TextBody = content.StripHtml().Trim(), Story = this, User = (User)byUser, IPAddress = fromIPAddress, CreatedAt = at }; StoryComments.Add(comment); IoC.Resolve <ICommentRepository>().Add(comment); LastActivityAt = at; return(comment); }
public virtual void DeleteComment(IComment comment) { Check.Argument.IsNotNull(comment, "comment"); StoryComment storyComment = (StoryComment)comment; IoC.Resolve <ICommentRepository>().Remove(comment); StoryComments.Remove(storyComment); }
public void DeleteComment(IComment comment) { Check.Argument.IsNotNull(comment, "comment"); var storyComment = (StoryComment)comment; this.RemoveComment(comment); StoryComments.Remove(storyComment); }
public IComment PostComment(string content, DateTime at, IUser byUser, string fromIpAddress) { Check.Argument.IsNotEmpty(content, "content"); Check.Argument.IsNotInFuture(at, "at"); Check.Argument.IsNotNull(byUser, "byUser"); Check.Argument.IsNotEmpty(fromIpAddress, "fromIpAddress"); var comment = this.AddComment(content, at, byUser, fromIpAddress); StoryComments.Add((StoryComment)comment); LastActivityAt = at; return(comment); }
private async Task SaveStory(StoryComments storyComments, char fetcher, CancellationToken cancellationToken) { using (var db = new PikabuContext()) using (var transaction = await db.Database.BeginTransactionAsync(IsolationLevel.Serializable, cancellationToken)) { var scanTime = DateTime.UtcNow; var story = await db.Stories.SingleOrDefaultAsync(s => s.StoryId == storyComments.StoryId, cancellationToken); if (story == null) { story = new Story { StoryId = storyComments.StoryId, Rating = storyComments.Rating, Title = storyComments.StoryTitle, Author = storyComments.Author, DateTimeUtc = storyComments.Timestamp.UtcDateTime, Comments = new List <Comment>() }; await db.Stories.AddAsync(story, cancellationToken); } story.Rating = storyComments.Rating; story.Title = storyComments.StoryTitle; story.Author = storyComments.Author; story.LastScanUtc = scanTime; var storyCommentIds = storyComments.Comments.Select(c => c.CommentId).ToArray(); var existingComments = await db.Comments.Where(c => storyCommentIds.Contains(c.CommentId)).ToDictionaryAsync(c => c.CommentId, cancellationToken); var storyUserNames = new HashSet <string>(storyComments.Comments.Select(c => c.User)); var existingUsers = (await db.Users.Where(c => storyUserNames.Contains(c.UserName)).ToDictionaryAsync(u => u.UserName, cancellationToken)); var newComments = 0; foreach (var comment in storyComments.Comments) { if (!existingUsers.TryGetValue(comment.User, out var user)) { user = new User { UserName = comment.User, AvatarUrl = comment.UserAvatarUrl, Comments = new List <Comment>() }; await db.Users.AddAsync(user, cancellationToken); existingUsers[user.UserName] = user; } else { user.AvatarUrl = comment.UserAvatarUrl; } if (!existingComments.TryGetValue(comment.CommentId, out var c)) { var item = new Comment { CommentId = comment.CommentId, ParentId = comment.ParentId, DateTimeUtc = comment.Timestamp.UtcDateTime, Rating = comment.Rating, Story = story, UserName = comment.User, CommentContent = new CommentContent { BodyHtml = comment.Body } }; await db.Comments.AddAsync(item, cancellationToken); existingComments[item.CommentId] = item; } else { newComments++; c.Rating = comment.Rating; } } /*Console.Write($"{fetcher}{DateTime.UtcNow} ({storyComments.StoryId}) {storyComments.Rating?.ToString("+0;-#") ?? "?"} {storyComments.StoryTitle}"); * if (newComments > 0) { * var color = Console.ForegroundColor; * Console.ForegroundColor = ConsoleColor.Yellow; * Console.WriteLine($" +{newComments}"); * Console.ForegroundColor = color; * } * else * { * Console.WriteLine(); * }*/ await db.SaveChangesAsync(cancellationToken); transaction.Commit(); } }