Beispiel #1
0
        private async static Task <List <LegoSet> > GetSetsToUpdate(List <LegoSet> sets, ILogger log)
        {
            List <LegoSet> updatedSets = new List <LegoSet>();
            int            i           = 0;

            foreach (LegoSet set in sets)
            {
                LegoSet updatedSet = new LegoSet();
                log.LogInformation($"Set {i++}, {set.Name}");
                try
                {
                    updatedSet = await PromoklockiHtmlParser.GetSetInfo(set.Link).TimeoutAfter(TimeoutMiliseconds);
                }
                catch (OperationCanceledException)
                {
                    log.LogInformation($"Timeout, {set.Name}");
                    continue;
                }
                catch (Exception e)
                {
                    log.LogError($"{e.Message}");
                    continue;
                }

                updatedSet.LastLowestPrice = set.LowestPrice;
                if (updatedSet.LowestPrice != set.LowestPrice)
                {
                    updatedSets.Add(updatedSet);
                }
            }

            return(updatedSets);
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            log.LogInformation("C# HTTP trigger function processed a request.");

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            string mail           = data?.mail;
            string url            = data?.url;
            bool   onlyBigUpdates = data?.onlyBigUpdates ?? false;

            if (url == null || mail == null || !IsUrlCorrec(url))
            {
                return(new BadRequestResult());
            }

            try
            {
                int    catalogNumber = GetCatalogNumber(url);
                string str           = Environment.GetEnvironmentVariable("sqldb_connectionstring");
                using (SqlConnection conn = new SqlConnection(str))
                {
                    conn.Open();
                    if (!SetIsInDb(conn, catalogNumber))
                    {
                        LegoSet set = await PromoklockiHtmlParser.GetSetInfo(url);
                        await AddNewSet(conn, set);
                    }

                    AddNewSubscription(conn, mail, catalogNumber, onlyBigUpdates);
                }

                stopwatch.Stop();
                return(new OkObjectResult($"You have just subscribed Lego {catalogNumber} set. ({stopwatch.ElapsedMilliseconds})"));
            }
            catch (Exception e)
            {
                return(new BadRequestResult());
            }
        }