Example #1
0
        public void AddStoryTest()
        {
            //arrange
            StoryResponse story = new StoryResponse()
            {
                Title = "Test title",
                Date  = "test date",
                Text  = "test text"
            };

            story.Author = new User()
            {
                Username = "******"
            };
            var repo2 = new FakeStoryRepository();

            //act
            repo2.AddStory(story);

            //assert, could add a comparer to simply this assertion to one line
            Assert.Equal("Test title", repo2.Stories.Last().Title);
            Assert.Equal("test date", repo2.Stories.Last().Date);
            Assert.Equal("test text", repo2.Stories.Last().Text);
            Assert.Equal("Mr. Test", repo2.Stories.Last().Author.Username);
        }
Example #2
0
        //setup on DB connection
        //Service Provider connnects Entity Framework to whatever Database the app uses
        //Have to use IApplicationBuilder in order to call this class in Startup.cs in the Configure method
        public static void Seed(IApplicationBuilder app)
        {
            AppDbContext context = app.ApplicationServices.GetRequiredService <AppDbContext>();

            context.Database.EnsureCreated();

            //If there are no stories in the DB, we'll add some
            if (!context.Stories.Any())
            {
                //create a user
                User author = new User {
                    Username = "******"
                };
                context.Users.Add(author);

                //create a comment by that user
                Comment comment = new Comment {
                    Commenter = author, CommentText = "I love this story"
                };
                context.Comments.Add(comment);

                //create a story by that user, add that user's comment to the story
                StoryResponse story = new StoryResponse {
                    Title  = "A tale of Seeds",
                    Author = author,
                    Date   = "beginning of time",
                    Text   = "There once lived a seed, that grew to populate a database"
                };
                story.Comments.Add(comment);
                context.Stories.Add(story);

                //save all the added seed data to the database
                context.SaveChanges();
            }
        }
Example #3
0
        public StoryResponse GetStoryByTitle(string title)
        {
            //couldn't I just use Stories.Find() instead of context?
            //nevermind, gotta use the context for talking to the database
            StoryResponse story = context.Stories.First(s => s.Title == title);

            return(story);
        }
Example #4
0
        public async Task <ActionResult <StoryResponse> > Get([FromQuery] StoryParameters storyParameters)
        {
            StoryResponse response = new StoryResponse();
            var           result   = await _storyRep.GetStories(storyParameters);

            response.Stories    = result;
            response.Parameters = storyParameters;

            return(Ok(response));
        }
Example #5
0
        public void GetStoryByTitleTest()
        {
            //arrange
            var repo4 = new FakeStoryRepository();

            //act
            StoryResponse fetchedStory = repo4.GetStoryByTitle("Ghandi goes on hunger strike");

            //assert, checking a random property of the fetchedStory is equal to that same property in the test data
            Assert.Equal("April 9th, 1908", fetchedStory.Date);
        }
Example #6
0
        void AddTestData()
        {
            StoryResponse story = new StoryResponse()
            {
                Title = "Ghandi goes on hunger strike",
                Date  = "April 9th, 1908",
                Text  = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet pulvinar lorem."
            };

            story.Author = new User()
            {
                Username = "******"
            };
            stories.Add(story);
        }
Example #7
0
        public RedirectToActionResult AddComment(string title, string commentText, string commenter)
        {
            StoryResponse story = repo.GetStoryByTitle(title);

            repo.AddComment(story, new Comment()
            {
                Commenter = new User()
                {
                    Username = commenter
                },
                CommentText = commentText
            });
            ViewBag.newestCommenter = "Thanks for the comment!";
            return(RedirectToAction("UserStories"));
        }
Example #8
0
        public RedirectToActionResult Stories(string title, string author, string date, string text)
        {
            StoryResponse story = new StoryResponse();

            story.Title  = title;
            story.Date   = date; //for now Date is a string
            story.Text   = text;
            story.Author = new User()
            {
                Username = author
            };
            ViewBag.newestStory = title;
            repo.AddStory(story);
            return(RedirectToAction("UserStories"));
        }
Example #9
0
 public void AddComment(StoryResponse story, Comment comment)
 {
     //finish writing a fake version of this method
 }
Example #10
0
        public StoryResponse GetStoryByTitle(string title)
        {
            StoryResponse story = stories.Find(s => s.Title == title);

            return(story);
        }
Example #11
0
 public void AddStory(StoryResponse story)
 {
     stories.Add(story);
 }
Example #12
0
 //need to add the comment to the Comments context, otherwise it won't show up in the database
 public void AddComment(StoryResponse story, Comment comment)
 {
     story.Comments.Add(comment);
     context.Stories.Update(story);
     context.SaveChanges();
 }
Example #13
0
 public void AddStory(StoryResponse story)
 {
     context.Stories.Add(story);
     context.SaveChanges();
 }