Esempio n. 1
0
        //system.data.entity.infrastructure.dbupdateexception savechanges
        public ActionResult DeleteConfirmed(short id)
        {
            Order order = db.Orders.Find(id);
            // remove the songs from person
            AppUser owner = db.Users.Find(order.RecipientID);

            foreach (SongOrderBridge song in order.SongsInOrder)
            {
                Song songToRemove = db.Songs.Find(song.SongInOrder.SongID);
                owner.Songs.Remove(songToRemove);
            }
            // remove the albums from person

            foreach (AlbumOrderBridge album in order.AlbumsInOrder)
            {
                Album albumToRemove = db.Albums.Find(album.AlbumInOrder.AlbumID);
                owner.Albums.Remove(albumToRemove);
            }

            if (order.IsGift == false)
            {
                // send a new email to the recipient and the user who just placed the order
                EmailController.Refund(order.Customer);
            }
            if (order.IsGift == true)
            {
                // send a new email to the recipient and the user who just placed the order
                EmailController.RefundGift(order.Customer, owner);
            }

            // remove the song order bridge from bridge table
            order.SongsInOrder.Clear();
            order.AlbumsInOrder.Clear();

            db.Orders.Remove(order);


            db.SaveChanges();

            return(RedirectToAction("Index"));
        }
Esempio n. 2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //TODO: Add fields to user here so they will be saved to the database
                //Create a new user with all the properties you need for the class
                var user = new AppUser {
                    UserName = model.Email, Email = model.Email, FName = model.FName, MName = model.MName, LName = model.LName, StreetAddress = model.StreetAddress, City = model.City, State = model.State, ZipCode = model.ZipCode, PhoneNumber = model.PhoneNumber, IsAccountEnabled = model.IsAccountEnabled
                };


                db.SaveChanges();

                //Add the new user to the database
                var result = await UserManager.CreateAsync(user, model.Password);

                //TODO: Once you get roles working, you may want to add users to roles upon creation
                //await UserManager.AddToRoleAsync(user.Id, "User"); //adds user to role called "User"
                // --OR--

                if (result.Succeeded) //user was created successfully
                {
                    //sign the user in
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    await UserManager.AddToRoleAsync(user.Id, "Customer"); //adds user to role called "Customer"

                    //Send confirmation email
                    EmailController.AccountCreation(user);

                    //send them to the page to add their credit cards
                    return(RedirectToAction("CustomerDashboard", "Account"));
                }

                //if there was a problem, add the error messages to what we will display
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 3
0
        public ActionResult GiftCheckout(string FriendEmail, int?SelectedCreditCardID)
        {
            AppUser userLoggedIn = db.Users.Find(User.Identity.GetUserId());
            AppUser friend       = db.Users.First(a => a.Email == FriendEmail);

            if (SelectedCreditCardID == null)
            {
                return(RedirectToAction("CheckoutPage", new { ErrorMessage = "Looks like you forgot to select a credit card. If you don't have a credit card, make sure to add one!" }));
            }

            if (DuplicatesExist())
            {
                return(RedirectToAction("ShoppingCartIndex", new { ErrorMessage = "Looks like you have some duplicates in your shopping cart. Check back through your shopping cart!" }));
            }
            else
            {
                // throw in total price, songs, and albums into a new order
                Order NewOrder = new Order();
                NewOrder.Customer = userLoggedIn;

                // add all the songs from shopping cart to this order
                foreach (var song in userLoggedIn.SongsInShoppingCart)
                {
                    Song            songToAdd       = db.Songs.Find(song.Song.SongID);
                    SongOrderBridge songBridgeToAdd = new SongOrderBridge();

                    decimal SongPricePostDiscounts = song.Song.SongPrice;


                    // calculate total discounts
                    foreach (Discount discount in songToAdd.SongDiscounts)
                    {
                        if (discount.IsActiveDiscount)
                        {
                            SongPricePostDiscounts = songToAdd.SongPrice * (1 - discount.DiscountPercentage);
                        }
                    }

                    //properties of songbridgetoadd
                    songBridgeToAdd.PriceAtPointOfPurchase = SongPricePostDiscounts * 1.0825m;
                    songBridgeToAdd.SongInOrder            = songToAdd;

                    // add to order
                    NewOrder.SongsInOrder.Add(songBridgeToAdd);

                    // add all the songs to the recipient's songs list
                    friend.Songs.Add(songToAdd);
                }

                // add all the albums from shopping cart to this order
                foreach (var album in userLoggedIn.AlbumsInShoppingCart)
                {
                    Album            albumToAdd       = db.Albums.Find(album.Album.AlbumID);
                    AlbumOrderBridge albumBridgeToAdd = new AlbumOrderBridge();

                    decimal AlbumPricePostDiscounts = album.Album.AlbumPrice;

                    foreach (Discount discount in albumToAdd.AlbumDiscounts)
                    {
                        if (discount.IsActiveDiscount)
                        {
                            AlbumPricePostDiscounts = albumToAdd.AlbumPrice * (1 - discount.DiscountPercentage);
                        }
                    }

                    //properties of songbridgetoadd
                    albumBridgeToAdd.PriceAtPointOfPurchase = AlbumPricePostDiscounts * 1.0825m;
                    albumBridgeToAdd.AlbumInOrder           = albumToAdd;

                    // add to the order
                    NewOrder.AlbumsInOrder.Add(albumBridgeToAdd);

                    // add all the albums to the recipient's albums list
                    friend.Albums.Add(albumToAdd);
                }

                // add the total price to the order
                NewOrder.TotalPrice = (CalculateSongTotal() + CalculateAlbumTotal()) * 1.0825m;

                // add the credit card used
                NewOrder.CreditCardUsed = db.CreditCards.Find(SelectedCreditCardID);

                // is gift?
                NewOrder.IsGift      = true;
                NewOrder.RecipientID = friend.Id;

                String GiftSongs = "";
                foreach (var item in NewOrder.SongsInOrder)
                {
                    GiftSongs += item.SongInOrder.SongName + ", ";
                }

                String GiftAlbums = " ";
                foreach (var item in NewOrder.AlbumsInOrder)
                {
                    GiftAlbums += item.AlbumInOrder.AlbumName + ", ";
                }

                // send a new email to the recipient and the user who just placed the order
                EmailController.OrderGift(NewOrder.Customer, friend, GiftSongs, GiftAlbums);

                // clear out the shopping cart
                userLoggedIn.SongsInShoppingCart.Clear();
                userLoggedIn.AlbumsInShoppingCart.Clear();

                // add the order to the database
                db.Orders.Add(NewOrder);
                db.SaveChanges();

                // take the customer to the order confirmation page so they can see the songs/albums they just purchased
                return(RedirectToAction("CheckoutConfirmationPage", "ShoppingCarts", new { RecipientID = friend.Id, PlacedOrderID = NewOrder.OrderID }));
            }
        }
Esempio n. 4
0
        public ActionResult Checkout(int?SelectedCreditCardID)
        {
            AppUser userLoggedIn = db.Users.Find(User.Identity.GetUserId());

            // check to see if they have a credit card that they're purchasing with
            if (SelectedCreditCardID == null)
            {
                return(RedirectToAction("CheckoutPage", new { ErrorMessage = "Looks like you forgot to select a credit card. If you don't have a credit card, make sure to add one!" }));
            }

            // check to see that there are no duplicates first of all
            if (DuplicatesExist())
            {
                return(RedirectToAction("ShoppingCartIndex", new { ErrorMessage = "Looks like you have some duplicates in your shopping cart. Check back through your shopping cart!" }));
            }
            else
            {
                // throw in total price, songs, and albums into a new order
                Order NewOrder = new Order();
                NewOrder.Customer = userLoggedIn;

                // add all the songs from shopping cart to this order
                foreach (var song in userLoggedIn.SongsInShoppingCart)
                {
                    Song            songToAdd              = db.Songs.Find(song.Song.SongID);
                    SongOrderBridge songBridgeToAdd        = new SongOrderBridge();
                    decimal         SongPricePostDiscounts = song.Song.SongPrice;

                    // calculate total discounts
                    foreach (Discount discount in songToAdd.SongDiscounts)
                    {
                        if (discount.IsActiveDiscount)
                        {
                            SongPricePostDiscounts = songToAdd.SongPrice * (1 - discount.DiscountPercentage);
                        }
                    }

                    //properties of songbridgetoadd
                    songBridgeToAdd.PriceAtPointOfPurchase = SongPricePostDiscounts * 1.0825m;
                    songBridgeToAdd.SongInOrder            = songToAdd;

                    // add to the order
                    NewOrder.SongsInOrder.Add(songBridgeToAdd);

                    // add all the songs to the customer's songs list
                    userLoggedIn.Songs.Add(songToAdd);
                    db.SaveChanges();
                }

                // add all the albums from shopping cart to this order
                foreach (var album in userLoggedIn.AlbumsInShoppingCart)
                {
                    Album            albumToAdd              = db.Albums.Find(album.Album.AlbumID);
                    AlbumOrderBridge albumBridgeToAdd        = new AlbumOrderBridge();
                    decimal          AlbumPricePostDiscounts = album.Album.AlbumPrice;
                    foreach (Discount discount in albumToAdd.AlbumDiscounts)
                    {
                        if (discount.IsActiveDiscount)
                        {
                            AlbumPricePostDiscounts = albumToAdd.AlbumPrice * (1 - discount.DiscountPercentage);
                        }
                    }

                    //properties of songbridgetoadd
                    albumBridgeToAdd.PriceAtPointOfPurchase = AlbumPricePostDiscounts * 1.0825m;
                    albumBridgeToAdd.AlbumInOrder           = albumToAdd;

                    // add to the order
                    NewOrder.AlbumsInOrder.Add(albumBridgeToAdd);

                    // add all the albums to the customer's albums list
                    userLoggedIn.Albums.Add(albumToAdd);
                }

                // add the total price to the order
                NewOrder.TotalPrice = (CalculateSongTotal() + CalculateAlbumTotal()) * 1.0825m;

                // add the credit card to the order
                NewOrder.CreditCardUsed = db.CreditCards.Find(SelectedCreditCardID);

                //isgift?
                NewOrder.IsGift      = false;
                NewOrder.RecipientID = userLoggedIn.Id;

                //pick random song
                //take genre from song
                //query song with highest rating of that genre

                List <short> SongsList = new List <short>();

                if (userLoggedIn.SongsInShoppingCart != null || userLoggedIn.SongsInShoppingCart.Count() > 0)
                {
                    foreach (var item in userLoggedIn.SongsInShoppingCart)
                    {
                        foreach (var item2 in item.Song.SongGenres)
                        {
                            SongsList.Add(item2.GenreID);
                        }
                    }
                }
                if (userLoggedIn.AlbumsInShoppingCart != null || userLoggedIn.AlbumsInShoppingCart.Count() > 0)
                {
                    foreach (var item in userLoggedIn.AlbumsInShoppingCart)
                    {
                        foreach (var item2 in item.Album.AlbumGenres)
                        {
                            SongsList.Add(item2.GenreID);
                        }
                    }
                }

                String AllSongsPurchased = "";
                foreach (var item in userLoggedIn.SongsInShoppingCart)
                {
                    AllSongsPurchased += item.Song.SongName + ", ";
                }

                String AllAlbumsPurchased = " ";
                foreach (var item in userLoggedIn.AlbumsInShoppingCart)
                {
                    AllAlbumsPurchased += item.Album.AlbumName + ", ";
                }

                short RandomGenre = SongsList[0];

                var query = from s in db.Songs where (s.SongGenres.Any(a => a.GenreID == RandomGenre)) select s;
                //query = from s in db.Songs where (s == s.SongRatings.First(x => x.RatingNumber.Max)) select s;
                List <Song> BestSongs          = query.ToList();
                String      SongRecommendation = BestSongs[0].SongName;

                EmailController.OrderCustomer(userLoggedIn, AllSongsPurchased, AllAlbumsPurchased, SongRecommendation, NewOrder.OrderID);

                // clear out the shopping cart
                userLoggedIn.SongsInShoppingCart.Clear();
                userLoggedIn.AlbumsInShoppingCart.Clear();

                // add the order to the database
                db.Orders.Add(NewOrder);
                db.SaveChanges();

                // take the customer to the confirmation pageso they can see the songs/albums they just purchased
                return(RedirectToAction("CheckoutConfirmationPage", "ShoppingCarts", new { RecipientID = userLoggedIn.Id, PlacedOrderID = NewOrder.OrderID }));
            }
        }