public ActionResult PutBlogs(int id, Blogs blogs)
        {
            if (id != blogs.Id)
            {
                return(BadRequest());
            }

            _context.Entry(blogs).State = EntityState.Modified;

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BlogsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #2
0
        public void SetUpTests()
        {
            var rnd = new Random();

            _options = new DbContextOptionsBuilder <BlogsDbContext>()
                       .UseInMemoryDatabase(databaseName: "openreferralsTests")
                       .Options;
            _blogs = Builder <Blog> .CreateListOfSize(100)
                     .All()
                     .With(p => p.Title   = Faker.Lorem.Sentence())
                     .With(p => p.Content = Faker.Lorem.Paragraph())
                     .With(p => p.Active  = true)
                     .With(p => p.UserId  = 1)
                     .Build();

            var user = Builder <User> .CreateNew().With(p => p.Active = true).With(p => p.FirsName = Faker.Name.First())
                       .With(p => p.Id       = 1)
                       .With(p => p.LastName = Faker.Name.Last())
                       .With(p => p.Username = Faker.Internet.UserName())
                       .With(p => p.Password = "******")
                       .Build()
            ;

            _blogs = Builder <Blog> .CreateListOfSize(100)
                     .All()
                     .With(p => p.Title   = Faker.Lorem.Sentence())
                     .With(p => p.Content = Faker.Lorem.Paragraph())
                     .With(p => p.Active  = true)
                     .With(p => p.UserId  = 1)
                     .With(p => p.User    = user)
                     .Build();

            // Insert seed data into the database using one instance of the context
            using (var context = new BlogsDbContext(_options))
            {
                context.Database.EnsureDeleted();

                foreach (var blog in _blogs)
                {
                    try
                    {
                        context.Blogs.Add(blog);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
                }
                context.SaveChanges();
            }
        }
예제 #3
0
        public void SetUpTests()
        {
            var rnd = new Random();

            _options = new DbContextOptionsBuilder <BlogsDbContext>()
                       .UseInMemoryDatabase(databaseName: "openreferralsTests")
                       .Options;
            _users = Builder <User> .CreateListOfSize(100)
                     .All()
                     .With(p => p.LastName = Faker.Name.Last())
                     .With(p => p.Username = Faker.Internet.UserName())
                     .With(p => p.Password = "******")
                     .Build();

            //foreach (var user in _users)
            //{
            //    user.Blogs = CreateBlogList(user);
            //}

            // Insert seed data into the database using one instance of the context
            using (var context = new BlogsDbContext(_options))
            {
                context.Database.EnsureDeleted();

                foreach (var user in _users)
                {
                    try
                    {
                        context.Users.Add(user);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
                }

                context.SaveChanges();
            }
        }
 public void CreatePost([ModelBinder] Blog blog)
 {
     context.Blogs.Add(blog);
     context.SaveChanges();
 }