public void save_should_save_to_repository_and_return_id()
        {
            var template = new Template();
            var username = "******";
            var mockRepo = new Mock<ITemplateRepository>();
            mockRepo.Expect(r => r.Save(username, "name", "content")).Returns(1);

            ITemplateService service = new InMemoryTemplateService(mockRepo.Object);
            var id = service.Save(username, "name", "content");

            mockRepo.VerifyAll();
            Assert.AreEqual(1, id);
        }
        public void save_saves_user_template()
        {
            var template = new Template();
            var username = "******";
            var mockSer = new Mock<ITemplateService>();
            mockSer.Expect(s => s.Save(username, "name", "content")).Returns(1);
            var con = new TemplateController(mockSer.Object);
            con.SetFakeControllerContext();

            var result = con.Save("name", "content");

            mockSer.VerifyAll();
            Assert.IsInstanceOfType(typeof(JsonResult), result);
            var jsonResult = result as JsonResult;
            Assert.IsInstanceOfType(typeof(JsonData), jsonResult.Data);
            Assert.IsTrue(((JsonData)jsonResult.Data).success);
            Assert.AreEqual(1, ((JsonData)jsonResult.Data).id);
        }
        public int Save(string username, string name, string content)
        {
            if (!Directory.Exists(HttpContext.Current.Server.MapPath(string.Format("~/content/Templates/{0}", username))))
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(string.Format("~/content/Templates/{0}", username)));
            }

            using (TextWriter tw = new StreamWriter(HttpContext.Current.Server.MapPath(string.Format("~/content/Templates/{0}/{1}", username, name))))
            {
                tw.Write(HttpUtility.HtmlDecode(content));
                tw.Close();
            }

            var template = new Template
                               {
                                   Id = AutoId,
                                   Path = string.Format("/content/Templates/{0}/{1}", username, name),
                                   Thumbnail = "/content/templates/images/default.jpg",
                                   Username = username
                               };
            Templates.Add(template);
            return template.Id;
        }