Exemple #1
0
        public ActionResult AddAlbum(int?id, AlbumAdd newItem)
        {
            // Validate the input
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("details", "artists", new { id = id }));
            }

            // Verify that the passed-in identifier is in the new item's
            // collection of identifiers
            if (!newItem.ArtistIds.Contains(id.GetValueOrDefault()))
            {
                return(RedirectToAction("details", "artists", new { id = id }));
            }

            // Process the input
            var addedItem = m.AlbumAdd(newItem);

            if (addedItem == null)
            {
                return(RedirectToAction("details", "artists", new { id = id }));
            }
            else
            {
                // Attention 15 - Must redirect to the albums controller
                return(RedirectToAction("details", "albums", new { id = addedItem.Id }));
            }
        }
        public AlbumBase AlbumAdd(AlbumAdd newItem)
        {
            // Attention 14 - Manager method to add a new album for an artist

            // Validate each of the desired associated artists
            var artists = new List <Artist>();

            foreach (var artistId in newItem.ArtistIds)
            {
                var a = ds.Artists.Find(artistId);
                if (a != null)
                {
                    artists.Add(a);
                }
            }

            // Validate each of the desired associated tracks
            var tracks = new List <Track>();

            foreach (var trackId in newItem.TrackIds)
            {
                var a = ds.Tracks.Find(trackId);
                if (a != null)
                {
                    tracks.Add(a);
                }
            }

            // We only really want to continue if the album HAS at least one associated artist
            if (artists.Count == 0)
            {
                return(null);
            }
            else
            {
                // Attempt to add the new item
                var addedItem = ds.Albums.Add(mapper.Map <Album>(newItem));

                // Set the association - artists
                foreach (var item in artists)
                {
                    addedItem.Artists.Add(item);
                }

                // Set the association - tracks
                foreach (var item in tracks)
                {
                    addedItem.Tracks.Add(item);
                }

                // Set the coordinator user name
                addedItem.Coordinator = User.Name;

                ds.SaveChanges();

                return((addedItem == null) ? null : mapper.Map <AlbumBase>(addedItem));
            }
        }
        // Create a new album with artists
        public AlbumWithDetail AlbumAdd(AlbumAdd newItem)
        {
            var o = ds.Albums.Add(Mapper.Map <Album>(newItem));

            foreach (var item in newItem.ArtistIds)
            {
                var a = ds.Artists.Find(item);
                o.Artists.Add(a);
            }

            foreach (var item in newItem.TrackIds)
            {
                var a = ds.Tracks.Find(item);
                o.Tracks.Add(a);
            }

            ds.SaveChanges();

            return((o == null) ? null : Mapper.Map <AlbumWithDetail>(o));
        }
        public ActionResult Create(AlbumAdd newItem)
        {
            newItem.Coordinator = HttpContext.User.Identity.Name;
            ModelState.Clear();

            if (!ModelState.IsValid)
            {
                return(View(newItem));
            }

            var addedItem = m.AlbumAdd(newItem);

            if (addedItem == null)
            {
                return(View(newItem));
            }
            else
            {
                return(RedirectToAction("details", new { id = addedItem.Id }));
            }
        }