public async Task <string> AddRecipeAsync(AddRecipeInputViewModel addRecipeViewModel, string userId) { var recipe = this.mapper.Map <Recipe>(addRecipeViewModel); recipe.CreatorId = userId; await this.db.Recipes.AddAsync(recipe); await this.db.SaveChangesAsync(); return(recipe.Id); }
public async Task <IActionResult> Add(AddRecipeInputViewModel addRecipeViewModel) { ; if (!this.ModelState.IsValid) { return(this.View(addRecipeViewModel)); } var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; var recipeId = await this.recipeService.AddRecipeAsync(addRecipeViewModel, userId); this.TempData["Success"] = "True"; return(this.Redirect($"/Recipes/Details/{recipeId}")); }
public async Task RemoveRecipeFromUserCollectionShouldDeleteRecipe() { //Arange var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; //Act var db = new ApplicationDbContext(options); var config = new MapperConfiguration(cfg => { cfg.AddProfile <ApplicationProfile>(); }); var mapper = new Mapper(config); var recipeService = new RecipeService(db, mapper); var user = new ApplicationUser { UserName = "******", Email = "*****@*****.**" }; db.Users.Add(user); await db.SaveChangesAsync(); var recipeModel = new AddRecipeInputViewModel { CookingTime = 5, ImageUrl = "URL123123", Instructions = "instructions", Name = "Name", ProductType = ProductType.Salad }; var recipeModel2 = new AddRecipeInputViewModel { CookingTime = 5, ImageUrl = "URL123123", Instructions = "instructions", Name = "Name", ProductType = ProductType.Salad }; var id = await recipeService.AddRecipeAsync(recipeModel, user.Id); var id2 = await recipeService.AddRecipeAsync(recipeModel2, user.Id); await recipeService.RemoveRecipeFromUserCollection(id, user.Id); var expected = db.Users.FirstOrDefault(X => X.Id == user.Id).Recipes.Count(); //assert Assert.True(expected == 1); }
public async Task ApplyEditRecipeShoudWorkCorectly() { //Arange var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; //Act var db = new ApplicationDbContext(options); var config = new MapperConfiguration(cfg => { cfg.AddProfile <ApplicationProfile>(); }); var mapper = new Mapper(config); var recipeService = new RecipeService(db, mapper); var user = new ApplicationUser { UserName = "******", Email = "*****@*****.**" }; db.Users.Add(user); await db.SaveChangesAsync(); var recipeModel = new AddRecipeInputViewModel { CookingTime = 5, ImageUrl = "URL123123", Instructions = "instructions", Name = "Name", ProductType = ProductType.Salad }; var id = await recipeService.AddRecipeAsync(recipeModel, user.Id); var editModel = db.Recipes.Where(x => x.Id == id) .Select(x => new RecipeEditViewModel { Id = x.Id, CookingTime = x.CookingTime, ImageUrl = x.ImageUrl, Instructions = x.Instructions, Name = x.Name, }).FirstOrDefault(); editModel.Name = "Name2"; await recipeService.ApplyEditRecipe(editModel); //assert Assert.True(recipeModel.Name != editModel.Name); }
public async Task GetUserRecipesSholdWorkCorrectlyWithValidData() { //Arange var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; //Act var db = new ApplicationDbContext(options); var config = new MapperConfiguration(cfg => { cfg.AddProfile <ApplicationProfile>(); }); var mapper = new Mapper(config); var recipeService = new RecipeService(db, mapper); var user = new ApplicationUser { UserName = "******", Email = "*****@*****.**" }; db.Users.Add(user); await db.SaveChangesAsync(); var recipeModel = new AddRecipeInputViewModel { CookingTime = 5, ImageUrl = "URL123123", Instructions = "instructions", Name = "Name", ProductType = ProductType.Salad }; var recipeModel2 = new AddRecipeInputViewModel { CookingTime = 5, ImageUrl = "URL123122", Instructions = "instructions2", Name = "Name", ProductType = ProductType.Salad }; var id = await recipeService.AddRecipeAsync(recipeModel, user.Id); var id2 = await recipeService.AddRecipeAsync(recipeModel2, user.Id); var recipeModels = recipeService.GetUserRecipes(1, 2, user.Id); //assert Assert.True(recipeModels.GetAwaiter().GetResult().Count == 2); }
public async Task GetRecipeForEditShouldShouldReturnCorrectRecipe() { //Arange var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; //Act var db = new ApplicationDbContext(options); var config = new MapperConfiguration(cfg => { cfg.AddProfile <ApplicationProfile>(); }); var mapper = new Mapper(config); var recipeService = new RecipeService(db, mapper); var user = new ApplicationUser { UserName = "******", Email = "*****@*****.**" }; db.Users.Add(user); await db.SaveChangesAsync(); var recipeModel = new AddRecipeInputViewModel { CookingTime = 5, ImageUrl = "URL123123", Instructions = "instructions", Name = "Name", ProductType = ProductType.Salad }; var id = await recipeService.AddRecipeAsync(recipeModel, user.Id); var recipe = recipeService.GetRecipeForEdit(id); //assert Assert.Equal(recipe.Id, id); Assert.NotNull(recipe); }
public IActionResult Add() { var model = new AddRecipeInputViewModel(); return(this.View(model)); }