示例#1
0
        // GET: /Blog/Edit/5
        public ActionResult Edit(int?id)
        {
            var userId   = User.Identity.GetUserId <int>();
            var userName = User.Identity.GetUserName();
            var blog     = new Blog();

            if (id.HasValue)
            {
                blog = _blogService.Find(id.Value, userId);
            }
            if (id == null || blog == null)
            {
                blog = _blogService.FindDraftBlog(userId);
                if (blog == null)
                {
                    blog = new Blog()
                    {
                        UserId      = userId,
                        UserName    = userName,
                        Title       = "T",
                        Content     = string.Empty,
                        HtmlContent = string.Empty,
                        TimeStamp   = DateTime.Now,
                        Status      = BlogStatus.Draft,
                        Links       = new Collection <Link>()
                    };
                    _blogService.Add(blog);
                }
                blog.Title = string.Empty;
            }
            return(View(blog));
        }
        public async Task <IActionResult> Add(Blog blog)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                await _blogService.Add(blog.Url);

                return(RedirectToAction(actionName: nameof(Index)));
            }
        }
示例#3
0
 public IHttpActionResult Post(Blog blog)
 {
     blog.TimeStamp = DateTime.Now;
     blog.Status    = BlogStatus.Publish;
     _blogService.Add(blog);
     return(Ok(blog.Id));
 }
示例#4
0
        public ActionResult Create(BlogViewModel Cvm, HttpPostedFileBase imagePath)
        {
            var userId = User.Identity.GetUserId();


            int ide = Convert.ToInt32(userId);


            Blog c = new Blog();

            c.blogId     = Cvm.blogId;
            c.titreblog  = Cvm.titreblog;
            c.contenu    = Cvm.contenu;
            c.dateblog   = DateTime.Now;
            c.imagePath  = imagePath.FileName;
            c.userblogId = ide;


            Bs.Add(c);
            Bs.Commit();
            imagePath.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"), imagePath.FileName));
            try
            {
                // TODO: Add insert logic here

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
示例#5
0
 public ActionResult Add(VmBlogAdd model)
 {
     if (ModelState.IsValid)
     {
         var userid          = HttpContext.Request.GetOwinContext().Request.User.Identity.GetUserId();
         var loggedInDonorId = appDbContext.Users.FirstOrDefault(f => f.Id == userid).ReferrenceId ?? 0;
         #region Image Upload
         var uri = Request.Url.Host;
         System.IO.Directory.CreateDirectory(Server.MapPath("~/Images/Blog/" + loggedInDonorId + "/" + uri));
         string path = "";
         if (model.Image != null)
         {
             string pic          = System.IO.Path.GetFileName(model.Image.FileName);
             string physicalPath =
                 System.IO.Path.Combine(Server.MapPath("~/Images/Blog/" + loggedInDonorId + "/" + uri), pic);
             path = "/Images/Blog/" + loggedInDonorId + "/" + uri + "/" + pic;
             model.Image.SaveAs(physicalPath);
             model.ImageUrl = path;
         }
         #endregion
         BlogService blogService = new BlogService(db);
         int         blogId      = 0;
         blogService.Add(model, loggedInDonorId, out blogId);
     }
     //return Json(JsonRequestBehavior.AllowGet);
     return(RedirectToAction("Index", "Home"));
 }
        public void BlogInsertTest()
        {
            BlogService blogService = WindsorHelper.WindsorContainer.Resolve <BlogService>();
            string      searchTerm  = "https://example.com";

            BlogContext context = blogService.Context;

            try
            {
                // In-memory database exists only for the duration of an open connection
                context.Database.OpenConnection();
                context.Database.EnsureCreated();

                blogService.Add(searchTerm);

                // Will automatically use transaction
                context.SaveChanges();

                var searchResult = blogService.Find(searchTerm);
                Assert.AreEqual(1, context.Blogs.Count());
                Assert.AreEqual(searchTerm, searchResult.First().Url);
            }
            finally
            {
                context.Database.CloseConnection();
                WindsorHelper.WindsorContainer.Release(blogService);
            }
        }
        public void Add_writes_to_database()
        {
            var options = new DbContextOptionsBuilder <BloggingContext>()
                          .UseInMemoryDatabase(databaseName: "Add_writes_to_database")
                          .Options;

            // Seed the database
            using (var context = new BloggingContext(options))
            {
                context.Database.EnsureCreated();
            }

            // Run the test against one instance of the context
            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);
                service.Add("https://example.com");
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new BloggingContext(options))
            {
                // seeding contains 4 so total is now 5
                Assert.Equal(5, context.Blogs.Count());
                Assert.Equal("https://example.com",
                             context.Blogs.FirstOrDefault(b => b.BlogId == 5).Url);
            }
        }
示例#8
0
        public void Add_writes_to_database()
        {
            var options = new DbContextOptionsBuilder <BloggingContext>()
                          .UseInMemoryDatabase(databaseName: "Add_writes_to_database")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new BloggingContext(options))
            {
                context.Authors.Add(new Author()
                {
                    Id = 11, Name = "Ion"
                });
                var service = new BlogService(context);
                service.Add("https://example.com");
                context.SaveChanges();
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new BloggingContext(options))
            {
                Assert.Equal(1, context.Blogs.Count());
                Assert.Equal("https://example.com", context.Blogs.Single().Url);
                Assert.Equal(1, context.Authors.Count());
            }
        }
示例#9
0
        static void Main(string[] args)
        {
            var options = new DbContextOptionsBuilder <BloggingContext>()
                          .UseInMemoryDatabase(databaseName: "Add_writes_to_database")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);

                service.Add("http://sample.com");
                service.Add("http://github.com");
                service.Add("http://microsoft.com");
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);
                var blogs   = service.GetAll().ToList();

                Console.WriteLine($"Count:\t{blogs.Count}"); // Should be 3

                foreach (var blog in blogs)
                {
                    Console.WriteLine(blog);
                }

                Console.WriteLine("--------------------------------------------------------------------------------");
                var oneBlog = service.FindOne("http://github.com");
                if (oneBlog != null)
                {
                    Console.WriteLine("Found!");
                    Console.WriteLine(oneBlog);
                }
                else
                {
                    Console.WriteLine("Blog not found");
                }
            }
        }
        public void Delete_ShouldDelete()
        {
            var options = new DbContextOptionsBuilder <BloggingContext>()
                          .UseInMemoryDatabase(databaseName: "MAhDeleteDatabase").Options;

            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);
                service.Add("http://Hi.I.Am.A.New.Blog");
                service.Add("http://Hi.Ill.BeDeleted");
            }

            using (var context = new BloggingContext(options))
            {
                var service  = new BlogService(context);
                var toDelete = service.Find("Deleted").Single();
                context.Blogs.Remove(toDelete);
                context.Blogs.Count().Equals(1); //Yeeeeiii
            }
        }
示例#11
0
        public IActionResult PostOne([FromBody] Blog body)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _blogService.Add(body.Url);

            return(new OkObjectResult("saved to database"));
        }
示例#12
0
 public ActionResult Add(VmBlogAdd model)
 {
     if (ModelState.IsValid)
     {
         var         userid          = HttpContext.Request.GetOwinContext().Request.User.Identity.GetUserId();
         var         loggedInDonorId = appDbContext.Users.FirstOrDefault(f => f.Id == userid).ReferrenceId ?? 0;
         BlogService blogService     = new BlogService(db);
         int         blogId          = 0;
         blogService.Add(model, loggedInDonorId, out blogId);
     }
     return(Json(JsonRequestBehavior.AllowGet));
 }
        public async System.Threading.Tasks.Task AddNewRandomBlogAsync()
        {
            Logger.Here(l => l.Entering());

            var blogDto = await BlogService.Add(Guid.NewGuid().ToString());

            // Will automatically use transaction
            BlogService.Context.SaveChanges();

            this.StateHasChanged();

            Logger.Here(l => l.Exiting());
        }
示例#14
0
        public void Only_one_was_added()
        {
            var options = CreateNewContextOptions();

            using (var context = new BloggingContext(options))
            {
                var serivce = new BlogService(context);
                serivce.Add("http://sample.com");
            }

            using (var context = new BloggingContext(options))
            {
                Assert.AreEqual(1, context.Blogs.Count());
            }
        }
        public async System.Threading.Tasks.Task <BlogDto> HandleValidSubmitAsync()
        {
            Logger.Here(l => l.Entering(BlogDto));

            var blogDto = await BlogService.Add(BlogDto);

            // Will automatically use transaction
            BlogService.Context.SaveChanges();

            BlogDto = new BlogDto();

            this.StateHasChanged();

            Logger.Here(l => l.Exiting(blogDto));
            return(blogDto);
        }
示例#16
0
        public ActionResult Create(BlogViewModel bvm)
        {
            DateTime now = DateTime.Now;
            Blog     b   = new Blog();

            b.Contenu    = bvm.Contenu;
            b.Titre      = bvm.Titre;
            b.NbrComment = 0;
            b.NbrLike    = 0;
            b.DatePost   = now;
            b.Photo      = bvm.Photo;
            blogService.Add(b);
            blogService.Commit();

            return(RedirectToAction("Index"));
        }
示例#17
0
        public void Add_writes_to_database()
        {
            var options = CreateNewContextOptions();

            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);
                service.Add("http://sample.com");
            }

            using (var context = new BloggingContext(options))
            {
                Assert.AreEqual(1, context.Blogs.Count());
                Assert.AreEqual("http://sample.com", context.Blogs.Single().Url);
            }
        }
        public void Add_writes_to_database()
        {
            // In-memory database only exists while the connection is open
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <BloggingContext>()
                              .UseSqlite(connection)
                              .Options;

                // Create the schema in the database
                using (var context = new BloggingContext(options))
                {
                    context.Database.EnsureCreated();
                    context.Authors.Add(new Author()
                    {
                        Id = 11, Name = "Ion"
                    });
                }

                // Run the test against one instance of the context
                using (var context = new BloggingContext(options))
                {
                    var service = new BlogService(context);
                    service.Add("https://example.com");
                    context.SaveChanges();
                }

                // Use a separate instance of the context to verify correct data was saved to database
                using (var context = new BloggingContext(options))
                {
                    Assert.Equal(1, context.Blogs.Count());
                    Assert.Equal("https://example.com", context.Blogs.Single().Url);
                    Assert.Equal(1, context.Authors.Count());
                }
            }
            finally
            {
                connection.Close();
            }
        }
        public void Add_WriterToDB()
        {
            var options = new DbContextOptionsBuilder <BloggingContext>()
                          .UseInMemoryDatabase(databaseName: "Add_WritesToDB")
                          .Options;

            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);
                service.Add("http://sample.com");
                // context.SaveChanges();
            }

            using (var context = new BloggingContext(options))
            {
                Assert.Equal(1, context.Blogs.Count());
                Assert.Equal("http://sample.com", context.Blogs.Single().Url);
            }
        }
        public void Add_writes_to_database()
        {
            // All contexts that share the same service provider will share the same InMemory database
            var options = CreateNewContextOptions();

            // Run the test against one instance of the context
            using (var context = new BloggingContext(options))
            {
                var service = new BlogService(context);
                service.Add("http://sample.com");
            }

            // Use a separate instance of the context to verify correct data was saved to database
            using (var context = new BloggingContext(options))
            {
                Assert.AreEqual(1, context.Blogs.Count());
                Assert.AreEqual("http://sample.com", context.Blogs.Single().Url);
            }
        }
        public void Add_writes_to_database()
        {
            // All contexts created from this service provider will access the same InMemory database
            var serviceProvider = _serviceCollection.BuildServiceProvider();

            // Run the test against one instance of the context
            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var context = scope.ServiceProvider.GetService <BloggingContext>();
                var service = new BlogService(context);
                service.Add("http://sample.com");
            }

            // User a seperate instance of the context to verify correct data was saved to database
            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var context = scope.ServiceProvider.GetService <BloggingContext>();
                Assert.AreEqual(1, context.Blogs.Count());
                Assert.AreEqual("http://sample.com", context.Blogs.Single().Url);
            }
        }
        public void Add_writes_to_database()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = new DbContextOptionsBuilder <BloggingContext>()
                              .UseSqlite(connection)
                              .Options;

                // Create the schema in the database
                using (var context = new BloggingContext(options))
                {
                    context.Database.EnsureCreated();
                }

                // Run the test against one instance of the context
                using (var context = new BloggingContext(options))
                {
                    var service = new BlogService(context);
                    service.Add("https://example.com");
                    context.SaveChanges();
                }

                // Use a separate instance of the context to verify correct data was saved to database
                using (var context = new BloggingContext(options))
                {
                    // seeding contains 4 so total is now 5
                    Assert.Equal(5, context.Blogs.Count());
                    Assert.Equal("https://example.com",
                                 context.Blogs.FirstOrDefault(b => b.BlogId == 5).Url);
                }
            }
            finally
            {
                connection.Close();
            }
        }
示例#23
0
        public void Add_writes_to_database()
        {
            // In-memory database only exists while the connection is open
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            try
            {
                var options = GetOptions(connection);

                // Create the schema in the database
                using (var context = new BloggingContext(options))
                {
                    context.Database.EnsureCreated();
                }

                // Run the test against one instance of the context
                using (var context = new BloggingContext(options))
                {
                    var service = new BlogService(context);
                    service.Add("http://sample.com");
                }

                // Use a separate instance of the context to verify correct data was saved to database
                using (var context = new BloggingContext(options))
                {
                    Assert.AreEqual(1, context.Blogs.Count());
                    Assert.AreEqual("http://sample.com", context.Blogs.Single().Url);
                }
            }
            finally
            {
                connection.Close();
            }
        }
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            using (var db = new BloggingContext())
            {
                try
                {
                    db.Database.Migrate();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An error occurred migrating the DB.");
                    Console.WriteLine(ex);
                }
            }

            using (var db = new BloggingContext())
            {
                var blogs = await db.Blogs.ToListAsync();

                var posts = await db.Posts.ToListAsync();

                db.Blogs.RemoveRange(blogs);
                db.Posts.RemoveRange(posts);

                await db.SaveChangesAsync();
            }

            var blogId = 0;

            using (var db = new BloggingContext())
            {
                var blogService = new BlogService(db);

                blogService.Add("http://blogs.msdn.com/adonet");

                blogId = blogService
                         .Find(string.Empty)
                         .Single()
                         .BlogId;

                Console.WriteLine("{0} records saved to database", 1);
            }

            using (var db = new BloggingContext())
            {
                var postService = new PostService(db);

                postService.Add(blogId, "Hello world!");

                Console.WriteLine("{0} records saved to database", 1);
            }

            Console.WriteLine();

            using (var db = new BloggingContext())
            {
                var blogService = new BlogService(db);

                var blogs = blogService.Find(string.Empty);

                Console.WriteLine("All blogs in database:");

                foreach (var blog in blogs)
                {
                    Console.WriteLine(" - {0}", blog.Url);
                    Console.WriteLine("   - {0}", blog.Posts.First().Title);
                }
            }
        }
 public Blog Post([FromBody] Blog blog)
 {
     return(_blogService.Add(blog));
 }