public CampaighnOperations(IEntityDb <Campaighn> campaighnContext, IEntityDb <CampaighnRace> campaighnRaceContext) { _campaighnContext = campaighnContext; _campaighnRaceContext = campaighnRaceContext; _campaighnRaceContext.SetSession(_campaighnContext.GetSession()); }
public AttribureOperations(IEntityDb <DictionaryAttributeType> dictionaryAttrTypeContext, IEntityDb <DictionaryAttribute> dictionaryAttrContext) { _dictionaryAttrTypeContext = dictionaryAttrTypeContext; _dictionaryAttrContext = dictionaryAttrContext; _dictionaryAttrContext.SetSession(_dictionaryAttrTypeContext.GetSession()); }
public static List <AttributeLinkListModel> GetAttributes <T>(this IEntityDb <T> ownerContext, Guid ownerId) where T : class, IAttributeableEntity { var contextAttributeGroup = (IEntityDb <AttributeGroup>)Resolver.Resolve <IEntityDb <AttributeGroup> >(); var contextAttributeGroupData = (IEntityDb <AttributeGroupData>)Resolver.Resolve <IEntityDb <AttributeGroupData> >(); var contextAttribute = (IEntityDb <DictionaryAttribute>)Resolver.Resolve <IEntityDb <DictionaryAttribute> >(); contextAttributeGroup.SetSession(ownerContext.GetSession()); contextAttributeGroupData.SetSession(ownerContext.GetSession()); contextAttribute.SetSession(ownerContext.GetSession()); var attrGroupQuery = contextAttributeGroup.GetList().List().Where(ww => ownerContext.GetList() .List() .Where(w => w.OwnerId.Equals(ownerId)) .Select(s => s.AttributeGroupId).Contains(ww.Id)); var groupData = contextAttributeGroupData.GetList().List() .Join(contextAttribute.GetList().List(), agd => agd.AttributeId, att => att.Id, (agd, att) => new { Group = agd, Atr = att }) .Where(w => attrGroupQuery.Select(s => s.Id).Contains(w.Group.AttributeGroupId)) .ToList(); var attrGroup = attrGroupQuery.ToList(); var answer = attrGroup.Select(s => new AttributeLinkListModel { Id = s.Id, Count = s.AllowSelectCount }).ToList(); foreach (var elem in answer) { elem.Attributes = groupData.Where(w => w.Group.AttributeGroupId.Equals(elem.Id)) .Select(s => new AttributeLinkModel { Id = s.Group.AttributeId, Value = ConvertToType(s.Atr.Type, s.Group.Value), Label = s.Atr.Name, Type = s.Atr.Type, AllowSelectCount = null, ListValues = null, ListApi = null, ListApiParams = null }) .ToList(); } return(answer); }
protected override void UpdateDatabase(IEntityDb db) { HashSet <string> updated = new HashSet <string>(); IList <LVCountry> dbCountries = db.LVCountries.ToList(); IList <LVRegion> dbRegions = db.LVRegions.ToList(); //update existing application codes foreach (var dbCountry in dbCountries) { CountryModel updateCountries; if (parsedData.TryGetValue(dbCountry.Code, out updateCountries)) { int?regionId = null; if (dbRegions.Where(dbr => dbr.Code == updateCountries.RegionCode).Any()) { regionId = dbRegions.Where(dbr => dbr.Code == updateCountries.RegionCode).First().RegionID; } dbCountry.CountryName = updateCountries.Name; dbCountry.IsDeleted = updateCountries.IsDeleted; if (regionId != null) { dbCountry.RegionID = regionId; } updated.Add(dbCountry.Code); } else { dbCountry.IsDeleted = true; } } //new applications: foreach (var newCountry in parsedData.Where(a => !updated.Contains(a.Value.Code))) { int?regionId = null; if (dbRegions.Where(dbr => dbr.Code == newCountry.Value.RegionCode).Any()) { regionId = dbRegions.Where(dbr => dbr.Code == newCountry.Value.RegionCode).First().RegionID; } dbCountries.Add(db.LVCountries.Add(new LVCountry() { Code = newCountry.Value.Code, CountryName = newCountry.Value.Name, RegionID = regionId, IsDeleted = newCountry.Value.IsDeleted })); } //update replaceby value: foreach (var updatedCountry in parsedData.Where(a => !string.IsNullOrEmpty(a.Value.ReplacedBy))) { var oldAc = dbCountries.Where(ac => ac.Code == updatedCountry.Value.Code).FirstOrDefault(); var newAc = dbCountries.Where(ac => ac.Code == updatedCountry.Value.ReplacedBy).FirstOrDefault(); oldAc.IsDeleted = true; oldAc.ReplacedBy = newAc.CodeID; //set replaced by } }
public Response <bool> DeleteEntity <T>(Guid id, IEntityDb <T> context) where T : class, IEntity { try { var transaction = context.StartTransaction(); context.Delete(id); transaction.Commit(); } catch (Exception) { return(new Response <bool>(true, (int)ErrorEnum.HasReferences, "Удаление невозможно")); } return(new Response <bool>(true)); }
public DictionaryOperations(IEntityDb <Race> raceContext, IEntityDb <Subrace> subraceContext, IEntityDb <ItemType> itemTypeContext, IEntityDb <RaceAttribute> raceAttributeContext) { _raceContext = raceContext; _subraceContext = subraceContext; _itemTypeContext = itemTypeContext; _raceAttributeContext = raceAttributeContext; _subraceContext.SetSession(_raceContext.GetSession()); _itemTypeContext.SetSession(_raceContext.GetSession()); _raceAttributeContext.SetSession(_raceContext.GetSession()); }
/// <summary> /// Update CQP database with parsed values /// </summary> /// <param name="db">Initialized CQP database context</param> protected abstract void UpdateDatabase(IEntityDb db);
public static void SaveAttributes <T>(this List <AttributeLinkListModel> attributeModel, Guid ownerId, IEntityDb <T> ownerContext) where T : class, IAttributeableEntity { var contextAttributeGroup = (IEntityDb <AttributeGroup>)Resolver.Resolve <IEntityDb <AttributeGroup> >(); var contextAttributeGroupData = (IEntityDb <AttributeGroupData>)Resolver.Resolve <IEntityDb <AttributeGroupData> >(); contextAttributeGroup.SetSession(ownerContext.GetSession()); contextAttributeGroupData.SetSession(ownerContext.GetSession()); var deletedLinks = ownerContext.GetList() .List() .Where( w => w.OwnerId.Equals(ownerId) && !attributeModel.Where(ww => !ww.Id.Equals(null)) .Select(ss => ss.Id) .Contains(w.AttributeGroupId)); var dataAttrIds = contextAttributeGroupData.GetList().List() .Where(ww => attributeModel.Where(w => !w.Id.Equals(null)).Select(s => s.Id).Contains(ww.AttributeGroupId)) .Select(ss => new { ss.AttributeGroupId, ss.AttributeId, ss.Id }).ToList(); foreach (var deletedLink in deletedLinks) { ownerContext.Delete(deletedLink); } foreach (var attributeElement in attributeModel) { var groupId = attributeElement.Id ?? Guid.NewGuid(); if (attributeElement.Id.Equals(null)) { contextAttributeGroup.Upsert(new AttributeGroup { Id = groupId, AllowSelectCount = attributeElement.Count }); var constructor = typeof(T).GetConstructor(Type.EmptyTypes); if (constructor == null) { throw new Exception("Не найден конструктор"); } var newLink = (IAttributeableEntity)constructor.Invoke(new object[] { }); newLink.OwnerId = ownerId; newLink.AttributeGroupId = groupId; newLink.Id = Guid.NewGuid(); ownerContext.Upsert((T)newLink); } else { var deletingListGroupData = contextAttributeGroupData.GetList() .List() .Where( w => w.AttributeGroupId.Equals(groupId) && !attributeElement.Attributes.Select(s => s.Id).Contains(w.AttributeId)).ToList(); foreach (var deletedElement in deletingListGroupData) { contextAttributeGroupData.Delete(deletedElement); } } var upsertElementGroupData = attributeElement.Attributes.Select( s => new AttributeGroupData { Id = dataAttrIds.FirstOrDefault( f => f.AttributeGroupId.Equals(groupId) && f.AttributeId.Equals(s.Id))?.Id ?? Guid.NewGuid(), AttributeId = s.Id, AttributeGroupId = groupId, Value = s.Value?.ToString() ?? "" }); foreach (var upsertElement in upsertElementGroupData) { contextAttributeGroupData.Upsert(upsertElement); } } }
protected override void UpdateDatabase(IEntityDb db) { HashSet <string> updated = new HashSet <string>(); var dbCountryLocations = db.CompanyLocations.ToList(); //update existing application codes foreach (var dbCountryLocation in dbCountryLocations) { CompanyLocationModel updateCountries; if (parsedData.TryGetValue(dbCountryLocation.LocationId, out updateCountries)) { dbCountryLocation.CompanyCode = updateCountries.AbacusCode; dbCountryLocation.AddressId = updateCountries.AddressId; dbCountryLocation.LocationType = updateCountries.LocationType; dbCountryLocation.CountryHq = updateCountries.CountryHq; dbCountryLocation.Phone = updateCountries.Phone; dbCountryLocation.Fax = updateCountries.Fax; dbCountryLocation.LocalLanguageName = updateCountries.LocalLanguageName; dbCountryLocation.LocalName = updateCountries.LocalName; dbCountryLocation.MailingAddress = updateCountries.MailingAddress; dbCountryLocation.Comment = updateCountries.Comment; dbCountryLocation.Deleted = updateCountries.IsDeleted; dbCountryLocation.SuccessorId = updateCountries.ReplacedBy; dbCountryLocation.City = updateCountries.City; dbCountryLocation.CompanyName = updateCountries.Name; dbCountryLocation.Street = updateCountries.Street; dbCountryLocation.ZipCode = updateCountries.ZipCode; dbCountryLocation.State = updateCountries.State; updated.Add(dbCountryLocation.LocationId); } else { dbCountryLocation.Deleted = true; } } db.SaveChanges(); //new applications: foreach (var newApplication in parsedData.Where(a => !updated.Contains(a.Value.LocationId))) { db.CompanyLocations.Add(new CompanyLocations() { LocationId = newApplication.Value.LocationId, CompanyCode = newApplication.Value.AbacusCode, AddressId = newApplication.Value.AddressId, LocationType = newApplication.Value.LocationType, CountryHq = newApplication.Value.CountryHq, Phone = newApplication.Value.Phone, Fax = newApplication.Value.Fax, LocalLanguageName = newApplication.Value.LocalLanguageName, LocalName = newApplication.Value.LocalName, MailingAddress = newApplication.Value.MailingAddress, Comment = newApplication.Value.Comment, Deleted = newApplication.Value.IsDeleted, SuccessorId = newApplication.Value.ReplacedBy, City = newApplication.Value.City, CompanyName = newApplication.Value.Name, Street = newApplication.Value.Street, ZipCode = newApplication.Value.ZipCode, State = newApplication.Value.State }); } //update replaceby value: foreach (var updatedRegion in parsedData.Where(a => !string.IsNullOrEmpty(a.Value.ReplacedBy))) { var oldAc = dbCountryLocations.Where(ac => ac.LocationId == updatedRegion.Value.LocationId).FirstOrDefault(); var newAc = dbCountryLocations.Where(ac => ac.LocationId == updatedRegion.Value.ReplacedBy).FirstOrDefault(); oldAc.Deleted = true; oldAc.SuccessorId = newAc.LocationId; //set replaced by } }