Ejemplo n.º 1
0
        public Response <bool> UpdateCampaighn(CampaighnEditModel model)
        {
            if ((model.StartLevel ?? 1) < 1 || (model.StartLevel ?? 1) > 100)
            {
                return(new Response <bool>(false, (int)ErrorEnum.Aplied, "Уровень должен быть в диапазоне 1-99"));
            }

            var transaction = _campaighnContext.StartTransaction();

            var currentEntity = model.Id == null ? new Campaighn() : _campaighnContext.GetById((Guid)model.Id);
            var campaighnId   = model.Id ?? Guid.NewGuid();

            currentEntity.Name       = model.Name;
            currentEntity.StartLevel = model.StartLevel ?? 1;

            IEnumerable <CampaighnRace> newRaces;

            if (!model.Id.Equals(null))
            {
                var oldRaces =
                    _campaighnRaceContext.GetList()
                    .List()
                    .Where(w => w.CampaighnId.Equals(model.Id) && !model.Subraces.Contains(w.SubraceId));
                foreach (var oldRace in oldRaces)
                {
                    _campaighnRaceContext.Delete(oldRace);
                }

                newRaces =
                    model.Subraces.Where(
                        w =>
                        !_campaighnRaceContext.GetList()
                        .List()
                        .Where(ww => ww.CampaighnId.Equals(model.Id))
                        .Select(ss => ss.SubraceId)
                        .ToList()
                        .Contains(w)).Select(s => new CampaighnRace
                {
                    CampaighnId = campaighnId,
                    SubraceId   = s
                });
            }
            else
            {
                newRaces = model.Subraces.Select(s => new CampaighnRace
                {
                    CampaighnId = campaighnId,
                    SubraceId   = s
                });
            }

            foreach (var newRace in newRaces)
            {
                _campaighnRaceContext.Upsert(newRace);
            }


            transaction.Commit();
            return(new Response <bool>());
        }
Ejemplo n.º 2
0
 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));
 }
Ejemplo n.º 3
0
        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);
                }
            }
        }