Esempio n. 1
0
 public ActionResult Copy(Template template)
 {
     if (template != null && template.Id != 0)
     {
         template.ParentId = template.Id;
         template.Id = 0;
         TempData[TemplateKey] = template;
     }
     return RedirectToAction("Index", new { id = "" });
 }
Esempio n. 2
0
        public ActionResult ExecuteCompleted(Template template)
        {
            TempData[TemplateKey] = template;

            // ちょっとキモイね。
            // Ajaxでしか受け付けないんだろ?みたいなね。
            if (Request.IsAjaxRequest())
                return View("~/Views/Home/_Response.cshtml", template);

            return RedirectToAction("Index", new { id = "" });
        }
Esempio n. 3
0
 public Template(Template original)
     : this()
 {
     ModelCopier.CopyModel(original, this);
     //Id = original.Id;
     //ParentId = original.ParentId;
     Title = original.Title ?? "Razor do it!";
     //TagsValue = original.TagsValue;
     //Razor = original.Razor;
     //UrlKey = original.UrlKey;
     //CreateAt = original.CreateAt;
     //Account = original.Account;
 }
Esempio n. 4
0
        public ActionResult Execute(Template template)
        {
            var store = DependencyResolver.Current.GetService<ITemplateStore>();

            var path = string.Format("~/{0}/{1}.{2}",
                TemplateVirtualPathProvider.RootPath,
                Guid.NewGuid().ToString("n"),
                template.PageName);
            var virtualPath = VirtualPathUtility.ToAbsolute(path);

            store.Save(virtualPath, template.Razor);

            return new TemplateResult { VirtualPath = path };
        }
Esempio n. 5
0
        public void ExecuteAsync(Template template)
        {
            AsyncManager.OutstandingOperations.Increment();
            var client = new WebClient { Encoding = Encoding.UTF8 };
            client.UploadValuesCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    template.Response = Encoding.UTF8.GetString(e.Result);
                }
                else
                {
                    var exception = (WebException)e.Error;
                    var responseStream = exception.Response.GetResponseStream();
                    if (responseStream != null)
                        template.Response = new StreamReader(responseStream, Encoding.UTF8).ReadToEnd();
                }
                AsyncManager.Parameters["template"] = template;

                AsyncManager.OutstandingOperations.Decrement();
            };

            // SandboxにTemplateをPOSTしてビルド&実行!
            // ※なのでTemplate内でIsPostを見ると常にtrue...
            var uri = new Uri(ConfigurationManager.AppSettings["SandboxUri"]);
            var values = new NameValueCollection();
            var extensions = new Dictionary<string, string>
                                 {
                                     {"cshtml", "csdoit"},
                                     {"vbhtml", "vbdoit"}
                                 };
            var pageName = "default." + extensions[template.Language ?? "cshtml"];
            values["Razor"] = template.Razor;
            values["PageName"] = pageName;
            client.UploadValuesAsync(uri, "POST", values);
        }
Esempio n. 6
0
        public ActionResult Save(Template template)
        {
            if (ModelState.IsValid)
            {
                var key = _siteService.SaveTemplate(User.Identity.Name, template);
                TempData.Remove(TemplateKey);
                return RedirectToRoute("Permalink", new { key });
            }

            TempData[TemplateKey] = template;

            return RedirectToAction("Index", new { id = "" });
        }
Esempio n. 7
0
        public ActionResult Index(string key)
        {
            var template = _siteService.GetTemplate(key);

            if (!string.IsNullOrEmpty(key) && template.Id == 0)
                return RedirectToAction("Index", new { id = "" });

            var model = new Template(template);
            if (string.IsNullOrEmpty(model.Razor))
                model.Razor = GetDefaultTemplate();

            if (template.Id != 0)
            {
                ViewBag.Title = template.Title + " - Razor Do It";
            }
            else
            {
                var prev = TempData[TemplateKey] as Template;
                if (prev != null)
                    model = prev;
            }
            return View(model);
        }
Esempio n. 8
0
        public string SaveTemplate(string userName, Template template)
        {
            var account = GetAccount(userName);
            using (var ts = new TransactionScope())
            {
                var genLength = 4;
                while (string.IsNullOrEmpty(template.UrlKey))
                {
                    var key = KeyGenerator.Generate(genLength++);
                    if (GetTemplate(key).Id == 0)
                    {
                        template.UrlKey = key;
                    }
                }

                // Accountはログインで追加されてるはず。
                template.Account = account;

                // Templateはここで保存
                // ※なかでTagの保存もやってるよ。
                foreach (var tag in (template.TagsValue ?? "").Split(' ')
                                                              .Select(t => t.Trim())
                                                              .Distinct()
                                                              .Where(t => !string.IsNullOrEmpty(t)))
                {
                    template.Tags.Add(new Tag { Keyword = tag });
                }

                _repository.SaveTemplate(template);
                _repository.SaveChanges();

                ts.Complete();
            }

            return template.UrlKey;
        }
Esempio n. 9
0
        public Template SaveTemplate(Template template)
        {
            var now = DateTime.Now;
            template.UpdateAt = now;
            var existTemplate = GetTemplate(template.Id);
            if (existTemplate != null)
            {
                var entry = _db.Entry(existTemplate);
                entry.OriginalValues.SetValues(existTemplate);
                entry.CurrentValues.SetValues(template);

                // 今持ってるTagを一旦全部消す
                existTemplate.Tags.Clear();
            }
            else
            {
                template.CreateAt = now;
                _db.Templates.Add(template);
            }

            foreach (var tag in template.Tags)
            {
                tag.Templates.Add(template);
                _db.Tags.Add(tag);
            }

            return template;
        }