private bool SplitData(SeedData item, Property property) { var token = item.JObject.GetValue(property.Name); item.JObject.Remove(property.Name); var dataList = GetDataList(property.Class); if (token is JObject obj) { SeedRecord record = ToSeedRecord(property.Class, obj); dataList.Add(new SeedData(obj, record)); item.SeedRecord.Values.Add(new KeyValuePair <string, string>(property.Name + "Id", record.GetId())); return(true); } return(false); }
private bool SplitData(SeedData item, NestedResource nestedResource) { var token = item.JObject.GetValue(nestedResource.Name.Plural); item.JObject.Remove(nestedResource.Name.Plural); var dataList = GetDataList(nestedResource.Class); if (token is JArray array) { foreach (var o in array) { JObject jObj = o as JObject; if (jObj == null) { continue; } SeedRecord record = ToSeedRecord(nestedResource.Class, jObj); var id = record.GetId(); if (!dataList.Any(d => d.SeedRecord.GetId() == id)) { dataList.Add(new SeedData(jObj, record)); } if (nestedResource.Pivot != null) { var pivotDataList = GetDataList(nestedResource.Pivot); var pivotRecord = new SeedRecord { Name = nestedResource.Pivot.Name, IsPivot = true, Values = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>(item.SeedRecord.Name.Plural + "Id", item.SeedRecord.GetId()), new KeyValuePair <string, string>(nestedResource.Name.Plural + "Id", record.GetId()) } }; pivotDataList.Add(new SeedData(null, pivotRecord)); } else { record.Values.Add(new KeyValuePair <string, string>(nestedResource.ParentRelation.Name + "Id", item.SeedRecord.GetId())); } } return(true); } return(false); }
private SeedRecord ToSeedRecord(ClassModel c, JObject obj) { var record = new SeedRecord { Name = c.Name }; foreach (var property in obj.Properties()) { switch (property.Value.Type) { case JTokenType.Date: var dateTime = (DateTime)property.Value; record.Set(property.Name, $"new DateTime({dateTime.Year}, {dateTime.Month}, {dateTime.Day}, {dateTime.Hour}, {dateTime.Minute}, {dateTime.Second})"); break; case JTokenType.String: record.Set(property.Name, $"\"{property.Value.ToString()}\""); break; case JTokenType.Integer: record.Set(property.Name, property.Value.ToString()); break; case JTokenType.Float: record.Set(property.Name, $"{property.Value.ToString()}f"); break; case JTokenType.Boolean: record.Set(property.Name, ((bool)property.Value).ToString().ToLower()); break; } } if (c.HasTimestamps) { record.Set("Created", "DateTime.Now"); record.Set("Updated", "DateTime.Now"); } record.Values.Insert(0, KeyValuePair.Create("Id", GetNextId(c))); return(record); }
internal SeedData(JObject obj, SeedRecord record) { JObject = obj; SeedRecord = record; }