private async Task FetchAndSendRequests(CommentsDbContext dbContext)
        {
            var requests = await dbContext.Comments.Where(cr => !cr.Fetched && !cr.Disabled).ToListAsync();

            if (requests.Any())
            {
                foreach (var request in requests)
                {
                    _logger.LogInformation($"Db Fetch Request ID: {request.ToString()}");
                    var host = new Uri(request.Url)?.Host;
                    _logger.LogInformation($"Host: {host}");
                    CommentDetails result = new CommentDetails();
                    switch (host)
                    {
                    case AppConstants.YoutubeHost:
                        _logger.LogInformation("Hello Youtube");
                        result = await _fetcherFactory(AppConstants.Youtube).FetchComments(request);

                        break;

                    case AppConstants.AmazonHost:
                        _logger.LogInformation("Hello Amazon");
                        result = await _fetcherFactory(AppConstants.Amazon).FetchComments(request);

                        break;

                    default:
                        _logger.LogInformation("We haven't implemented that yet");
                        break;
                    }

                    request.Fetched     = result?.Filename == null ? false : true;
                    request.Location    = result?.Filename;
                    request.NOC         = result?.NOC;
                    request.Name        = result?.Name;
                    request.UpdatedDate = DateTime.UtcNow;
                    request.Disabled    = result == null ? true : false;
                    dbContext.Attach(request);
                    dbContext.Entry(request).State = EntityState.Modified;
                    _logger.LogInformation($"Comment Download Successfull: {request.ToString()}");
                    await dbContext.SaveChangesAsync();
                }
            }
        }
Exemplo n.º 2
0
        public ActionResult Edit([Bind(Include = "CommentGuid,SpotGuid,Title,Text")] CommentEditViewModel commentEditViewModel)
        {
            if (ModelState.IsValid)
            {
                Comment comment = db.Comments.Find(commentEditViewModel.CommentGuid);
                comment.SpotGuid = commentEditViewModel.SpotGuid;
                comment.Title    = commentEditViewModel.Title;
                comment.Text     = commentEditViewModel.Text;

                comment.DateModified   = DateTime.Now;
                comment.UserModifiedID = Auxiliaries.GetUserId(User);

                db.Entry(comment).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.SpotGuid = new SelectList(db.Spots, "SpotGuid", "SpotName", commentEditViewModel.SpotGuid);
            return(View(commentEditViewModel));
        }
Exemplo n.º 3
0
        private async Task FetchAndSendEmail(CommentsDbContext dbContext)
        {
            using (var command = dbContext.Database.GetDbConnection().CreateCommand())
            {
                command.CommandText = "Select c.Location, c.Name,c.Disabled, cq.emailAddress, cq.Id from Comments" +
                                      " as c inner join CommentRequests as cq on c.id = cq.CommentId  " +
                                      "where (c.Fetched = 1 or c.Disabled = 1) and cq.emailed = 0";
                await command.Connection.OpenAsync();

                DbDataReader reader = await command.ExecuteReaderAsync();

                if (reader.HasRows)
                {
                    while (await reader.ReadAsync())
                    {
                        // do something with each in the list or just return the list
                        var id = reader["Id"].ToString();
                        _logger.LogInformation($"sending mail for {reader["Name"].ToString() }, {reader["Location"].ToString()}");
                        var commentRequest = await dbContext.CommentRequests.FindAsync(Convert.ToInt32(id));

                        var request = new EmailWorkerModel
                        {
                            emailAddress = reader["emailAddress"].ToString(),
                            Id           = Convert.ToInt32(reader["Id"].ToString()),
                            Location     = reader["Location"].ToString(),
                            Name         = reader["Name"].ToString(),
                        };
                        commentRequest.emailed = await SendMail(request);

                        commentRequest.dateSent = DateTime.UtcNow;
                        dbContext.Attach(commentRequest);
                        dbContext.Entry(commentRequest).State = EntityState.Modified;
                        await dbContext.SaveChangesAsync();
                    }

                    command.Connection.Close();
                }
                return;
            }
        }