コード例 #1
0
ファイル: TemplatesModule.cs プロジェクト: synhershko/NSemble
        public TemplatesModule(IDocumentSession session, IViewLocator viewLocator)
            : base("Templates")
        {
            Get["/"] = p =>
                           {
                               var templates = session.Advanced.LoadStartingWith<ViewTemplate>("NSemble/Views/");
                               return View["List", templates];
                           };

            Get["/new/"] = p => View["Edit", new ViewTemplate {}];

            Get[@"/edit/{viewName*}"] = p =>
                                            {
                                                var viewName = (string) p.viewName;
                                                if (!viewName.StartsWith(Constants.RavenViewDocumentPrefix, StringComparison.InvariantCultureIgnoreCase))
                                                    viewName = Constants.RavenViewDocumentPrefix + viewName;
                                                var template = session.Load<ViewTemplate>(viewName);

                                                // Even if we don't have it stored in the DB, it might still exist as a resource. Try loading it from Nancy.
                                                if (template == null)
                                                {
                                                    var vlr = viewLocator.LocateView(viewName.Substring(Constants.RavenViewDocumentPrefix.Length), Context);
                                                    if (vlr == null)
                                                        return 404;

                                                    template = new ViewTemplate
                                                                   {
                                                                       Location = vlr.Location,
                                                                       Name = vlr.Name,
                                                                       Extension = vlr.Extension,
                                                                       Contents = vlr.Contents.Invoke().ReadToEnd(),
                                                                   };
                                                }

                                                return View["Edit", template];
                                            };

            Post[@"/edit/{viewName*}"] = p =>
                                                   {
                                                       var template = this.Bind<ViewTemplate>();

                                                       var viewName = (string) p.viewName;
                                                       if (!viewName.StartsWith(Constants.RavenViewDocumentPrefix, StringComparison.InvariantCultureIgnoreCase))
                                                           viewName = Constants.RavenViewDocumentPrefix + viewName;

                                                       session.Store(template, string.Concat(Constants.RavenViewDocumentPrefix, template.Location, "/", template.Name, ".", template.Extension));
                                                       session.SaveChanges();

                                                       return "Success";
                                                   };

            Post["/new"] = p =>
                                {
                                    var template = this.Bind<ViewTemplate>();
                                    session.Store(template, string.Concat(Constants.RavenViewDocumentPrefix, template.Location, "/", template.Name, ".", template.Extension));

                                    return Response.AsRedirect("/");
                                };
        }
コード例 #2
0
ファイル: TemplatesModule.cs プロジェクト: jchannon/NSemble
        public TemplatesModule(IDocumentSession session, IViewLocator viewLocator)
            : base("Templates")
        {
            Get["/"] = p =>
                           {
                               var templates = session.Query<ViewTemplate>().ToArray();

                               return View["List", templates];
                           };

            Get["/new/"] = p => View["Edit", new ViewTemplate {}];

            Get[@"/edit/(?<viewName>\S+)/"] = p =>
                                                  {
                                                      var viewName = (string) p.viewName;
                                                var template = session.Load<ViewTemplate>(viewName);

                                                // Even if we don't have it stored in the DB, it might still exist as a resource. Try loading it from Nancy.
                                                if (template == null)
                                                {
                                                    ViewLocationResult vlr = viewLocator.LocateView(viewName, Context);
                                                    if (vlr == null)
                                                        return 404;

                                                    template = new ViewTemplate
                                                                   {
                                                                       Location = vlr.Location,
                                                                       Name = vlr.Name,
                                                                       Extension = vlr.Extension,
                                                                       Contents = vlr.Contents.Invoke().ReadToEnd(),
                                                                   };
                                                }

                                                return View["Edit", template];
                                            };

            Post["/new/"] = p =>
                                {
                                    var template = this.Bind<ViewTemplate>();
                                    session.Store(template, string.Concat(Constants.RavenViewDocumentPrefix, template.Location, "/", template.Name, ".", template.Extension));

                                    return Response.AsRedirect("/");
                                };
        }