Example #1
0
        public void DeleteShouldDeleteFavoriteProduct()
        {
            var options = new DbContextOptionsBuilder <XeonDbContext>()
                          .UseInMemoryDatabase(databaseName: "Delete_Favorites_Database")
                          .Options;
            var dbContext = new XeonDbContext(options);

            var username = "******";

            dbContext.Users.Add(new XeonUser {
                UserName = username
            });


            var productNameForDelete = "USB";
            var products             = new List <Product>
            {
                new Product {
                    Name = productNameForDelete
                },
                new Product {
                    Name = "Phone Samsung"
                },
                new Product {
                    Name = "Phone Nokia"
                },
                new Product {
                    Name = "Phone Iphone"
                },
                new Product {
                    Name = "Tablet Galaxy"
                }
            };

            dbContext.Products.AddRange(products);
            dbContext.SaveChanges();

            var favoriteService = new FavoritesService(dbContext);

            foreach (var product in products.Take(3))
            {
                favoriteService.Add(product.Id, username);
            }

            var productId = products.FirstOrDefault(x => x.Name == productNameForDelete).Id;

            favoriteService.Delete(productId, username);

            var userFavoriteProduxts = dbContext.Users
                                       .FirstOrDefault(x => x.UserName == username)
                                       .FavoriteProducts
                                       .ToList();

            var isProductExist = userFavoriteProduxts.Any(x => x.Product.Name == productNameForDelete);

            Assert.Equal(2, userFavoriteProduxts.Count());
            Assert.False(isProductExist);
        }
Example #2
0
        public IHttpActionResult DeleteFavorite(int id)
        {
            FavoriteDTO favorite = _favoritesService.GetById(id);

            if (favorite == null)
            {
                return(NotFound());
            }

            _favoritesService.Delete(id);

            return(Ok(favorite));
        }
Example #3
0
 //删除
 public JsonResult Delete(Enums.FavoritesType type, long contentId)
 {
     serviceFavorites.Delete(currentUser.UserId, type, contentId);
     result.code = 1;
     return(Json(result));
 }