예제 #1
0
        public static void AddTextContentApi_code(SiteDb sitedb, string folderName, string actionName, string codebody)
        {
            string url = "/textapi/" + folderName + "/" + actionName;

            Code code = new Code();

            code.Name = "textapi_" + folderName + "_" + actionName;
            code.Body = codebody;

            var oldcode = sitedb.Code.Get(code.Id);

            if (oldcode != null)
            {
                return; // already exists.
            }

            code.CodeType = CodeType.Api;

            if (code.CodeType == Sites.Models.CodeType.Api)
            {
                if (!sitedb.Routes.Validate(url, code.Id))
                {
                    // already exists...
                    return;
                }
            }

            sitedb.Code.AddOrUpdate(code);

            var route = new Kooboo.Sites.Routing.Route();

            route.Name                 = url;
            route.objectId             = code.Id;
            route.DestinationConstType = ConstObjectType.Code;
            sitedb.Routes.AddOrUpdate(route);
        }
예제 #2
0
파일: Code.cs 프로젝트: xhute/Kooboo
        public virtual Guid Post(CodeEditViewModel model, ApiCall call)
        {
            var sitedb = call.WebSite.SiteDb();

            Code code = new Code();

            code.Name   = model.Name;
            code.Config = model.Config;
            code.Body   = model.Body;

            if (HasScriptTag(code.Body))
            {
                throw new Exception(Data.Language.Hardcoded.GetValue("You do not need script tag in code. Only in the page, view or layout, you need the script tag"));
            }

            if (!string.IsNullOrEmpty(model.EventType))
            {
                code.EventType = Lib.Helper.EnumHelper.GetEnum <Sites.FrontEvent.enumEventType>(model.EventType);
            }

            if (!string.IsNullOrEmpty(model.CodeType))
            {
                code.CodeType = Lib.Helper.EnumHelper.GetEnum <CodeType>(model.CodeType);
            }

            code.Id = model.Id;

            if (code.CodeType == Sites.Models.CodeType.Api)
            {
                if (!string.IsNullOrEmpty(model.Url) && !sitedb.Routes.Validate(model.Url, model.Id))
                {
                    throw new Exception(Data.Language.Hardcoded.GetValue("Url occupied", call.Context));
                }

                // check if it only return Json...
                code.IsJson = Lib.Helper.JsonHelper.IsJson(code.Body);
            }


            if (model.Id != default(Guid))
            {
                var oldcode = sitedb.Code.Get(model.Id);
                if (oldcode != null)
                {
                    oldcode.Name   = model.Name;
                    oldcode.Body   = model.Body;
                    oldcode.Config = model.Config;
                    oldcode.IsJson = Lib.Helper.JsonHelper.IsJson(oldcode.Body);

                    if (oldcode.IsEmbedded && oldcode.CodeType == Sites.Models.CodeType.PageScript)
                    {
                        sitedb.Code.AddOrUpdate(oldcode, true, true, call.Context.User.Id);
                    }
                    else
                    {
                        sitedb.Code.AddOrUpdate(oldcode, call.Context.User.Id);
                    }


                    code.EventType = oldcode.EventType;
                    code.CodeType  = oldcode.CodeType;
                }
            }
            else
            {
                sitedb.Code.AddOrUpdate(code);
            }

            if (code.CodeType == Sites.Models.CodeType.Api)
            {
                string url = model.Url;
                if (!string.IsNullOrEmpty(url))
                {
                    var route = new Kooboo.Sites.Routing.Route();
                    route.Name                 = url;
                    route.objectId             = code.Id;
                    route.DestinationConstType = ConstObjectType.Code;
                    sitedb.Routes.AddOrUpdate(route);
                }
                else
                {
                    // delete the route.
                    var route = sitedb.Routes.GetByObjectId(code.Id);
                    if (route != null)
                    {
                        sitedb.Routes.Delete(route.Id);
                    }
                }
            }

            return(code.Id);
        }
예제 #3
0
        public static Kooboo.Sites.Routing.Route RaiseRouteEvent(enumEventType eventtype, RenderContext context, Kooboo.Sites.Routing.Route route = null)
        {
            if (eventtype == enumEventType.RouteFinding)
            {
                RouteFinding finding = new RouteFinding(context);
                RaiseEvent(context, finding);
                return(finding.Route);
            }
            else if (eventtype == enumEventType.RouteFound)
            {
                if (route != null)
                {
                    RouteFound found = new RouteFound(context, route);
                    RaiseEvent(context, found);
                    if (found.DataChange && found.Route != null)
                    {
                        return(found.Route);
                    }
                }
            }
            else if (eventtype == enumEventType.RouteNotFound)
            {
                RouteNotFound notfound = new RouteNotFound(context);
                RaiseEvent(context, notfound);
                return(notfound.Route);
            }

            return(null);
        }
예제 #4
0
        public virtual Guid Post(CodeEditViewModel model, ApiCall call)
        {
            if (model.Url != null)
            {
                if (model.Url.StartsWith("\\"))
                {
                    model.Url = "/" + model.Url.Substring(1);
                }
                if (!model.Url.StartsWith("/"))
                {
                    model.Url = "/" + model.Url;
                }
            }
            else
            {
                throw new Exception(Kooboo.Data.Language.Hardcoded.GetValue("Url is required", call.Context));
            }

            var sitedb = call.WebSite.SiteDb();

            Code code = new Code();

            code.Name   = model.Name;
            code.Config = model.Config;
            code.Body   = model.Body;

            if (HasScriptTag(code.Body))
            {
                throw new Exception(Data.Language.Hardcoded.GetValue("You do not need script tag in code. Only in the page, view or layout, you need the script tag"));
            }

            if (!string.IsNullOrEmpty(model.EventType))
            {
                code.EventType = Lib.Helper.EnumHelper.GetEnum <Sites.FrontEvent.enumEventType>(model.EventType);
            }

            if (!string.IsNullOrEmpty(model.CodeType))
            {
                code.CodeType = Lib.Helper.EnumHelper.GetEnum <CodeType>(model.CodeType);
            }

            code.Id = model.Id;

            if (code.CodeType == Sites.Models.CodeType.Api)
            {
                if (!string.IsNullOrEmpty(model.Url) && !sitedb.Routes.Validate(model.Url, model.Id))
                {
                    // one more verify.
                    var route = sitedb.Routes.Get(model.Url);
                    if (route != null && route.objectId != default(Guid))
                    {
                        var siteobjecttype = Kooboo.ConstTypeContainer.GetModelType(route.DestinationConstType);
                        if (siteobjecttype != null)
                        {
                            var repo = sitedb.GetSiteRepositoryByModelType(siteobjecttype);
                            if (repo != null)
                            {
                                var obj = repo.Get(route.objectId);
                                if (obj != null)
                                {
                                    throw new Exception(Data.Language.Hardcoded.GetValue("Url occupied", call.Context));
                                }
                            }
                        }
                    }
                }
                // check if it only return Json...
                code.IsJson = Lib.Helper.JsonHelper.IsJson(code.Body);
            }

            if (model.Id != default(Guid))
            {
                var oldcode = sitedb.Code.Get(model.Id);
                if (oldcode != null)
                {
                    // check if needed to change route.
                    if (code.CodeType == Sites.Models.CodeType.Api)
                    {
                        var oldroute = sitedb.Routes.GetByObjectId(oldcode.Id);
                        if (oldroute != null && oldroute.Name != model.Url)
                        {
                            sitedb.Routes.ChangeRoute(oldroute.Name, model.Url);
                        }
                    }

                    oldcode.Name   = model.Name;
                    oldcode.Body   = model.Body;
                    oldcode.Config = model.Config;
                    oldcode.IsJson = Lib.Helper.JsonHelper.IsJson(oldcode.Body);

                    if (oldcode.IsEmbedded && oldcode.CodeType == Sites.Models.CodeType.PageScript)
                    {
                        sitedb.Code.AddOrUpdate(oldcode, true, true, call.Context.User.Id);
                    }
                    else
                    {
                        sitedb.Code.AddOrUpdate(oldcode, call.Context.User.Id);
                    }


                    code.EventType = oldcode.EventType;
                    code.CodeType  = oldcode.CodeType;
                }
            }
            else
            {
                //Api add route.
                if (code.CodeType == Sites.Models.CodeType.Api)
                {
                    // add a new route.
                    var route = new Kooboo.Sites.Routing.Route();
                    route.Name                 = model.Url;
                    route.objectId             = code.Id;
                    route.DestinationConstType = ConstObjectType.Code;
                    sitedb.Routes.AddOrUpdate(route);
                }

                sitedb.Code.AddOrUpdate(code);
            }

            return(code.Id);
        }