public ActionResult Save(Album album) { if (!ModelState.IsValid) { var viewModel = new AlbumFormViewModel(album) { Genres = _context.Genres.ToList() }; return(View("AlbumForm", viewModel)); } if (album.Id == 0) { album.DateAdded = DateTime.Now; _context.Albums.Add(album); } else { var albumInDb = _context.Albums.Single(m => m.Id == album.Id); albumInDb.Name = album.Name; albumInDb.GenreId = album.GenreId; albumInDb.NumberInStock = album.NumberInStock; albumInDb.ReleaseDate = album.ReleaseDate; } _context.SaveChanges(); return(RedirectToAction("Index", "Albums")); }
public async Task <IActionResult> Edit(int id, AlbumFormViewModel model) { var IsExisting = await this.albumService.ExistAsync(id); if (!IsExisting) { ModelState.AddModelError(nameof(id), "There is no such albums"); } 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 AlbumFormViewModel { Title = model.Title, Price = model.Price, AmountOfSongs = model.AmountOfSongs, Artists = await this.GetArtists() })); } await this.albumService.EditAsync(id, model.Title, model.Price, model.AmountOfSongs, model.ArtistId); return(RedirectToAction(nameof(ListAll))); }
public ActionResult Save(AlbumFormViewModel albumVm) { if (!ModelState.IsValid) { return(View("AlbumForm", albumVm)); } if (albumVm.Id == 0) { var album = Mapper.Map <Album>(albumVm); db.Albums.Add(album); } else { //db.Entry(albumVm).State = EntityState.Modified; var albumInDb = db.Albums.Find(albumVm.Id); Mapper.Map <AlbumFormViewModel, Album> ( source: albumVm, destination: albumInDb ); } db.SaveChanges(); return(RedirectToAction("Index")); //ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Name", albumVm.ArtistId); //return View(albumVm); }
public ActionResult Save(AlbumFormViewModel albumViewModel) { //If it is a new album create it if (albumViewModel.album.albumId == 0) { var album = albumViewModel.album; Genre genre = _context.genres.SingleOrDefault(c => c.id == albumViewModel.genreChosen); album.genres.Add(genre); _context.albums.Add(albumViewModel.album); _context.SaveChanges(); } else { //Update database album Album databaseAlbum = _context.albums.Single(c => c.albumId == albumViewModel.album.albumId); Album viewAlbum = albumViewModel.album; databaseAlbum.albumName = viewAlbum.albumName; databaseAlbum.bandName = viewAlbum.bandName; databaseAlbum.year = viewAlbum.year; Genre genre = _context.genres.SingleOrDefault(c => c.id == albumViewModel.genreChosen); databaseAlbum.genres.Clear(); databaseAlbum.genres.Add(genre); } _context.SaveChanges(); //Send user to new album's info page. String redirect = "GetAlbum/" + albumViewModel.album.albumId; return(RedirectToAction(redirect, "Album")); }
// GET: Albums/Add public ActionResult Add() { var vm = new AlbumFormViewModel { ArtistsList = GetArtistsSelectListItem() }; return(View("AlbumForm", vm)); }
public ActionResult AlbumForm() { var genres = _context.genres.ToList(); var albumViewModel = new AlbumFormViewModel() { genres = genres }; return(View(albumViewModel)); }
//Shows the page with results, may want to combine with the search form. //TODO: Make this obsolete by allowing the search form to update by api calls public ActionResult SearchResults(AlbumFormViewModel albumViewModel) { var albums = _context.albums.Where(c => c.albumName.Equals(albumViewModel.album.albumName) || c.bandName.Equals(albumViewModel.album.bandName) || c.year == albumViewModel.album.year).ToList(); SearchResultsViewModel resultsViewModel = new SearchResultsViewModel { toShow = albums }; return(View(resultsViewModel)); }
public ViewResult New() { var genres = _context.Genres.ToList(); var viewModel = new AlbumFormViewModel { Genres = genres }; return(View("AlbumForm", viewModel)); }
// GET: Albums/Create public ActionResult New() { var types = db.TypesOfAlbum.ToList(); var artists = db.Artists.ToList(); var viewModel = new AlbumFormViewModel { Artist = artists, TypeOfAlbum = types }; return(View("AlbumForm", viewModel)); }
public ActionResult New() { var genres = _context.Genres.ToList(); var bands = _context.Bands.ToList(); var viewModel = new AlbumFormViewModel { Genres = genres, Bands = bands }; return(View("AlbumForm", viewModel)); }
public ActionResult Edit(int id) { var album = _context.Albums.SingleOrDefault(c => c.Id == id); if (album == null) { return(HttpNotFound()); } var viewModel = new AlbumFormViewModel(album) { Genres = _context.Genres.ToList() }; return(View("AlbumForm", viewModel)); }
// GET: Albums/Edit/5 public ActionResult Edit(int id) { var album = db.Albums.Include(a => a.Type).SingleOrDefault(a => a.AlbumId == id); var artists = db.Artists.ToList(); if (album == null) { return(HttpNotFound()); } var viewModel = new AlbumFormViewModel { Artist = artists, Album = album, TypeOfAlbum = db.TypesOfAlbum.ToList() }; return(View("AlbumForm", viewModel)); }
public async Task <IActionResult> Add(AlbumFormViewModel model) { 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 AlbumFormViewModel { Artists = await this.GetArtists() })); } await this.albumService.CreateAsync(model.Title, model.Price, model.AmountOfSongs, model.ArtistId); return(RedirectToAction(nameof(ListAll))); }
public ActionResult Edit(int id) { Album album = _context.albums.SingleOrDefault(c => c.albumId == id); if (album == null) { return(HttpNotFound()); } int genreChosenId = 0; if (album.genres.Count > 0) { genreChosenId = album.genres.ElementAt(0).id; } var g = _context.genres.ToList(); var viewModel = new AlbumFormViewModel { album = album, genres = _context.genres.ToList(), genreChosen = genreChosenId }; return(View("AlbumForm", viewModel)); }
public ActionResult Save(Album album) { if (!ModelState.IsValid) { var viewModel = new AlbumFormViewModel(album) { Genres = _context.Genres.ToList(), Bands = _context.Bands.ToList() }; return(View("AlbumForm", viewModel)); } if (album.Id == 0) { var bandInDb = _context.Bands.SingleOrDefault(m => m.BandId == album.BandId); if (bandInDb != null) { bandInDb.NrAlbumsOnSite += 1; } var genreInDb = _context.Genres.SingleOrDefault(m => m.Id == album.GenreId); if (genreInDb != null) { genreInDb.AlbumCount += 1; } _context.Albums.Add(album); } else { var albumOld = _context.Albums.Single(m => m.Id == album.Id); if (albumOld.BandId != album.BandId) { var oldBand = _context.Bands.Single(m => m.BandId == albumOld.BandId); oldBand.NrAlbumsOnSite -= 1; var newBand = _context.Bands.SingleOrDefault(m => m.BandId == album.BandId); if (newBand != null) { newBand.NrAlbumsOnSite += 1; } } if (albumOld.GenreId != album.GenreId) { var oldGenre = _context.Genres.Single(m => m.Id == albumOld.Id); oldGenre.AlbumCount -= 1; var newGenre = _context.Genres.SingleOrDefault(m => m.Id == album.Id); if (newGenre != null) { newGenre.AlbumCount += 1; } } albumOld.Name = album.Name; albumOld.Price = album.Price; albumOld.ReleaseDate = album.ReleaseDate; albumOld.BandId = album.BandId; albumOld.GenreId = album.GenreId; albumOld.NumberInStock = album.NumberInStock; } _context.SaveChanges(); return(RedirectToAction("Index", "Albums")); }