private static void SeedCostEntries(JustMonitorDbContext dbContext) { if (!dbContext.CostEntries.Any()) { var allVehicles = dbContext.Vehicles.ToList(); var defaultCurrencyId = dbContext .Currencies .First(c => c.Code == GlobalConstants.DefaultCurrencyCode) .Id; var random = new Random(); var currentDate = DateTime.UtcNow; var minDay = 1; var maxDay = 29; var costEntryTypesIds = dbContext.CostEntryTypes.Select(cet => cet.Id).ToArray(); var note = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."; var costEntries = new List <CostEntry>(); foreach (var vehicle in allVehicles) { var startDate = new DateTime(currentDate.Year - 2, 1, 1); while (startDate < currentDate) { var day = random.Next(minDay, maxDay); var entryDate = new DateTime(startDate.Year, startDate.Month, day); var costEntryTypeId = costEntryTypesIds[random.Next(0, costEntryTypesIds.Length)]; var price = (decimal)random.NextDouble() * 250; var entry = new CostEntry(entryDate, costEntryTypeId, vehicle.Id, price, defaultCurrencyId, note); costEntries.Add(entry); startDate = startDate.AddMonths(4); } } dbContext.CostEntries.AddRange(costEntries); dbContext.SaveChanges(); } }
public async Task <bool> CreateAsync(DateTime dateCreated, int costEntryTypeId, int vehicleId, decimal price, int currencyId, string note, int?odometer) { var costEntry = new CostEntry(dateCreated, costEntryTypeId, vehicleId, price, currencyId, note, odometer); try { this.ValidateEntityState(costEntry); await this.db.CostEntries.AddAsync(costEntry); await this.db.SaveChangesAsync(); return(true); } catch { return(false); } }
private void SeedCostEntriesForVehicle(int count, int vehicleId, bool randomDate = false) { var random = new Random(); var entries = new List <CostEntry>(); var vehicle = this.dbContext.Vehicles.FirstOrDefault(v => v.Id == vehicleId); for (int i = 1; i <= count; i++) { var entry = new CostEntry(SampleDate, 1, vehicleId) { Vehicle = vehicle }; if (randomDate) { entry.DateCreated = SampleDate.AddDays(random.Next(1, 1000)); } entries.Add(entry); } this.dbContext.AddRange(entries); this.dbContext.SaveChanges(); }
private void SeedCostEntries(int count, bool randomDate = false) { var random = new Random(); var entriesToSeed = new List <CostEntry>(); for (int i = 1; i <= count; i++) { var sampleCostEntry = new CostEntry(SampleDateCreated, SampleCostEntryTypeId, SampleVehicleId) { Id = i }; if (randomDate) { sampleCostEntry.DateCreated = SampleDateCreated.AddDays(random.Next(1, 1000)); } entriesToSeed.Add(sampleCostEntry); } this.dbContext.AddRange(entriesToSeed); this.dbContext.SaveChanges(); }
/// <summary> /// Loads building entries from provided file into provided dictionary /// </summary> /// <param id="dict">Dictionary the entries will be loaded into</param> /// <param id="xmlFile">File the entries are loaded from</param> public static void LoadBuildings(SortedDictionary<string, BuildingDb> dict, XDocument xmlFile) { var bldgEntries = xmlFile.Descendants("Building"); foreach (var bldgXml in bldgEntries) { BuildingDb bldgEntry = new BuildingDb(); List<CostEntry> costEntries; foreach (XNode en in bldgXml.Nodes()) { XElement e = en as XElement; if (e == null) continue; switch (e.Name.LocalName.ToLower()) { case "id": bldgEntry.Id = e.Value; break; case "name": bldgEntry.Name = e.Value; break; case "tier": bldgEntry.Tier = int.Parse(e.Value); break; case "health": bldgEntry.Health = int.Parse(e.Value); break; case "constructiontime": bldgEntry.ConstructionAmount = int.Parse(e.Value); break; case "texture": if (!UI.Instance.HaveTexture(e.Value)) { throw new Exception("Entry: " + bldgEntry.Id + " - texture " + e.Value + " not found"); } bldgEntry.Texture = UI.Instance.GetTexture(e.Value); break; case "textureconstruction": if (!UI.Instance.HaveTexture(e.Value)) { throw new Exception("Entry: " + bldgEntry.Id + " - texture " + e.Value + " not found"); } bldgEntry.TextureConstruction = UI.Instance.GetTexture(e.Value); break; case "resourcecenter": bldgEntry.ResourceCenter = bool.Parse(e.Value); break; case "onlyoneallowed": bldgEntry.OnlyOneAllowed = bool.Parse(e.Value); break; case "builtonstart": bldgEntry.BuiltOnStart = bool.Parse(e.Value); break; case "unlockedby": bldgEntry.UnlockedByUR = e.Value; break; case "costs": #region costEntries = new List<CostEntry>(); foreach (XNode eCostsn in e.Nodes()) { XElement eCosts = eCostsn as XElement; if (eCosts == null) continue; CostEntry costEntry = new CostEntry(); foreach (XNode een in eCosts.Nodes()) { XElement ee = een as XElement; if (ee == null) continue; switch (ee.Name.LocalName.ToLower()) { case "resource": costEntry.EntryUR = ee.Value; break; case "amount": costEntry.Amount = int.Parse(ee.Value); break; } } costEntries.Add(costEntry); } bldgEntry.Cost = costEntries.ToArray(); #endregion break; case "trains": #region var trainEntries = new List<TrainEntry>(); foreach (XNode eTrainn in e.Nodes()) { XElement eTrain = eTrainn as XElement; if (eTrain == null) continue; TrainEntry trainEntry = new TrainEntry(); foreach (XNode een in eTrain.Nodes()) { XElement ee = een as XElement; if (ee == null) continue; switch (ee.Name.LocalName.ToLower()) { case "from": trainEntry.TrainFromUR = ee.Value; break; case "to": trainEntry.TrainToUR = ee.Value; break; case "speed": trainEntry.Speed = int.Parse(ee.Value); break; case "costs": costEntries = new List<CostEntry>(); foreach (XNode eCostsn in ee.Nodes()) { XElement eCosts = eCostsn as XElement; if (eCosts == null) continue; CostEntry costEntry = new CostEntry(); foreach (XNode eeen in eCosts.Nodes()) { XElement eee = eeen as XElement; if (eee == null) continue; switch (eee.Name.LocalName.ToLower()) { case "resource": costEntry.EntryUR = eee.Value; break; case "amount": costEntry.Amount = int.Parse(eee.Value); break; } } costEntries.Add(costEntry); } trainEntry.Cost = costEntries.ToArray(); break; } } trainEntries.Add(trainEntry); } bldgEntry.Trains = trainEntries.ToArray(); #endregion break; case "spawns": #region List<BuildingDb.SpawnEntry> spawnEntries = new List<BuildingDb.SpawnEntry>(); foreach (XNode eSpawnsn in e.Nodes()) { XElement eSpawns = eSpawnsn as XElement; if (eSpawns == null) continue; BuildingDb.SpawnEntry spawnEntry = new BuildingDb.SpawnEntry(); foreach (XNode een in eSpawns.Nodes()) { XElement ee = een as XElement; if (ee == null) continue; switch (ee.Name.LocalName.ToLower()) { case "entry": spawnEntry.EntryUR = ee.Value; break; case "speed": spawnEntry.Speed = int.Parse(ee.Value); break; case "capacity": spawnEntry.Capacity = int.Parse(ee.Value); break; } } spawnEntries.Add(spawnEntry); } bldgEntry.Spawns = spawnEntries.ToArray(); #endregion break; } } dict.Add(bldgEntry.Id, bldgEntry); } }