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); }
//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(); } }
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); }
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)); }
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); }
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); }
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")); }
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")); }
public void AddComment(StoryResponse story, Comment comment) { //finish writing a fake version of this method }
public StoryResponse GetStoryByTitle(string title) { StoryResponse story = stories.Find(s => s.Title == title); return(story); }
public void AddStory(StoryResponse story) { stories.Add(story); }
//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(); }
public void AddStory(StoryResponse story) { context.Stories.Add(story); context.SaveChanges(); }