Beispiel #1
0
        public int RemoveFromCart(MusicAlbum musicAlbum)
        {
            var shoppingCartItem =
                _appDbContext.ShoppingCartItems.SingleOrDefault(
                    s => s.MusicAlbum.MusicAlbumId == musicAlbum.MusicAlbumId && s.ShoppingCartId == ShoppingCartId);

            var localAmount = 0;

            if (shoppingCartItem != null)
            {
                if (shoppingCartItem.Amount > 1)
                {
                    shoppingCartItem.Amount--;
                    localAmount = shoppingCartItem.Amount;
                }
                else
                {
                    _appDbContext.ShoppingCartItems.Remove(shoppingCartItem);
                }
            }

            _appDbContext.SaveChanges();

            return(localAmount);
        }
Beispiel #2
0
        private MusicAlbum GetCategoryByCategoryId(MusicAlbum musicAlbum)
        {
            Category tempCategory;

            switch (musicAlbum.Category.CategoryId)
            {
            case 1:
                tempCategory = new Category
                {
                    CategoryId = 1, CategoryName = "Electronica", Description = "Electronica Albums"
                };
                musicAlbum.Category   = tempCategory;
                musicAlbum.CategoryId = musicAlbum.Category.CategoryId;
                break;

            case 2:
                tempCategory = new Category
                {
                    CategoryId = 2, CategoryName = "Jazz", Description = "Jazz Albums"
                };
                musicAlbum.Category   = tempCategory;
                musicAlbum.CategoryId = musicAlbum.Category.CategoryId;
                break;

            case 3:
                tempCategory = new Category
                {
                    CategoryId = 3, CategoryName = "Rock", Description = "Rock Albums"
                };
                musicAlbum.Category   = tempCategory;
                musicAlbum.CategoryId = musicAlbum.Category.CategoryId;
                break;

            case 4:
                tempCategory = new Category
                {
                    CategoryId = 4, CategoryName = "Soul-Funk-Disco", Description = "Soul-Funk-Disco Albums"
                };
                musicAlbum.Category   = tempCategory;
                musicAlbum.CategoryId = musicAlbum.Category.CategoryId;
                break;

            default:
                Console.WriteLine($"An unexpected value for CategoryId ({musicAlbum.Category.CategoryId})");
                break;
            }
            return(musicAlbum);
        }
Beispiel #3
0
 public string AddMusicAlbum(MusicAlbum musicAlbum)
 {
     try
     {
         using (SqlConnection con = new SqlConnection(connectionString))
         {
             con.Open();
             musicAlbum = GetCategoryByCategoryId(musicAlbum);
             con.Execute(
                 "INSERT INTO MusicAlbums VALUES(@Name, @ShortDescription, @LongDescription, @Price, @ImageUrl, @ImageThumbnailUrl, @IsMusicAlbumOfTheWeek, @InStock, @CategoryId)",
                 musicAlbum);
             con.Close();
             return("Music album saved Successfully");
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine("Count not insert.");
         return(ex.Message.ToString());
     }
 }
Beispiel #4
0
        public void AddToCart(MusicAlbum musicAlbum, int amount)
        {
            var shoppingCartItem =
                _appDbContext.ShoppingCartItems.SingleOrDefault(
                    s => s.MusicAlbum.MusicAlbumId == musicAlbum.MusicAlbumId && s.ShoppingCartId == ShoppingCartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    MusicAlbum     = musicAlbum,
                    Amount         = 1
                };

                _appDbContext.ShoppingCartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Amount++;
            }
            _appDbContext.SaveChanges();
        }