public async Task <bool> ScrapeForLocalMovieAsync(int id, CancellationToken cancellationToken = default) { var movie = await _context.Set <Movie>().FindAsync(id); if (movie is null) { throw EntityNotFoundException.Of <Movie>(id); } IScrapeSession session = null; foreach (var scraper in _scrapers.Where(x => x.Type == ScraperType.Local)) { session = await _movieService.GetScrapeSessionAsync(scraper.Source, scraper.Type, session, cancellationToken); await scraper.ScrapeAsync(session, cancellationToken); if (session.LocalImdbCodes.Contains(movie.ImdbCode)) { return(true); } } return(false); }
public async Task <MovieDto> GetAsync(int id, CancellationToken cancellationToken = default) { var movie = await _context.GetMovieDetailByIdAsync(id, cancellationToken); if (movie is null) { throw EntityNotFoundException.Of <Movie>(id); } return(_mapper.Map <MovieDto>(movie)); }
public async Task <TAggregateRoot> Find(int id) { var entity = await Fetch(Context.Set <TAggregateRoot>()) .Where(x => x.Id == id) .SingleOrDefaultAsync(); if (entity == null) { throw EntityNotFoundException.Of <TAggregateRoot>(id); } return(entity); }
public async Task <TransmissionContextDto> GetActiveContextByExternalIdAsync(int externalId, CancellationToken cancellationToken = default) { var context = await _context.TransmissionContexts() .FirstOrDefaultAsync(x => x.ExternalId == externalId && x.Statuses.Any() && x.Statuses.OrderBy(s => s.DateCreated).Last().Status == TransmissionStatusCode.Started, cancellationToken); if (context is null) { throw EntityNotFoundException.Of <TransmissionContext>(new { ExternalId = externalId }); } return(_mapper.Map <TransmissionContextDto>(context)); }
public async Task <TAggregateRoot[]> Find(params int[] ids) { var entities = await Fetch(Context.Set <TAggregateRoot>()) .Where(x => ids.Contains(x.Id)) .ToArrayAsync(); var missingIds = ids.Except(entities.Select(x => x.Id)).ToArray(); if (missingIds.Any()) { throw EntityNotFoundException.Of <TAggregateRoot>() .WithData("missingIds", string.Join(",", missingIds)); } return(entities); }
public async Task <GetItemQueryResponse> Handle(GetItemQuery query, CancellationToken token) { var item = (await repository.Query(x => x.ExternalId == query.ItemId, token)) .SingleOrDefault(); if (item == null) { throw EntityNotFoundException.Of <Item>(query.ItemId); } var supplier = await dbContext.Set <Supplier>() .FindAsync(new object[] { item.SupplierId }, token); return(new GetItemQueryResponse { Item = ItemDto.FromEntity(item, supplier) }); }
public async Task <GetOrderQueryResponse> Handle(GetOrderQuery query, CancellationToken token) { var order = (await repository.Query(x => x.ExternalId == query.OrderId, token)) .SingleOrDefault(); if (order == null) { throw EntityNotFoundException.Of <Order>(query.OrderId); } var itemIds = order.Items.Select(x => x.ItemId).ToList(); var items = await dbContext.Set <Item>() .Where(x => itemIds.Contains(x.Id)) .ToDictionaryAsync(k => k.Id, v => v, token); return(new GetOrderQueryResponse { Order = OrderDto.FromEntity(order, items) }); }
public async Task Handle(CreateOrderCommand command, CancellationToken token) { var order = Order.Create(clock); command.Id = order.ExternalId; var itemIds = command.Entries.Select(x => x.ItemId).ToList(); var items = await dbContext.Set <Item>() .Where(x => itemIds.Contains(x.ExternalId)) .ToDictionaryAsync(k => k.ExternalId, v => v, token); foreach (var entry in command.Entries) { if (!items.TryGetValue(entry.ItemId, out var item)) { throw EntityNotFoundException.Of <Item>(entry.ItemId); } order.Add(item, clock); } await dbContext.AddAsync(order, token); }
public async Task <LiveTransmissionStatusDto> GetLiveTransmissionStatusAsync(int movieId, int torrentId, CancellationToken cancellationToken = default) { var context = await _context.TransmissionContexts() .FirstOrDefaultAsync(x => x.MovieId == movieId && x.TorrentId == torrentId, cancellationToken); if (context is null) { throw EntityNotFoundException.Of <TransmissionContext>(new { MovieId = movieId, TorrentId = torrentId }); } var status = context.GetStatus(); if (status != TransmissionStatusCode.Started) { return(LiveTransmissionStatusDto.GetComplete(context.Name)); } // Get live status. var client = new Client(_options.Value.RpcUri.ToString()); var torrents = await client.TorrentGetAsync(TorrentFields.ALL_FIELDS, context.ExternalId); var torrent = torrents.Torrents.FirstOrDefault(); if (torrent is null) { return(LiveTransmissionStatusDto.GetComplete(context.Name)); } return(new LiveTransmissionStatusDto { Name = context.Name, Complete = !(torrent.PercentDone < 1.0), Stalled = torrent.IsStalled, Eta = torrent.ETA <= 0 ? null : torrent.ETA as int?, PercentComplete = torrent.PercentDone, }); }