예제 #1
0
        public override async Task <ActionResult <IEnumerable <New> > > GetTopNews([FromQuery] int quantity, [FromQuery] string subject)
        {
            if (quantity > 0)
            {
                Website vnexpress = new Zing();
                // vnexpress.SetUrl("https://vnexpress.net");
                var news = await vnexpress.GetTopNews(quantity, subject);

                if (news != null)
                {
                    foreach (var n in news)
                    {
                        var exist = _context.News.FirstOrDefault(e => e.Url == n.Url && e.Title == n.Title);
                        if (exist == null)
                        {
                            _context.News.Add(n);

                            try
                            {
                                var post = PostCreateModel.Create(n);
                                if (post == null)
                                {
                                    return(BadRequest());
                                }
                                await CreatePost(post);
                            }
                            catch (System.Exception e)
                            {
                                return(BadRequest(e.Message));
                            }
                        }
                    }
                    try
                    {
                        // await _context.SaveChangesAsync();
                    }
                    catch (Exception e)
                    {
                        throw new Exception(e.Message);
                    }
                    return(Ok(news));
                }
            }
            return(BadRequest(nameof(quantity)));
        }
예제 #2
0
 public Declarer(Zing.ErrorHandler errorHandler)
     : base(errorHandler)
 {
 }