예제 #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>());
        }
예제 #2
0
        public Response <bool> UpdateAttribute(AttributeEditModel model)
        {
            if (model.Abbreviation.Equals(""))
            {
                return(new Response <bool>(false, (int)ErrorEnum.Aplied, "Сокращенное название обязательно для заполнения"));
            }

            if (model.Name.Equals(""))
            {
                return(new Response <bool>(false, (int)ErrorEnum.Aplied, "Наименование обязательно для заполнения"));
            }

            var currentEntity = model.Id == null ? new DictionaryAttribute {
                Id = Guid.NewGuid()
            } : _dictionaryAttrContext.GetById((Guid)model.Id);

            currentEntity.Abbreviation      = model.Abbreviation.ToUpper().Replace(" ", "_");
            currentEntity.AttributeFunction = AttributeFunction.GetFunction(model.AttributeFunction);
            currentEntity.AttributeTypeId   = model.AttributeTypeId;
            currentEntity.Name = model.Name;

            var transaction = _dictionaryAttrContext.StartTransaction();

            _dictionaryAttrContext.Upsert(currentEntity);
            transaction.Commit();



            return(new Response <bool>());
        }
예제 #3
0
        public Response <bool> UpdateSubrace(SubraceListModel updateModel)
        {
            var currentEntity = updateModel.Id == null ? new Subrace() : _subraceContext.GetById((Guid)updateModel.Id);

            currentEntity.Name        = updateModel.Name;
            currentEntity.Description = updateModel.Description ?? "";
            currentEntity.RaceId      = updateModel.RaceId;
            _subraceContext.Upsert(currentEntity);
            return(new Response <bool>(true));
        }
예제 #4
0
        public Response <bool> UpdateItemType(ItemTypeEditModel updateModel)
        {
            var currentEntity = updateModel.Id == null
                ? new ItemType()
                : _itemTypeContext.GetById((Guid)updateModel.Id);

            currentEntity.Name        = updateModel.Name;
            currentEntity.Description = updateModel.Description ?? "";
            currentEntity.RootId      = updateModel.RootId;
            currentEntity.Plural      = updateModel.Plural;
            _itemTypeContext.Upsert(currentEntity);
            return(new Response <bool>(true));
        }
예제 #5
0
        public Response <bool> UpdateRace(RaceEditModel updateModel)
        {
            var transaction   = _raceContext.StartTransaction();
            var id            = updateModel.Id ?? Guid.NewGuid();
            var currentEntity = updateModel.Id == null
                ? new Race {
                Id = id
            }
                : _raceContext.GetById((Guid)updateModel.Id);

            currentEntity.Name        = updateModel.Name;
            currentEntity.Description = updateModel.Description ?? "";
            _raceContext.Upsert(currentEntity);

            updateModel.Attributes.SaveAttributes(id, _raceAttributeContext);

            transaction.Commit();
            return(new Response <bool>(true));
        }
예제 #6
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);
                }
            }
        }