// GET api/SearchBeer
        public async Task<List<BeerItem>> Get(string keyword)
        {
            Services.Log.Info(string.Format("Search beer call with keyword {0}",keyword));
            var rv = new List<BeerItem>();

            if (!BreweryDBHelper.InsureBreweryDBIsInitialized(Services))
                return rv;

            try
            {
                var results = await new BreweryDB.BreweryDBClient().SearchForBeer(keyword);
                if (results != null && results.Any())
                {
                    Services.Log.Info(string.Format("Found {0} beers", results.Count()));

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

                                if (r.Style != null)
                                {
                                    var style = context.BeerStyles.FirstOrDefault(f => f.Name == r.Style.Name);
                                    if (style == null)
                                    {
                                        style = r.Style.ToBeerStyle();
                                        context.BeerStyles.Add(style);
                                    }
                                    beer.StyleId = style.Id;
                                }
                                context.BeerItems.Add(beer);
                            }
                            catch (Exception ex)
                            {
                                Services.Log.Error(string.Format("Exception creating beer: {0}", ex.Message));
                            }
                        }
                        rv.Add(beer);
                    }
                    if (needSave)
                        await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                Services.Log.Error(ex.Message);
            }

            return rv;
        }
        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!");
        }
Esempio n. 3
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!");
        }
        // 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(Services, 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");
        }