Beispiel #1
0
        public void CallSongServiceMethodGetSongsOnce_WhenInvoked()
        {
            // Arrange
            var songService       = new Mock <ISongService>();
            var userService       = new Mock <IUserService>();
            var artistService     = new Mock <IArtistService>();
            var albumService      = new Mock <IAlbumService>();
            var genreService      = new Mock <IGenreService>();
            var songModifyService = new Mock <ISongModifyService>();

            var songCollection = new List <Song>()
            {
                new Song()
                {
                    Title = "Song Title",
                    Album = new Album()
                    {
                        Title = "Album Title"
                    },
                    Artist = new Artist()
                    {
                        Name = "Artist Name"
                    },
                    Duration = 5,
                    Genres   = new List <Genre>()
                    {
                        new Genre()
                        {
                            Name = "Genre Name"
                        }
                    },
                    Lyrics   = "Some Lyrics",
                    VideoUrl = "VideoUrl"
                }
            };

            songService.Setup(x => x.GetSongs()).Returns(() => songCollection.AsQueryable());

            var sut = new SongController(
                songService.Object,
                userService.Object,
                artistService.Object,
                albumService.Object,
                genreService.Object,
                songModifyService.Object);

            var model = new SongSearchViewModel();

            // Act
            sut.SearchSong(model);

            // Assert
            songService.Verify(x => x.GetSongs());
        }
        public async Task <IActionResult> IndexAsync(SongSearchViewModel searchModel)
        {
            int?emphasisHeaderNum = null;

            // load songs and related albums
            IEnumerable <Song> songs = await _context.Songs
                                       .Include(s => s.Album)
                                       .ToListAsync();

            // get current user
            ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (currentUser == null)
            {
                throw new Exception("No current user.");
            }

            // filter by current owner
            songs = songs.Where(s => s.ArtistId == currentUser.Id);

            // if album supplied, filter by that album
            if (searchModel.AlbumId != null)
            {
                Album album = _context.Albums.FirstOrDefault(a => a.Id == searchModel.AlbumId);

                if (album == null)
                {
                    throw new Exception("That album doesn't exist.");
                }

                songs = songs.Where(s => s.AlbumId == album.Id);
            }

            // search criteria
            if (!string.IsNullOrWhiteSpace(searchModel.SearchCriteria))
            {
                songs = songs.Where(item => item.Name.ToUpper().Contains(searchModel.SearchCriteria.ToUpper()));
            }

            // date filtering
            // start date
            if (!string.IsNullOrEmpty(searchModel.StartDate))
            {
                DateTime startDate = DateTime.Parse(searchModel.StartDate);
                songs = songs.Where(item => item.ReleaseDate.Value.CompareTo(startDate) >= 0);
            }

            // end date
            if (!string.IsNullOrEmpty(searchModel.EndDate))
            {
                DateTime endDate = DateTime.Parse(searchModel.EndDate);
                songs = songs.Where(item => item.ReleaseDate.Value.CompareTo(endDate) <= 0);
            }

            // sort order
            switch (searchModel.SortOrder)
            {
            case "song-name":
                songs             = songs.OrderBy(s => s.Name);
                emphasisHeaderNum = 0;
                break;

            case "artist":
                songs             = songs.OrderBy(s => s.ArtistName);
                emphasisHeaderNum = 1;
                break;

            case "album-name":
                songs = songs.OrderBy(s => {
                    if (s.Album != null)
                    {
                        return(s.Album.Name);
                    }
                    else
                    {
                        return("Unknown Album");
                    }
                });
                emphasisHeaderNum = 2;
                break;

            case "release-date":
                songs = songs.OrderBy(s => {
                    if (s.ReleaseDate != null)
                    {
                        return(s.ReleaseDate.Value.ToString());
                    }
                    else
                    {
                        return("");
                    }
                });
                emphasisHeaderNum = 3;
                break;

            default:
                songs = songs.OrderBy(s => s.Id);
                break;
            }

            // flip order
            if (searchModel.FlipOrder)
            {
                songs = songs.Reverse();
            }

            // pagination
            int pageNumber = (searchModel.PageNumber ?? 1);
            int pageSize   = (searchModel.PageSize ?? 10);

            IPaginator <Song> paginator = new Paginator <Song>(songs, pageSize, pageNumber);

            ViewData["paginator"]         = paginator;
            ViewData["searchSettings"]    = searchModel;
            ViewData["emphasisHeaderNum"] = emphasisHeaderNum;

            // send the paginated items to the view
            return(View(paginator.GetItems()));
        }
Beispiel #3
0
        public ActionResult SearchSong(SongSearchViewModel songRequest)
        {
            var songs = this.songService
                        .GetSongs();

            if (songRequest.OnlyFavorites)
            {
                songs = songs.Where(x => x.FavoritedBy.Select(u => u.UserName).Contains(User.Identity.Name));
            }

            if (!String.IsNullOrEmpty(songRequest.SearchTerm))
            {
                switch (songRequest.SearchBy)
                {
                case "Title":
                    songs = songs.Where(x => x.Title.Contains(songRequest.SearchTerm));
                    break;

                case "Album":
                    songs = songs.Where(x => x.Album.Title.Contains(songRequest.SearchTerm));
                    break;

                case "Artist":
                    songs = songs.Where(x => x.Artist.Name.Contains(songRequest.SearchTerm));
                    break;
                }
            }

            if (songRequest.IsDescending)
            {
                switch (songRequest.OrderBy)
                {
                case "Title":
                    songs = songs.OrderByDescending(x => x.Title);
                    break;

                case "Album":
                    songs = songs.OrderByDescending(x => x.Album.Title);
                    break;

                case "Artist":
                    songs = songs.OrderByDescending(x => x.Artist.Name);
                    break;
                }
            }
            else
            {
                switch (songRequest.OrderBy)
                {
                case "Title":
                    songs = songs.OrderBy(x => x.Title);
                    break;

                case "Album":
                    songs = songs.OrderBy(x => x.Album.Title);
                    break;

                case "Artist":
                    songs = songs.OrderBy(x => x.Artist.Name);
                    break;
                }
            }

            var data = songs
                       .Select(x => new SongViewModel
            {
                Id       = x.Id,
                Title    = x.Title,
                Artist   = x.Artist.Name,
                Album    = x.Album.Title,
                Duration = x.Duration,
                Genres   = x.Genres.Select(g => g.Name).ToList(),
                Lyrics   = x.Lyrics,
                VideoUrl = x.VideoUrl,
                CoverUrl = x.Album.CoverUrl,
                Users    = x.FavoritedBy.Select(u => u.Email)
            })
                       .ToList();


            return(View(LibraryView, data));
        }