Example #1
0
        public async Task <Result <CommentModel, Error> > AddComment(Guid activityId, NewCommentModel commentModel)
        {
            commentModel.UserId = Guid.Parse(_accessor.HttpContext.User.Claims.First(c => c.Type == "userId").Value);
            var activity = await _activitiesRepository.GetById(activityId);

            if (activity == null)
            {
                return(Result.Failure <CommentModel, Error>(ErrorsList.UnavailableActivity));
            }

            var comment = _mapper.Map <Comment>(commentModel);

            activity.AddComment(comment);

            var user = await _userRepository.GetById(commentModel.UserId);

            var notification = new Notification(activityId,
                                                DateTime.Now,
                                                $" {user.Username} has added a comment to activity {activity.Name} : {comment.Content}."
                                                );

            activity.AddNotification(notification);

            _activitiesRepository.Update(activity);
            await _activitiesRepository.SaveChanges();

            return(Result.Success <CommentModel, Error>(CommentModel.Create(comment.Id, comment.ActivityId,
                                                                            comment.UserId, user.Username, comment.Content)));
        }
Example #2
0
        public async Task <Result <IList <CommentModel>, Error> > GetComments(Guid activityId)
        {
            var activityExists = (await _activitiesRepository.GetById(activityId)) != null;

            if (!activityExists)
            {
                return(Result.Failure <IList <CommentModel>, Error>(ErrorsList.UnavailableActivity));
            }


            var comments = (await _activitiesRepository.GetByIdWithComments(activityId)).Comments;

            var commentsModel = new List <CommentModel>();

            foreach (var comment in comments)
            {
                var user = await _userRepository.GetById(comment.UserId);

                commentsModel.Add(CommentModel.Create(comment.Id, comment.ActivityId,
                                                      comment.UserId, user.Username, comment.Content));
            }

            return(Result.Success <IList <CommentModel>, Error>(commentsModel));
        }