public async Task <List <WowAuctions> > GetAuctionsByRealm(string realmName, string regionName = "us") { AuctionsModel.AuctionFile file; AuctionsModel.Auctions a = new AuctionsModel.Auctions(); AuctionsModel.Auction[] auctions; string url = string.Empty; string fileContent = string.Empty; DateTime? latestTimeStampFromDb; List <WowAuctions> dbAuctions = new List <WowAuctions>(); List <WowAuctions> returnAuction = new List <WowAuctions>(); url = $"/auction/data/{realmName}?locale={regionName}"; file = JsonConvert.DeserializeObject <AuctionsModel.AuctionFile>(GetAPIRequest(url, regionName)); string fileURL = file.files[0].url; DateTime lastModified = UnixTimeStampToDateTime(file.files[0].lastModified); using (var db = new NinjaBotEntities()) { dbAuctions = db.WowAuctions.Where(r => r.RealmName.ToLower() == realmName.ToLower()).ToList(); latestTimeStampFromDb = dbAuctions.OrderBy(t => t.DateModified).Take(1).Select(l => l.DateModified).FirstOrDefault(); //db.Database.CurrentTransaction.Dispose(); //db.Database.Connection.Close(); } if (dbAuctions.Count > 0) { if (lastModified > latestTimeStampFromDb) { fileContent = GetAPIRequest(fileURL, true); } else { return(dbAuctions); } } else { fileContent = GetAPIRequest(fileURL, true); } a = JsonConvert.DeserializeObject <AuctionsModel.Auctions>(fileContent); auctions = a.auctions; auctions[0].fileDate = lastModified; await AddAuctionsToDb(realmName, a, auctions, lastModified); using (var db = new NinjaBotEntities()) { string slugName = a.realms[0].slug; returnAuction = db.WowAuctions.Where(w => w.RealmSlug == slugName).ToList(); //db.Database.Connection.Close(); } return(returnAuction); }
private async Task AddAuctionsToDb(string realmName, AuctionsModel.Auctions a, AuctionsModel.Auction[] auctions, DateTime lastModified) { using (var db = new NinjaBotEntities()) { List <WowAuctions> dbAuctions = new List <WowAuctions>(); //db.Configuration.AutoDetectChangesEnabled = false; //db.Configuration.ValidateOnSaveEnabled = false; try { string slugName = a.realms[0].slug; dbAuctions = db.WowAuctions.Where(w => w.RealmSlug == slugName).ToList(); } catch (Exception ex) { _logger.LogError(ex.Message); } if (dbAuctions.Count > 0) { DateTime?latestTimeStamp = dbAuctions.OrderBy(t => t.DateModified).Take(1).Select(l => l.DateModified).FirstOrDefault(); if (!string.IsNullOrEmpty(latestTimeStamp.Value.ToString("d"))) { if (lastModified > latestTimeStamp) { var delThese = db.WowAuctions.Where(d => d.DateModified != lastModified); db.WowAuctions.RemoveRange(delThese); await db.SaveChangesAsync(); foreach (var auction in auctions) { db.WowAuctions.Add(new WowAuctions { RealmName = realmName, AuctionBid = auction.bid, AuctionBuyout = auction.buyout, AuctionContext = auction.context, WowAuctionId = auction.auc, RealmSlug = a.realms[0].slug, AuctionItemId = auction.item, AuctionOwner = auction.owner, AuctionOwnerRealm = auction.ownerRealm, AuctionRand = auction.rand, AuctionQuantity = auction.quantity, AuctionTimeLeft = auction.timeLeft, AuctionSeed = auction.seed, DateModified = lastModified }); } _logger.LogInformation("New Auctions... Saving changes to DB"); } } } else { foreach (var auction in auctions) { db.WowAuctions.Add(new WowAuctions { RealmName = realmName, AuctionBid = auction.bid, AuctionBuyout = auction.buyout, AuctionContext = auction.context, WowAuctionId = auction.auc, RealmSlug = a.realms[0].slug, AuctionItemId = auction.item, AuctionOwner = auction.owner, AuctionOwnerRealm = auction.ownerRealm, AuctionRand = auction.rand, AuctionQuantity = auction.quantity, AuctionTimeLeft = auction.timeLeft, AuctionSeed = auction.seed, DateModified = lastModified }); } _logger.LogInformation("Saving changes to DB"); } await db.SaveChangesAsync(); //db.Database.Connection.Close(); } }