public async Task <Unit> Handle(AddSongsToPlaylistCommand command, CancellationToken cancellationToken) { var playlist = await _dbContext.Playlists.FindAsync(command.PlaylistId); if (playlist == null) { throw new NotFoundException("Playlist", command.PlaylistId); } CheckThatAlbumsExist(command); CheckThatSongsExist(command); var tracks = _dbContext.Tracks.Include(t => t.Album) .Where(t => command.TrackIds.Contains(t.Id) || command.AlbumIds.Contains(t.Album.Id)); await tracks.ForEachAsync(t => _dbContext.PlaylistTracks.Add(new PlaylistTrack { Track = t, Playlist = playlist }), cancellationToken); playlist.TotalTracks = playlist.PlaylistTracks.Count; await _dbContext.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(CreatePlaylistCommand request, CancellationToken cancellationToken) { var user = await _context.Users.FindAsync(request.UserId); if (user == null) { throw new NotFoundException(nameof(User), request.UserId); } var playlist = new Playlist { Name = request.Name, User = user, TotalTracks = 0 }; await _context.Playlists.AddAsync(playlist, cancellationToken); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }