/// <summary> /// Returns the default currency. /// </summary> /// <param name="repositoryFactory"> /// The repository factory. /// </param> /// <returns> /// The default currency. /// </returns> internal static CurrencyViewModel GetDefaultCurrency(IRepositoryFactory repositoryFactory) { Currency currencyDto; using (var repository = repositoryFactory.GetSettingRepository()) { currencyDto = repository.FirstOrDefault(s => !s.IsDeleted)?.DefaultCurrency; } return(ModelsMapper.GetCurrencyViewModel(currencyDto)); }
/// <summary> /// Returns the album price in the specified currency and price level. /// </summary> /// <param name="albumPriceRepository"> /// The repository. /// </param> /// <param name="currencyRatesRepository"> /// The currency rates repository. /// </param> /// <param name="albumId"> /// The album id. /// </param> /// <param name="currencyCode"> /// The currency code. /// </param> /// <param name="priceLevelId"> /// The price level id. /// </param> /// <returns> /// The album price in the specified currency and price level or <b>null</b>. /// </returns> internal static PriceViewModel GetAlbumPrice( IAlbumPriceRepository albumPriceRepository, ICurrencyRateRepository currencyRatesRepository, int albumId, int currencyCode, int priceLevelId) { var price = albumPriceRepository.FirstOrDefault( p => p.AlbumId == albumId && p.PriceLevelId == priceLevelId && p.Currency.Code == currencyCode, p => p.Album, p => p.Currency, p => p.PriceLevel); // if price is not exist for the specified currency then we'll try to find price in any other currency if (price == null) { price = albumPriceRepository.FirstOrDefault( p => p.AlbumId == albumId && p.PriceLevelId == priceLevelId, p => p.Currency); if (price != null) { PriceViewModel targetPrice; if (TryConvertToTargetPrice( currencyRatesRepository, price.Price, price.Currency.Code, currencyCode, out targetPrice)) { return(targetPrice); } } } return(ModelsMapper.GetPriceViewModel(price)); }
/// <summary> /// Tries to convert price to price in the specified currency via a cross-cource. /// </summary> /// <param name="repository"> /// The currency rates repository. /// </param> /// <param name="amount"> /// The price amount. /// </param> /// <param name="currencyCode"> /// The currency code. /// </param> /// <param name="targetCurrencyCode"> /// The target currency code. /// </param> /// <param name="targetPrice"> /// The convertion result. /// </param> internal static bool TryConvertToTargetPrice( ICurrencyRateRepository repository, decimal amount, int currencyCode, int targetCurrencyCode, out PriceViewModel targetPrice) { var rate = GetCurrencyRate(repository, currencyCode, targetCurrencyCode); if (rate != null) { targetPrice = new PriceViewModel { // financial rounding to even Amount = Math.Round(rate.CrossCourse * amount, 2, MidpointRounding.ToEven), Currency = ModelsMapper.GetCurrencyViewModel(rate.TargetCurrency) }; return(true); } targetPrice = null; return(false); }
/// <summary> /// Converts each album to <see cref="AlbumViewModel"/>. /// </summary> /// <param name="factory"> /// The factory. /// </param> /// <param name="albums"> /// The albums. /// </param> /// <param name="currencyCode"> /// The currency code. /// </param> /// <param name="priceLevel"> /// The price level. /// </param> /// <param name="userId"> /// The current user id. /// </param> /// <returns> /// A new collection with <see cref="AlbumViewModel"/> items. /// </returns> internal static ICollection <AlbumViewModel> ConvertToAlbumViewModels( IRepositoryFactory factory, ICollection <Album> albums, int?currencyCode, int?priceLevel, int?userId) { if (currencyCode == null) { currencyCode = GetDefaultCurrency(factory).Code; } if (priceLevel == null) { priceLevel = GetDefaultPriceLevel(factory); } List <AlbumViewModel> albumViewModels = new List <AlbumViewModel>(); using (var repository = factory.GetAlbumTrackRelationRepository()) { using (var albumPriceRepository = factory.GetAlbumPriceRepository()) { using (var currencyRatesrepository = factory.GetCurrencyRateRepository()) { foreach (var album in albums) { var albumViewModel = ModelsMapper.GetAlbumViewModel(album); if (albumViewModel != null) { albumViewModel.TracksCount = repository.Count(r => r.AlbumId == albumViewModel.Id); if (albumViewModel.TracksCount > 0) { albumViewModel.Price = PriceHelper.GetAlbumPrice(albumPriceRepository, currencyRatesrepository, albumViewModel.Id, currencyCode.Value, priceLevel.Value); } albumViewModels.Add(albumViewModel); } } } } } if (userId != null) { using (var repository = factory.GetOrderAlbumRepository()) { foreach (var albumViewModel in albumViewModels) { albumViewModel.IsOrdered = repository.Exist(o => o.UserId == userId && o.AlbumId == albumViewModel.Id); } } using (var repository = factory.GetPurchasedAlbumRepository()) { foreach (var albumViewModel in albumViewModels) { albumViewModel.IsPurchased = repository.Exist(p => p.UserId == userId && p.AlbumId == albumViewModel.Id); } } } return(albumViewModels); }
/// <summary> /// Converts each track to <see cref="TrackViewModel"/>. /// </summary> /// <param name="factory"> /// The factory. /// </param> /// <param name="tracks"> /// The tracks. /// </param> /// <param name="currencyCode"> /// The currency code. /// </param> /// <param name="priceLevel"> /// The price level. /// </param> /// <param name="userId"> /// The current user id. /// </param> /// <returns> /// A new collection with <see cref="TrackViewModel"/> items. /// </returns> internal static ICollection <TrackViewModel> ConvertToTrackViewModels( IRepositoryFactory factory, ICollection <Track> tracks, int?currencyCode, int?priceLevel, int?userId) { if (currencyCode == null) { currencyCode = GetDefaultCurrency(factory).Code; } if (priceLevel == null) { priceLevel = GetDefaultPriceLevel(factory); } List <TrackViewModel> trackViewModels = new List <TrackViewModel>(); using (var repository = factory.GetTrackPriceRepository()) { using (var currencyRatesrepository = factory.GetCurrencyRateRepository()) { foreach (var track in tracks) { var trackViewModel = ModelsMapper.GetTrackViewModel(track); if (trackViewModel != null) { trackViewModel.Price = PriceHelper.GetTrackPrice( repository, currencyRatesrepository, trackViewModel.Id, currencyCode.Value, priceLevel.Value); trackViewModels.Add(trackViewModel); } } } } using (var repository = factory.GetAlbumTrackRelationRepository()) { trackViewModels.ForEach(t => t.AlbumsCount = repository.Count(r => r.TrackId == t.Id)); } using (var repository = factory.GetVoteRepository()) { trackViewModels.ForEach(t => t.Rating = repository.GetAverageMark(t.Id)); } if (userId != null) { using (var repository = factory.GetOrderTrackRepository()) { foreach (var trackViewModel in trackViewModels) { trackViewModel.IsOrdered = repository.Exist(o => o.UserId == userId && o.TrackId == trackViewModel.Id); } } using (var repository = factory.GetPurchasedTrackRepository()) { foreach (var trackViewModel in trackViewModels) { trackViewModel.IsPurchased = repository.Exist(p => p.UserId == userId && p.TrackId == trackViewModel.Id); } } } return(trackViewModels); }