Ejemplo n.º 1
0
        public async Task <Guid> Handle(CreateCountryCommand request, CancellationToken cancellationToken)
        {
            var exists = _context.CoronaCountries.FirstOrDefault(item => item.Title == request.Title);

            if (string.IsNullOrEmpty(request.Title))
            {
                throw new Exception("Empty Title");
            }

            if (exists != null)
            {
                exists.NewCases       = request.NewCases;
                exists.Title          = request.Title;
                exists.NewDeaths      = request.NewDeaths;
                exists.TotalCases     = request.TotalCases;
                exists.TotalDeaths    = request.TotalDeaths;
                exists.TotalRecovered = request.TotalRecovered;

                _context.CoronaCountries.Update(exists);

                await _context.SaveChangesAsync(cancellationToken);

                return(exists.Id);
            }
            else
            {
                var entity = new CoronaCountry
                {
                    Title          = request.Title,
                    NewCases       = request.NewCases,
                    NewDeaths      = request.NewDeaths,
                    TotalDeaths    = request.TotalDeaths,
                    TotalCases     = request.TotalCases,
                    TotalRecovered = request.TotalRecovered
                };

                _context.CoronaCountries.Add(entity);
                await _context.SaveChangesAsync(cancellationToken);

                return(entity.Id);
            }
        }
Ejemplo n.º 2
0
        private void Scrap(IHtmlCollection <IElement> tableBody)
        {
            foreach (var table in tableBody)
            {
                var body = table.QuerySelectorAll("tbody").FirstOrDefault();
                if (body != null)
                {
                    var lines = body.QuerySelectorAll("tr");
                    foreach (var line in lines)
                    {
                        var collumns      = line.QuerySelectorAll("td");
                        var coronaCountry = new CoronaCountry();
                        int x;
                        coronaCountry.Title = collumns[1].Text().Trim();
                        if (int.TryParse(collumns[2].Text().Trim().Replace(",", ""), out x))
                        {
                            coronaCountry.TotalCases = x;
                        }
                        if (int.TryParse(collumns[3].Text().Trim().Replace(",", ""), out x))
                        {
                            coronaCountry.NewCases = x;
                        }
                        if (int.TryParse(collumns[4].Text().Trim().Replace(",", ""), out x))
                        {
                            coronaCountry.TotalDeaths = x;
                        }
                        if (int.TryParse(collumns[5].Text().Trim().Replace(",", ""), out x))
                        {
                            coronaCountry.NewDeaths = x;
                        }
                        if (int.TryParse(collumns[6].Text().Trim().Replace(",", ""), out x))
                        {
                            coronaCountry.TotalRecovered = x;
                        }


                        SendAsync(coronaCountry);
                    }
                }
            }
            Console.WriteLine("Scrap done");
        }