示例#1
0
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // get the song from the database
            var song = _context
                       .Songs
                       .Include(s => s.Album)
                       .SingleOrDefault(s => s.ID == id);

            // Check if the song is null
            if (song == null)
            {
                return(HttpNotFound());
            }

            //Get the Albums from the database
            var albums = _context.Albums.ToList();

            // Init the viewmodel and fill it
            var viewModel = new SongFormViewModel()
            {
                Song   = song,
                Albums = albums
            };

            return(View("SongForm", viewModel));
        }
示例#2
0
        public async Task <IActionResult> Add(SongFormViewModel model)
        {
            var IsGanreExist = this.songService.IsGanreExist((int)model.Ganre);

            if (!IsGanreExist)
            {
                ModelState.AddModelError(nameof(model.Ganre), "Please select at least one Ganre");
            }

            var isArtistExisting = await this.artistService.ExistAsync(model.ArtistId);

            if (!isArtistExisting)
            {
                ModelState.AddModelError(nameof(model.ArtistId), "Please select valid Artist");
            }

            if (!ModelState.IsValid)
            {
                return(View(new SongFormViewModel
                {
                    Artists = await this.GetArtists()
                }));
            }

            await this.songService.CreateAsync(model.Name, model.Price, model.Duration, model.ArtistId, model.Ganre);

            TempData.AddSuccessMessage($"The song {model.Name} has been added successfully");

            return(RedirectToAction(nameof(ListAll)));
        }
示例#3
0
        public ActionResult Save(Song song)
        {
            if (song.Youtube != null)
            {
                song.Youtube = $"https://www.youtube.com/embed/{song.Youtube}";
            }

            if (song.ID == 0)
            {
                _context.Songs.Add(song);
            }
            else
            {
                var songInDb = _context.Songs.Single(s => s.ID == song.ID);
                songInDb.Title   = song.Title;
                songInDb.Youtube = song.Youtube;
                songInDb.AlbumId = song.AlbumId;
            }

            if (!ModelState.IsValid)
            {
                var viewModel = new SongFormViewModel()
                {
                    Song   = song,
                    Albums = _context.Albums.ToList()
                };
                return(View("SongForm", viewModel));
            }
            else
            {
                _context.SaveChanges();
            }
            return(RedirectToAction("Index", "Songs"));
        }
示例#4
0
        public ActionResult Save(Song song)
        {
            //validation
            if (!ModelState.IsValid)
            {
                var viewModel = new SongFormViewModel(song)
                {
                    Genres = _context.Genres.ToList()
                };

                return(View("SongForm", viewModel));
            }
            if (song.Id == 0)
            {
                _context.Songs.Add(song);
            }
            else
            {
                var songInDb = _context.Songs.Single(s => s.Id == song.Id);

                songInDb.Name          = song.Name;
                songInDb.ReleaseDate   = song.ReleaseDate;
                songInDb.NumberInStock = song.NumberInStock;
                songInDb.Name          = song.Name;
                songInDb.GenreId       = song.GenreId;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Songs"));
        }
示例#5
0
        public ActionResult New()
        {
            //iniatialize the class and set the genre property to the genres list
            var genres    = _context.Genres.ToList();
            var viewModel = new SongFormViewModel
            {
                Genres = genres
            };

            return(View("SongForm", viewModel));
        }
示例#6
0
        public ActionResult New()
        {
            // Get Albums from the database
            var albums = _context.Albums.ToList();
            // Init and fill the viewmodel
            var viewmodel = new SongFormViewModel()
            {
                Song   = new Song(),
                Albums = albums
            };

            // Return the appropriate view with the viewmodel
            return(View("SongForm", viewmodel));
        }
示例#7
0
        public ActionResult Edit(int id)
        {
            var song = _context.Songs.SingleOrDefault(s => s.Id == id);

            if (song == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new SongFormViewModel(song)
            {
                Genres = _context.Genres.ToList()
            };

            return(View("SongForm", viewModel));
        }
示例#8
0
        public ActionResult New()
        {
            //getting the genres from the database.
            var genres  = _context.Genres.ToList();
            var artists = _context.Artist.ToList();
            var albums  = _context.Album.ToList();

            var viewModel = new SongFormViewModel
            {
                Genres  = genres,
                Artists = artists,
                Albums  = albums,
                Song    = new Song()
            };


            return(View("SongForm", viewModel));
        }
示例#9
0
        //Save fucntion that saves what was entered in a form to the database.
        public ActionResult Save(SongFormViewModel viewModel)
        {
            var song = viewModel.Song;

            //getting all the songs from the database.
            var songs = _context.Song.ToList();

            //getting the genres from the database.
            var genres  = _context.Genres.ToList();
            var artists = _context.Artist.ToList();
            var albums  = _context.Album.ToList();

            //getting all the albums from the database.
            var albumsDB = _context.Album.ToList();

            //getting all the artists from the database.
            var artistsDB = _context.Artist.ToList();

            //boolean that checks if the album is in the array or not.
            bool newAlbum = true;

            //the last Id in the album array.
            int lastAlbumId = 0;

            //the last Id in the album array.
            int lastSongId = 0;

            //looping through each of the albums in the array.
            foreach (Album album in albumsDB)
            {
                lastAlbumId = album.Id;
            }


            //if the album Id is 0 that means nothing was selected from the drop down menu.
            if (viewModel.Song.AlbumId == 0 || viewModel.Song.AlbumId == null)
            {
                if (song.Album.Name == null)
                {
                    Console.WriteLine("Please enter a value");
                }
                else
                {
                    Artist albumArtist;
                    int    albumId = lastAlbumId;
                    Album  theAlbum;

                    //if the last album Id is 0 then theres no need to add 1.
                    if (lastAlbumId != 0)
                    {
                        albumId = albumId + 1;
                    }

                    //if the user has chosen an artist from the list.
                    if (song.ArtistId != 0)
                    {
                        albumArtist = artistsDB[song.ArtistId - 1];
                        //creating a new album with the information we have.
                        theAlbum = new Album
                        {
                            Name     = song.Album.Name,
                            Id       = albumId,
                            ArtistId = song.ArtistId
                        };

                        //setting the album of the song to be the one we have just created.
                        song.Album = theAlbum;



                        string sql = "INSERT INTO Albums VALUES(@Id, @Name, @ArtistId, @Songs)";

                        _context.Database.ExecuteSqlCommand(sql,
                                                            new SqlParameter("Id", albumId),
                                                            new SqlParameter("Name", song.Album.Name),
                                                            new SqlParameter("ArtistId", song.ArtistId),
                                                            new SqlParameter("Songs", song.Name + "/")
                                                            );



                        //_context.Album.Add(theAlbum);
                        //saving the album to the database.

                        /*
                         * try
                         * {
                         *  _context.SaveChanges();
                         * }
                         * catch (Exception e)
                         * {
                         *  return Content(e.ToString());
                         * }
                         */

                        //setting the genreId of the song to be that of what was just created.
                        int insertedId = albumId;
                        song.AlbumId = insertedId;
                    }
                    /*Else this is an already existing album and we want to */
                    else
                    {
                    }

                    Console.WriteLine("The new album name was: " + song.Album.Name);
                }
            }
            //else something was selected from the drop down meny so we set the album name to be the Id of what was selected.
            else
            {
                //setting the album name to be the value of the existing album.
                song.Album.Name = albumsDB[song.AlbumId.Value - 1].Name;
                Console.WriteLine("The album " + albumsDB[song.AlbumId.Value - 1] + " already exists.");

                //getting the selected album from the database.
                Album currAlbum = _context.Album.SingleOrDefault(a => a.Id == song.AlbumId.Value);

                //the new string we're going to insert into the songs of the album.
                string currSongs;

                //ensuring the selected album is correct and not null.
                if (currAlbum != null)
                {
                    //setting songs to be what is in the database currently.
                    currSongs = currAlbum.Songs;
                    if (currSongs == null)
                    {
                        currSongs = "";
                    }
                    //checking if the string we just created is empty or not.
                    if (currSongs.Equals("") || currSongs == null)
                    {
                        //then we're going to set the initial string the / is the character we will use to
                        //start a new song.
                        currSongs = song.Name + "/";
                    }
                    //else there are already songs in the database so we will ammend the current songs.
                    else
                    {
                        currSongs += song.Name + "/";
                    }

                    //sql code to update the songs.
                    string thisSql = "UPDATE Albums" +
                                     " SET Songs = '" + currSongs +
                                     "' WHERE Id = " + (song.AlbumId.Value);

                    //updating the database.
                    _context.Database.ExecuteSqlCommand(thisSql);
                }
                else
                {
                    currSongs = "Error";
                }

                //TODO: Make it so users cannot enter special characters e.g. ' and / as this will break the code.

                //we're going to add a song to the album we've just selected.
                string sql = "INSERT INTO Albums VALUES(@Songs)";

                //_context.Database.ExecuteSqlCommand(sql);
            }



            if (!ModelState.IsValid)
            {
                var thisViewModel = new SongFormViewModel
                {
                    Song    = song,
                    Albums  = albums,
                    Genres  = genres,
                    Artists = artists
                };

                return(View("SongForm", thisViewModel));
            }


            //if the song is a new song.
            if (song.Id == 0 || song.Id == null)
            {
                foreach (Song theSong in songs)
                {
                    lastSongId = theSong.Id.Value;
                }

                //_context.Song.Add(song);
                song.Id = lastSongId + 1;
            }
            //else we are editing a song that already exists.
            else
            {
            }

            try
            {
                string sql = "INSERT INTO Songs VALUES (@Id, @Name, @GenreId, @Released, @Description, @ArtistId, @AlbumId)";
                _context.Database.ExecuteSqlCommand(sql,
                                                    new SqlParameter("Id", song.Id),
                                                    new SqlParameter("Name", song.Name),
                                                    new SqlParameter("GenreId", song.GenreId),
                                                    new SqlParameter("Released", song.Released),
                                                    new SqlParameter("Description", song.Description),
                                                    new SqlParameter("ArtistId", song.ArtistId),
                                                    new SqlParameter("AlbumId", song.AlbumId)
                                                    );
                _context.SaveChanges();
            } catch (Exception e)
            {
                return(Content(e.ToString()));
            }

            //also adding the list of songs here so we can send the user back to the songs index page.
            var songViewModel = new SongViewModel
            {
                Songs   = songs,
                Genres  = genres,
                Albums  = albums,
                Artists = artists
            };

            return(View("Index", songViewModel));
        }