public void Add_SavesToDatabase() { var options = new DbContextOptionsBuilder <GameInfoContext>() .UseInMemoryDatabase(databaseName: "AddGuide_ToDb") .Options; using (var context = new GameInfoContext(options)) { var guideToAdd = new AddGuideInputModel() { GuideTitle = "Guide", GuideContent = "Content" }; var user = new GameInfoUser() { UserName = "******" }; var service = new GuidesService(context); service.Add(guideToAdd, user); var expectedGuide = new Guide() { Title = guideToAdd.GuideTitle, Content = guideToAdd.GuideContent }; Assert.NotEmpty(context.Guides); Assert.Equal(expectedGuide.Title, context.Guides.First().Title); Assert.Equal(expectedGuide.Content, context.Guides.First().Content); } }
public void Add(AddGuideInputModel model, GameInfoUser currentUser) { var guide = new Guide { Title = model.GuideTitle, Content = model.GuideContent }; guide.Creator = currentUser; this._db.Guides.Add(guide); this._db.SaveChanges(); }
public async Task <IActionResult> Add(AddGuideInputModel inputModel) { if (!ModelState.IsValid) { return(View()); } if (inputModel.GuideContent.Contains('<') || inputModel.GuideContent.Contains('>')) { return(View()); } var currentUser = await _userManager.GetUserAsync(User); _guidesService.Add(inputModel, currentUser); return(Redirect(Guides_Root_Path)); }