public object Patch([FromBody] dynamic model, string id)
        {
            var inboundRuleId = new InboundRuleId(id);

            Site site = inboundRuleId.SiteId == null ? null : SiteHelper.GetSite(inboundRuleId.SiteId.Value);

            if (inboundRuleId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            InboundRulesSection section = InboundRulesHelper.GetSection(site, inboundRuleId.Path);
            InboundRule         rule    = (InboundRule)section.InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

            if (rule == null)
            {
                return(NotFound());
            }

            InboundRulesHelper.UpdateRule(model, rule, site, inboundRuleId.Path, ManagementUnit.ResolveConfigScope(model));

            ManagementUnit.Current.Commit();

            dynamic updatedRule = InboundRulesHelper.RuleToJsonModel(rule, site, inboundRuleId.Path, Context.Request.GetFields(), true);

            if (updatedRule.id != id)
            {
                return(LocationChanged(InboundRulesHelper.GetRuleLocation(updatedRule.id), updatedRule));
            }

            return(updatedRule);
        }
        public object Post([FromBody] dynamic model)
        {
            if (model == null)
            {
                throw new ApiArgumentException("model");
            }

            RewriteId parentId = RewriteHelper.GetRewriteIdFromBody(model);

            if (parentId == null)
            {
                throw new ApiArgumentException("url_rewrite");
            }

            // Get site the rule is for if applicable
            Site site = parentId.SiteId == null ? null : SiteHelper.GetSite(parentId.SiteId.Value);

            string configPath           = ManagementUnit.ResolveConfigScope(model);
            InboundRulesSection section = InboundRulesHelper.GetSection(site, parentId.Path, configPath);

            // Create rule
            InboundRule rule = InboundRulesHelper.CreateRule(model, site, parentId.Path, ManagementUnit.ResolveConfigScope(model));

            // Add it
            InboundRulesHelper.AddRule(rule, section, model);

            // Save
            ManagementUnit.Current.Commit();

            //
            // Create response
            dynamic r = InboundRulesHelper.RuleToJsonModel(rule, site, parentId.Path, Context.Request.GetFields(), true);

            return(Created(InboundRulesHelper.GetRuleLocation(r.id), r));
        }
        public void Delete(string id)
        {
            var inboundRulesId = new RewriteId(id);

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;

            Site site = (inboundRulesId.SiteId != null) ? SiteHelper.GetSite(inboundRulesId.SiteId.Value) : null;

            if (site != null)
            {
                var section = InboundRulesHelper.GetSection(site, inboundRulesId.Path, ManagementUnit.ResolveConfigScope());
                section.RevertToParent();
                ManagementUnit.Current.Commit();
            }
        }
        public object Get(string id)
        {
            var inboundRuleId = new InboundRuleId(id);

            Site site = inboundRuleId.SiteId == null ? null : SiteHelper.GetSite(inboundRuleId.SiteId.Value);

            if (inboundRuleId.SiteId != null && site == null)
            {
                return(NotFound());
            }

            InboundRule rule = (InboundRule)InboundRulesHelper.GetSection(site, inboundRuleId.Path).InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));

            if (rule == null)
            {
                return(NotFound());
            }

            return(InboundRulesHelper.RuleToJsonModel(rule, site, inboundRuleId.Path, Context.Request.GetFields()));
        }
        public void Delete(string id)
        {
            InboundRule rule          = null;
            var         inboundRuleId = new InboundRuleId(id);

            Site site = inboundRuleId.SiteId == null ? null : SiteHelper.GetSite(inboundRuleId.SiteId.Value);

            if (inboundRuleId.SiteId == null || site != null)
            {
                rule = (InboundRule)InboundRulesHelper.GetSection(site, inboundRuleId.Path).InboundRules.FirstOrDefault(r => r.Name.Equals(inboundRuleId.Name, StringComparison.OrdinalIgnoreCase));
            }

            if (rule != null)
            {
                var section = InboundRulesHelper.GetSection(site, inboundRuleId.Path, ManagementUnit.ResolveConfigScope());

                InboundRulesHelper.DeleteRule(rule, section);
                ManagementUnit.Current.Commit();
            }

            Context.Response.StatusCode = (int)HttpStatusCode.NoContent;
        }
        public object Get()
        {
            string inboundRulesId = Context.Request.Query[Defines.IDENTIFIER];

            if (string.IsNullOrEmpty(inboundRulesId))
            {
                return(NotFound());
            }

            var sectionId = new RewriteId(inboundRulesId);

            // Get site rule is for if applicable
            Site site = sectionId.SiteId == null ? null : SiteHelper.GetSite(sectionId.SiteId.Value);

            InboundRuleCollection rules = InboundRulesHelper.GetSection(site, sectionId.Path).InboundRules;

            // Set HTTP header for total count
            this.Context.Response.SetItemsCount(rules.Count());

            return(new {
                rules = rules.Select(rule => InboundRulesHelper.RuleToJsonModelRef((InboundRule)rule, site, sectionId.Path, Context.Request.GetFields()))
            });
        }