/// <summary> /// /// </summary> /// <param name="userName"></param> /// <param name="title"></param> /// <param name="comment"></param> /// <returns></returns> public async Task <bool> AddCommentAsync(string userName, string title, Comment comment) { try { Story story = await _storiesRepository.GetStoryByTitleAsync(userName, title); if (story == null) { return(false); } if (story.Comments.Count == 0) { comment.Id = story.Comments.Count; } else { comment.Id = story.Comments[story.Comments.Count - 1].Id + 1; // last id + 1 } comment.Date = DateTime.Now; story.Comments.Add(comment); return(await _storiesRepository.UpdateStoryAsync(userName, title, story)); } catch (Exception e) { _logger.LogError($"Error while adding comment\n {e.Message}"); } return(false); }
/// <summary> /// updates story or comment depends on {postType} /// </summary> /// <param name="postType">story or comment</param> /// <param name="userName">author of the story</param> /// <param name="title">title of the story</param> /// <param name="post">changed story or comment</param> /// <returns>if the post was updated</returns> private async Task <bool> UpdatePostWithGivenType(string postType, string userName, string title, IPostable post) { try { switch (postType) { case STORY: return(await _storiesRepository.UpdateStoryAsync(userName, title, (Story)post)); case COMMENT: return(await _commentsRepository.UpdateCommentAsync(userName, title, (Comment)post)); } } catch (Exception e) { _logger.LogError($"Error while updating post with given type\n {e.Message}"); } return(false); }