public AlbumSearchViewModel(string phrase, Chinook.Repository.IMusicRepository musicRepository)
        {
            phrase = phrase.ToLower();

            var artists = musicRepository.GetArtists().ToArray();

            var artistQuery =
                from x in artists
                select x;

            if (!String.IsNullOrWhiteSpace(phrase))
            {
                artistQuery =
                    from x in artistQuery
                    where x.Name.ToLower().Contains(phrase)
                    select x;
            }

            var artistIDs =
                (from x in artistQuery
                 select x.ID).ToArray();

            var query =
                from x in musicRepository.GetAlbums()
                where artistIDs.Contains(x.ArtistID)
                select x;

            if (!String.IsNullOrWhiteSpace(phrase))
            {
                var query2 =
                    from x in musicRepository.GetAlbums()
                    where x.Title.ToLower().Contains(phrase)
                    select x;
                query = query.Union(query2);
            }

            var albums = query.Distinct();

            Albums =
                from a in albums
                select new AlbumViewModel
            {
                AlbumID = a.AlbumID,
                Title   = a.Title,
                Artist  = artists.Where(x => x.ID == a.ArtistID).Select(x => x.Name).FirstOrDefault(),
            };
        }
Example #2
0
        public AlbumIndexViewModel(Chinook.Repository.IMusicRepository musicRepository, int page, int pageSize = 5)
        {
            PageSize = pageSize;

            var albums     = musicRepository.GetAlbums();
            int totalRows  = albums.Count();
            int totalPages = (int)Math.Ceiling((double)totalRows / pageSize);

            int currentPage = page;

            if (currentPage < 1)
            {
                currentPage = 1;
            }
            if (currentPage > totalPages)
            {
                currentPage = totalPages;
            }

            int startRow = (currentPage - 1) * pageSize;

            CurrentPage  = currentPage;
            TotalPages   = totalPages;
            NextPage     = currentPage < totalPages ? currentPage + 1 : (int?)null;
            PreviousPage = currentPage > 1 ? currentPage - 1 : (int?)null;
            StartingRow  = startRow + 1;

            Albums =
                from a in albums.OrderBy(x => x.AlbumID).Skip(startRow).Take(pageSize)
                select new AlbumViewModel
            {
                AlbumID = a.AlbumID,
                Title   = a.Title,
                Artist  = musicRepository.GetArtists().Where(x => x.ID == a.ArtistID).Select(x => x.Name).FirstOrDefault(),
            };
        }
 public AlbumEditViewModel(Chinook.DomainModel.Album album, Chinook.Repository.IMusicRepository musicRepository)
 {
     Album   = new AlbumInputModel(album);
     Artists = new SelectList(musicRepository.GetArtists(), "ID", "Name", album.ArtistID);
 }