Пример #1
0
        public async override Task ExecuteAsync()
        {
            Services.Log.Info("Hello from scheduled job!");

            if (!BreweryDBHelper.InsureBreweryDBIsInitialized(Services))
            {
                Services.Log.Error("Could not init BreweryDB API");
                return;
            }

            Services.Log.Info("BreweryDB is initialized");

            var context = new BeerDrinkinContext();

            foreach (var beerItem in context.BeerItems)
            {
                Services.Log.Info("updating beer " + beerItem.Name);
                var beer = await new BreweryDB.BreweryDBClient().QueryBeerById(beerItem.Id);
                if (beer == null)
                {
                    Services.Log.Error(string.Format("Could not get beer {0} with id {1}", beerItem.Name, beerItem.BreweryDBId));
                    continue;
                }
                //this call updates beerItem form BreweryDB beer object
                var newBeerItem = beer.ToBeerItem(beerItem);
            }
            await context.SaveChangesAsync();

            Services.Log.Info("UpdateBeerJob is completed!");
        }
Пример #2
0
        // POST api/BinaryItem
        public async Task <HttpResponseMessage> Post(BinaryUploadRequest binaryUploadRequest)
        {
            var binaryItem = new BinaryItem
            {
                Id         = Guid.NewGuid().ToString("N"),
                ObjectId   = binaryUploadRequest.BinaryId,
                BinaryType = binaryUploadRequest.BinaryType,
                UserId     = binaryUploadRequest.UserId
            };

            binaryItem.BinaryUrl =
                await BlobUtils.SaveBinaryToAzureStorage(settings, binaryItem.Id, binaryUploadRequest.BinaryData);

            if (!string.IsNullOrEmpty(binaryItem.BinaryUrl))
            {
                BeerDrinkinContext context = new BeerDrinkinContext();
                context.BinaryItems.Add(binaryItem);
                await context.SaveChangesAsync();

                return(this.Request.CreateResponse(HttpStatusCode.OK));
            }

            return(this.Request.CreateResponse(HttpStatusCode.Conflict,
                                               "Something wrong"));
        }
Пример #3
0
        public async override Task ExecuteAsync()
        {
            //We'll be going off to BreweryDB, fetching all the beers they have and checking if we've already for that in our DB. If not, we'll go ahead and save it.

            var context = new BeerDrinkinContext();

            var processedPageCount = 1;
            var totalPageCount     = 99; //We replace this pretty quickly!

            while (totalPageCount != processedPageCount)
            {
                var pageResult = await AllBeers(processedPageCount);

                totalPageCount = pageResult.NumberOfPages;

                foreach (var beer in pageResult.Data)
                {
                    var beerItem = beer.ToBeerItem();
                    context.Beers.Add(beerItem);
                    Services.Log.Info($"Saved {beerItem.Name} into our database");
                }
                Services.Log.Info($"Finished processing page {processedPageCount} of {totalPageCount}");
                processedPageCount++;

                await context.SaveChangesAsync();
            }

            Services.Log.Info("Hello from scheduled job!");
        }
Пример #4
0
        // GET api/SearchBeer
        public async Task <List <Beer> > Get(string keyword)
        {
            tracer.Info(string.Format("Search beer call with keyword {0}", keyword));
            var  rv       = new List <Beer>();
            bool needSave = false;

            if (!BreweryDBHelper.InsureBreweryDbIsInitialized(settings, tracer))
            {
                return(rv);
            }

            try
            {
                var results = await new BreweryDB.BreweryDBClient().SearchForBeer(keyword);

                if (results != null && results.Any())
                {
                    tracer.Info(string.Format("Found {0} beers", results.Count()));

                    var context = new BeerDrinkinContext();
                    foreach (var r in results)
                    {
                        //check if we already have beer in db
                        var beer = context.Beers.FirstOrDefault(f => f.BreweryDbId == r.Id);
                        if (beer == null)
                        {
                            tracer.Info(string.Format("Beer {0} wasn't logged yet", r.Name));
                            needSave = true;
                            try
                            {
                                beer = r.ToBeerItem();

                                context.Beers.Add(beer);
                            }
                            catch (Exception ex)
                            {
                                tracer.Error(string.Format("Exception creating beer: {0}", ex.Message));
                            }
                        }
                        rv.Add(beer);
                    }
                }
            }
            catch (Exception ex)
            {
                tracer.Error(ex.Message);
            }
            finally
            {
                if (needSave)
                {
                    await context.SaveChangesAsync();
                }
            }

            return(rv);
        }