public async Task <JObject> Save(JObject data)
 {
     return(JObject.Parse(JsonConvert.SerializeObject
                              (await _masterDataKeyValueBiz.Save(data), Formatting.None,
                              new JsonSerializerSettings()
     {
         ReferenceLoopHandling = ReferenceLoopHandling.Ignore
     })));
 }
        public async Task <MasterDataKeyValue> Save(JObject data)
        {
            dynamic masterDataDto = data;
            int     entityId;
            var     masterDatakeyValue = new MasterDataKeyValue();
            bool    isLeaf             = masterDataDto.IsLeaf;

            if (isLeaf)
            {
                try
                {
                    entityId = masterDataDto.ForeignKey3;
                }
                catch (Exception)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.FieldMustBeNumeric, "MasterDataKeyValue Id"));
                }
                int entityTypeId;
                try
                {
                    entityTypeId = masterDataDto.Key;
                }
                catch (Exception)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.FieldMustBeNumeric, "MasterDataKeyValue Key"));
                }
                int actionTypeId;
                try
                {
                    actionTypeId = masterDataDto.ForeignKey1;
                }
                catch (Exception)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.FieldMustBeNumeric, "MasterDataKeyValue ForeignKey1"));
                }
                if (entityTypeId != (int)EntityIdentity.Link)
                {
                    masterDatakeyValue = await _contentManagementContext.MasterDataKeyValues.SingleOrDefaultAsync(md => md.Id == entityId);

                    if (masterDatakeyValue == null)
                    {
                        throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.MasterDataKeyValuesNotFound));
                    }

                    if (!AuthorizeManager.IsAuthorize(masterDatakeyValue.AccessRoleId))
                    {
                        throw new UnauthorizedAccessException(LanguageManager.ToAsErrorMessage(ExceptionKey.InvalidGrant));
                    }
                }
                else if (actionTypeId == (int)ActionKey.ViewSourceCode)
                {
                    var link = await _contentManagementContext.Links.SingleOrDefaultAsync(ln => ln.Id == entityId);

                    var webPages = await _contentManagementContext.WebPages
                                   .Where(wp => wp.Url.ToLower() == link.Url).ToListAsync();

                    foreach (var webPage in webPages)
                    {
                        if (!AuthorizeManager.IsAuthorize(webPage.AccessRoleId))
                        {
                            throw new UnauthorizedAccessException(LanguageManager.ToAsErrorMessage(ExceptionKey.InvalidGrant));
                        }
                    }
                }
            }
            var updatedMd = await _masterDataKeyValueBiz.Save(data, true);

            if (!isLeaf || (updatedMd.Key == (int)EntityIdentity.Link))
            {
                return(updatedMd);
            }

            var key = CacheManager.GetAspectKey(CacheKey.Aspect.ToString(), ((int)ActionKey.RequestService).ToString(), masterDatakeyValue.PathOrUrl);

            CacheManager.Remove(key);
            return(updatedMd);
        }