public async Task Execute(IJobExecutionContext context) { int warnings = 0; int errors = 0; logger.LogInformation($"[{DateTimeOffset.Now}]BazaarScheduleJob started"); // Get bazaar api data var response = await skyblockApiService.GetBazaarResponse(); if (response.Success == false) { // Request failed errors++; // TODO } if (lastUpdated != response.LastUpdated && errors == 0) { lastUpdated = response.LastUpdated; long timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); foreach (var product in response.Products) { try { BazaarProductIDMapping mapping = db.BazaarProductIDMappings.FirstOrDefault(e => e.Name == product.Key); if (mapping == null) { errors++; logger.LogError("Mapping was NULL"); continue; } BazaarProductInfo info = product.Value.BazaarProductInfo; db.BazaarProductEntries.Add(new BazaarProductEntry() { Timestamp = timestamp, MappingID = mapping.ID, BuyPrice = info.BuyPrice, SellPrice = info.SellPrice }); } catch (Exception e) { errors++; logger.LogError(e, "Failed adding bazaar entry"); } } try { db.SaveChanges(); } catch (Exception e) { errors++; logger.LogError(e, "Failed to save bazaar data"); } } else { warnings++; logger.LogWarning($"[{DateTimeOffset.Now}]Bazaar already up-to-date"); } logger.LogInformation($"[{DateTimeOffset.Now}]BazaarScheduleJob finished with {warnings} warnings and {errors} errors"); }
public async Task <IActionResult> Get() { var response = await skyblockApiService.GetBazaarResponse(); if (response.Success == false) { return(BadRequest("Bazaar unavailble")); } if (lastUpdated != response.LastUpdated) { lastUpdated = response.LastUpdated; long timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); foreach (var product in response.Products) { try { BazaarProductIDMapping mapping = db.BazaarProductIDMappings.FirstOrDefault(e => e.Name == product.Key); if (mapping == null) { logger.LogError("Mapping was NULL"); continue; } BazaarProductInfo info = product.Value.BazaarProductInfo; db.BazaarProductEntries.Add(new BazaarProductEntry() { Timestamp = timestamp, MappingID = mapping.ID, BuyPrice = info.BuyPrice, SellPrice = info.SellPrice }); } catch (Exception e) { logger.LogError(e, "Failed adding bazaar entry"); } } try { db.SaveChanges(); } catch (Exception e) { logger.LogError(e, "Failed to save bazaar data"); } } else { return(BadRequest("Already up-to-date")); } // I used this to easily get seed data for bazaar product mappings /* * string teststring = ""; * int counter = 0; * foreach (var product in response.Products) * { * teststring += "new BazaarProductIDMapping { ID = " + counter.ToString() + ", Name = \"" + product.Key.ToString() + "\" },\r\n"; * counter++; * } */ return(Ok("OK")); }