public Domain.DataTransfer.BoughtSong BuySong(int songId, int userId, decimal discountForBuyAllAlbum = 0) { if (songId <= 0 || userId <= 0) { throw new ArgumentException("userId is less then 1 or songId is less then 1 in musicStoreService in BuySong", "userId or songId"); } User user = _userRepository.GetItem(userId); if (user == null) { throw new Exception("User is null"); } Song song = _songRepository.GetItem(songId); if (song == null) { throw new Exception("Song is null"); } if (user.Money < song.Price) { throw new Exception($"User has not enough money for buy {song.Name} song"); } decimal songBoughtPrice = 0; songBoughtPrice = discountForBuyAllAlbum > 0 ? song.Price - (song.Price * (discountForBuyAllAlbum / 100)) : song.Price; BoughtSong boughtSong = new BoughtSong() { BoughtPrice = songBoughtPrice, IsVisible = true, BoughtDate = DateTime.Now, Song = song, User = user }; user.Money -= songBoughtPrice; _boughtSongRepository.Create(boughtSong); _userRepository.Update(user); var result = _mapBoughtSong.AutoMap(boughtSong); return(result); }
public IList <Domain.DataTransfer.Song> GetSongsListFromAlbumAvailableForBuyByUser(int albumId, int userId) { if (albumId <= 0) { throw new ArgumentException($"{nameof(albumId)} is less then 1 in musicStoreDisplayService GetSongsListAvailableForBuyByUser", nameof(albumId)); } if (userId <= 0) { throw new ArgumentException($"{nameof(userId)} is less then 1 in musicStoreDisplayService GetSongsListAvailableForBuyByUser", nameof(userId)); } var user = _userRepository.GetItem(userId); if (user == null || user.BoughtSongs == null) { throw new Exception("user is null or user.BoughtSongs is null"); } var album = _albumRepository.GetItem(albumId); if (album == null || album.Songs == null) { throw new Exception("album is null or album.Songs is null"); } var domainSongsList = new List <Domain.DataTransfer.Song>(); foreach (var albumSong in album.Songs) { var coincidence = user.BoughtSongs.Any(x => x.Song.Id == albumSong.Id); if (!coincidence) { domainSongsList.Add(_mapSong.AutoMap(albumSong)); } } return(domainSongsList); }
public Domain.DataTransfer.UserAccount GetUserData(string identityId) { if (string.IsNullOrEmpty(identityId)) { throw new ArgumentException("User id is not valid", "userId"); } var resultOfParse = Guid.TryParse(identityId, out var guidIdentityId); if (resultOfParse == false) { throw new ArgumentException("Can not parse string to guid", "identityId"); } var result = GetItemWithGuidId(guidIdentityId); if (result == null) { throw new Exception("User not found"); } return(_mapUser.AutoMap(result)); }