コード例 #1
0
ファイル: AlbumService.cs プロジェクト: sergeyUzer/musicShop
        /// <summary>
        /// Returns the album details using the specified currency and price level for album price.
        /// </summary>
        /// <param name="id">The album id.</param>
        /// <param name="currencyCode">
        /// The currency code for album price. If it doesn't specified than default currency is used.
        /// </param>
        /// <param name="priceLevelId">
        /// The price level for album price. If it doesn't specified than default price level is used.
        /// </param>
        /// <param name="userId">
        /// The current user id.
        /// </param>
        /// <returns>
        /// The information about album with the specified <paramref name="id"/> or <b>null</b> if album doesn't exist.
        /// </returns>
        public AlbumDetailsViewModel GetAlbumDetails(int id, int?currencyCode = null, int?priceLevelId = null, int?userId = null)
        {
            Album album;

            using (var repository = Factory.GetAlbumRepository())
            {
                album = repository.GetById(id, a => a.Artist);
            }

            var albumViewModel = ModelsMapper.GetAlbumDetailsViewModel(album);

            if (currencyCode == null)
            {
                currencyCode = ServiceHelper.GetDefaultCurrency(Factory).Code;
            }

            if (priceLevelId == null)
            {
                priceLevelId = ServiceHelper.GetDefaultPriceLevel(Factory);
            }

            using (var repository = Factory.GetAlbumTrackRelationRepository())
            {
                albumViewModel.TracksCount = repository.Count(r => r.AlbumId == albumViewModel.Id);
                if (albumViewModel.TracksCount > 0)
                {
                    using (var albumPriceRepository = Factory.GetAlbumPriceRepository())
                    {
                        using (var currencyRatesrepository = Factory.GetCurrencyRateRepository())
                        {
                            albumViewModel.Price =
                                PriceHelper.GetAlbumPrice(albumPriceRepository, currencyRatesrepository, id, currencyCode.Value, priceLevelId.Value);
                        }
                    }
                }
            }

            if (userId != null)
            {
                using (var repository = Factory.GetOrderAlbumRepository())
                {
                    albumViewModel.IsOrdered =
                        repository.Exist(o => o.UserId == userId && o.AlbumId == albumViewModel.Id);
                }

                using (var repository = Factory.GetPurchasedAlbumRepository())
                {
                    albumViewModel.IsPurchased =
                        repository.Exist(p => p.UserId == userId && p.AlbumId == albumViewModel.Id);
                }
            }

            return(albumViewModel);
        }
コード例 #2
0
        public void GetAlbumDetailsViewModelTest()
        {
            var artistDto = CreateArtist();

            var albumDto = new Album
            {
                Id       = 1,
                Name     = "Some album",
                Artist   = artistDto,
                ArtistId = artistDto.Id
            };
            var albumViewModel = ModelsMapper.GetAlbumDetailsViewModel(albumDto);

            Assert.IsNotNull(albumViewModel);

            Assert.IsTrue(albumViewModel.Id == albumDto.Id);
            Assert.IsTrue(albumViewModel.Name.Equals(albumDto.Name, StringComparison.OrdinalIgnoreCase));

            Assert.IsNotNull(albumViewModel.Artist);
            Assert.IsTrue(albumViewModel.Artist.Id == albumDto.ArtistId);
        }