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!"); }
// 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; }
// GET api/beer public async Task<List<BeerItem>> Get(string upc) { BeerDrinkinContext context = new BeerDrinkinContext(); var beers = context.BeerItems.Where(x => x.UPC == upc).ToList(); return beers; }
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!"); }
// GET api/UPC public List<Beer> Get(string upc) { tracer.Info($"Searching for Barcode number: {upc}"); ITraceWriter traceWriter = this.Configuration.Services.GetTraceWriter(); BeerDrinkinContext context = new BeerDrinkinContext(); var beers = context.Beers.Where(x => x.UPC == upc).ToList(); return beers; }
/* // GET api/BinaryItem public List<string> Get(string objectId, string type) { var context = new BeerDrinkinContext(); return context.BinaryItems.Where(f => f.ObjectId == objectId && f.BinaryType == type) .Select(f => f.BinaryUrl) .ToList(); }*/ // GET api/BinaryItem public List<string> Get(string userId) { var context = new BeerDrinkinContext(); var photos = context.BinaryItems.Where(f => f.UserId == userId) .Select(f => f.BinaryUrl) .ToList(); return photos; }
public async override Task ExecuteAsync() { var searchServiceName = "beerdrinkin"; string apiKey; if (!Services.Settings.TryGetValue("SEARCH_PRIMARY_ADMIN_KEY", out apiKey)) { Services.Log.Error("Failed to find Search Service admin key"); } var serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); var indexClient = serviceClient.Indexes.GetClient("beers"); Services.Log.Info("Started updating Beer Drinkin's search engine index!"); var definition = new Index() { Name = "beers", Fields = new[] { new Field("id", DataType.String) { IsKey = true }, new Field("name", DataType.String) { IsSearchable = true, IsFilterable = true }, new Field("description", DataType.String) { IsSearchable = true, IsFilterable = true }, new Field("brewery", DataType.String) { IsSearchable = true, IsFilterable = true }, new Field("abv", DataType.String) { IsFilterable = true, IsSortable = true } } }; await serviceClient.Indexes.CreateOrUpdateAsync(definition); try { var context = new BeerDrinkinContext(); var beerItems = context.Beers; if (beerItems.Count() != 0) { var beers = beerItems.ToArray(); await indexClient.Documents.IndexAsync(IndexBatch.Create(beers.Select(IndexAction.Create))); } else Services.Log.Info("Failed to find any beers in the DB to index"); } catch (IndexBatchException e) { // Sometimes when your Search service is under load, indexing will fail for some of the documents in // the batch. Depending on your application, you can take compensating actions like delaying and // retrying. For this simple demo, we just log the failed document keys and continue. Services.Log.Error( $"Failed to index some of the documents: {string.Join(", ", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key))}"); } Services.Log.Info("Finished updating Beer Drinkin's search engine index!"); }
// GET api/HeaderInfo public HeaderInfo Get(string userId) { BeerDrinkinContext context = new BeerDrinkinContext(); var headerInfo = new HeaderInfo { Username = userId }; var checkIns = context.CheckInItems.Where(f => f.CheckedInBy == userId); var photos = context.BinaryItems.Where(f => f.UserId == userId); headerInfo.CheckIns = checkIns.Count(f => f.Deleted == false); headerInfo.Photos = photos.Count(f => f.Deleted == false); headerInfo.Ratings = checkIns.Count(f => f.Rating > 0); return headerInfo; }
// GET api/PopularBeers public async Task<List<BeerItem>> Get(double longitude, double latitude) { //Find the current country of the user string bingKey; if (!(Services.Settings.TryGetValue("BING_API_KEY", out bingKey))) { Services.Log.Error("Could not retrieve Bing API key."); bingKey = "AlPB42X199-b_n7tnHPSNM15E4cvLv18hfj4upv3irWgSFHx5GplSaOS3wpggCox"; } Services.Log.Info($"Bing API Key{bingKey}"); string countryRegion; var client = new Bing.MapsClient(bingKey); var result = await client.LocationQuery(new Bing.Maps.Point(latitude, longitude)); var locations = result.GetLocations(); if(locations.Count != 0) { countryRegion = result.GetLocations().First().Address.CountryRegion; Services.Log.Info($"A user is has just been seen in {countryRegion}"); } else { Services.Log.Error("Failed to lookup country. Results returned as null"); } var context = new BeerDrinkinContext(); var beerList = new List<BeerItem>(); //Setup BreweryDB Client if (string.IsNullOrEmpty(BreweryDBClient.ApplicationKey)) { string apiKey; // Try to get the BreweryDB API key app settings. if (!(Services.Settings.TryGetValue("BREWERYDB_API_KEY", out apiKey))) { Services.Log.Error("Could not retrieve BreweryDB API key."); return null; } Services.Log.Info($"BreweryDB API Key {apiKey}"); BreweryDBClient.Initialize(apiKey); } return beerList; }
// GET api/UserPrivateData public UserPrivateData Get(string username) { try { BeerDrinkinContext context = new BeerDrinkinContext(); return context.UserPrivateItems.SingleOrDefault(f => f.Id == username); } catch (Exception ex) { tracer.Error(ex.Message); } return null; }
public HttpResponseMessage Post(UserPrivateData privateData) { try { BeerDrinkinContext context = new BeerDrinkinContext(); var old= context.UserPrivateItems.SingleOrDefault(f => f.Id == privateData.Id); if (old != null) { old.DateOfBirth = privateData.DateOfBirth; } else context.UserPrivateItems.Add(privateData); context.SaveChanges(); return this.Request.CreateResponse(HttpStatusCode.OK); } catch { } return this.Request.CreateResponse(HttpStatusCode.Conflict, "Something wrong"); }
// 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"); }