Exemple #1
0
        public IActionResult EditOptionSet(EditOptionSetModel model)
        {
            if (ModelState.IsValid)
            {
                bool hasDupName = model.OptionSetName.GroupBy(x => x).Where(g => g.Count() > 1).Count() > 0;
                if (hasDupName)
                {
                    return(JError(T["validation_name_duplicated"]));
                }
                bool hasDupVal = model.OptionSetValue.GroupBy(x => x).Where(g => g.Count() > 1).Count() > 0;
                if (hasDupVal)
                {
                    return(JError(T["validation_value_duplicated"]));
                }
                var entity  = _optionSetFinder.FindById(model.OptionSetId);
                var details = entity.Items;
                model.CopyTo(entity);
                entity.IsPublic = true;
                _optionSetUpdater.Update(entity);
                int i = 0;
                foreach (var item in model.OptionSetName)
                {
                    if (item.IsEmpty())
                    {
                        continue;
                    }
                    Guid detailid = model.DetailId[i];
                    Schema.Domain.OptionSetDetail osd = new Schema.Domain.OptionSetDetail();
                    osd.OptionSetId  = entity.OptionSetId;
                    osd.Name         = item;
                    osd.Value        = model.OptionSetValue[i];
                    osd.IsSelected   = model.IsSelectedOption[i];
                    osd.DisplayOrder = i;
                    if (detailid.Equals(Guid.Empty))
                    {
                        osd.OptionSetDetailId = Guid.NewGuid();
                        _optionSetDetailCreater.Create(osd);
                    }
                    else
                    {
                        osd.OptionSetDetailId = detailid;
                        _optionSetDetailUpdater.Update(osd);
                        details.Remove(details.Find(n => n.OptionSetDetailId == detailid));
                    }

                    i++;
                }
                //delete lost detail
                if (details.NotEmpty())
                {
                    var lostid = details.Select(n => n.OptionSetDetailId).ToList();
                    _optionSetDetailDeleter.DeleteById(lostid.ToArray());
                }

                return(UpdateSuccess(new { id = entity.OptionSetId }));
            }
            return(UpdateFailure(GetModelErrors()));
        }
Exemple #2
0
        public bool Update(Domain.Attribute entity)
        {
            var result = true;

            using (UnitOfWork.Build(_attributeRepository.DbContext))
            {
                result = _attributeRepository.Update(entity);
                if (entity.OptionSetId.HasValue && entity.OptionSet != null && !entity.OptionSet.IsPublic)
                {
                    var details = _optionSetDetailFinder.Query(n => n.Select(f => f.OptionSetDetailId).Where(f => f.OptionSetId == entity.OptionSetId.Value));
                    foreach (var item in entity.OptionSet.Items)
                    {
                        if (item.OptionSetDetailId.Equals(Guid.Empty))
                        {
                            item.OptionSetDetailId = Guid.NewGuid();
                            result = _optionSetDetailCreater.Create(item);
                        }
                        else
                        {
                            result = _optionSetDetailUpdater.Update(item);
                        }
                    }
                    //delete lost
                    var ids    = entity.OptionSet.Items.Select(n => n.OptionSetDetailId);
                    var lostid = details.Select(n => n.OptionSetDetailId).Except(ids).ToList();
                    if (lostid.NotEmpty())
                    {
                        result = _optionSetDetailDeleter.DeleteById(lostid.ToArray());
                    }
                }
                if (entity.PickLists.NotEmpty())//bit
                {
                    foreach (var item in entity.PickLists)
                    {
                        if (item.StringMapId.Equals(Guid.Empty))
                        {
                            result = _stringMapCreater.Create(item);
                        }
                        else
                        {
                            result = _stringMapUpdater.Update(item);
                        }
                    }
                }
                //localization
                _localizedLabelService.Update(entity.LocalizedName.IfEmpty(""), "LocalizedName", entity.AttributeId, this._appContext.BaseLanguage);
                _localizedLabelService.Update(entity.Description.IfEmpty(""), "Description", entity.AttributeId, this._appContext.BaseLanguage);
                //set to cache
                var optionSetEntity = _optionSetFinder.FindById(entity.OptionSet.OptionSetId);
                _cacheService.SetEntity(entity);
                _cacheServiceOption.RemoveEntity(optionSetEntity);
            }
            return(result);
        }