예제 #1
0
        public IHttpActionResult CommentExists(int userId, int postId)
        {
            var exists = _commentRepository.CommentExists(userId, postId);
            CommentForReturnDTO model = new CommentForReturnDTO();

            model.Exists = exists;

            return(Ok(model));
        }
예제 #2
0
        public async Task <IActionResult> Submit(CommentForPostDto commentForPostDto)
        {
            if (await _repo.CommentExists(commentForPostDto.Producer, DateTime.Now))
            {
                return(BadRequest("Comment already exists for this day"));
            }

            _user = _httpContextAccessor.HttpContext.User.Identity.Name;

            var commentToPost = new Comment
            {
                Producer   = commentForPostDto.Producer,
                Content    = commentForPostDto.Content,
                CreatedBy  = _user,
                Created    = DateTime.Now.Date,
                LastEdited = DateTime.Now.Date,
                EditedBy   = _user
            };

            var postedComment = await _repo.Submit(commentToPost);

            return(StatusCode(201));
        }
예제 #3
0
        public async Task <ActionResult <Employee> > Delete(int id)
        {
            if (!await _commentRepository.CommentExists(id))
            {
                ModelState.AddModelError("", "Employee does not exist!");
                return(StatusCode(404, ModelState));
            }

            var comment = await _commentRepository.GetById(id);

            if (!await _commentRepository.DeleteAsync(comment))
            {
                ModelState.AddModelError("", "Error encountered when deleting from database, try again. If problem persists contact your administrator");
                return(StatusCode(500, ModelState));
            }

            return(NoContent());
        }
예제 #4
0
        public IActionResult GetReplies(int commentId)
        {
            try
            {
                if (!_commentRepository.CommentExists(commentId))
                {
                    _logger.LogInformation($"Comment with id {commentId} was not found when accessing replies.");
                    return(NotFound());
                }

                var repliesForComment        = _commentRepository.GetRepliesForComment(commentId);
                var repliesForCommentResults =
                    Mapper.Map <IEnumerable <ReplyDto> >(repliesForComment);

                return(Ok(repliesForCommentResults));
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"Exception while getting replies for comment with id {commentId}.", ex);
                return(StatusCode(500, "A problem happened while handling your request."));
            }
        }
예제 #5
0
        public void Run(
            IBotSettings settings,
            ISetlistAgent setlistAgent,
            ICommentBuilder commentBuilder,
            ICommentRepository commentRepository)
        {
            // todo arg null checks

            TelemetryClient telemetry = new TelemetryClient();

            try
            {
                string          subredditName = string.Format("/r/{0}", settings.Subreddit);
                DateParser      dateParser    = new DateParser();
                List <ISetlist> setlists      = new List <ISetlist>();

                BotWebAgent webAgent = new BotWebAgent(settings.Username, settings.Password, settings.Key, settings.Secret, "http://127.0.0.1");
                Reddit      reddit   = new Reddit(webAgent, true);
                reddit.RateLimit = WebAgent.RateLimitMode.Pace;

                Subreddit subreddit = reddit.GetSubreddit(subredditName);

                IEnumerable <Comment> comments = subreddit.Comments.Take(settings.MaxComments);

                foreach (Comment comment in comments)
                {
                    if (!comment.Body.ToLower().Contains(settings.Username))
                    {
                        continue;
                    }

                    if (commentRepository.CommentExists(comment.Id))
                    {
                        continue;
                    }

                    setlists.Clear();
                    string reply = string.Empty;

                    List <DateTime> dates = dateParser.ParseDates(comment.Body);
                    foreach (DateTime date in dates)
                    {
                        setlists.AddRange(setlistAgent.GetSetlists(date.Year, date.Month, date.Day));
                    }

                    if (setlists.Count > 1)
                    {
                        reply = commentBuilder.Build(setlists);
                    }
                    else if (setlists.Count == 1)
                    {
                        reply = commentBuilder.Build(setlists.First());
                    }

                    if (reply.Length > 0)
                    {
                        comment.Reply(reply);
                        commentRepository.SaveComment(comment.Id, comment.Body, reply);
                    }
                }
            }
            catch (RateLimitException rle)
            {
                // cool it down for 3 minutes
                telemetry.TrackException(rle);
                Thread.Sleep(180 * 1000);
            }
            catch (Exception ex)
            {
                telemetry.TrackException(ex);
            }
        }
예제 #6
0
 private bool CommentExists(int id)
 {
     return(_commentRepository.CommentExists(id));
 }