Exemplo n.º 1
0
 public IActionResult Create(CommentModel c)
 {
     using (var db = new CommentsDbContext())
     {
         if (ModelState.IsValid)
         {
             db.Comments.Add(new CommentModel
             {
                 OrganizationName = c.OrganizationName,
                 User             = c.User,
                 Post             = c.Post
             });
             db.SaveChanges();
             return(RedirectToAction(nameof(Index)));
         }
         return(View());
     }
 }
        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.º 3
0
        public List <AuthorCommentsPoco> GetAll(Guid authorId)
        {
            AuthorCommentsPoco authorComments;

            using (var dbContext = new CommentsDbContext())
            {
                authorComments = from author in dbContext.Authors
                                 join comment in dbContext.Comments
                                 on author.Id equals comment.AuthorId
                                 where author.Id == authorId
                                 select new AuthorCommentsPoco
                {
                    AuthorId   = author.Id,
                    AuthorName = author.Name,
                    Comments   = comment                   //TODO
                };
            }
            return(authorComments);
        }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 5
0
 public ContactController(CommentsDbContext context)
 {
     _context = context;
 }