public void Add(BookDownloadModel item)
 {
     using (BookDataContext context = BookDataContext.Connect())
     {
         context.Downloads.InsertOnSubmit(item);
         context.SubmitChanges();
     }
 }
Exemplo n.º 2
0
        public bool DownloadBook(CatalogBookItemModel catalogBookItemModel, int catalogId, bool fullBook = true)
        {
            var link = fullBook
                           ? GetDownloadLink(catalogBookItemModel.Links.ToArray())
                           : GetDownloadLink(catalogBookItemModel.TrialLink);

            if (link == null)
            {
                return(false);
            }

            if (!_bookDownloader.IsStarted)
            {
                _bookDownloader.Start();
            }

            var downloadModel = new BookDownloadModel
            {
                Path = link.Url,
                Type = GetBookType(link.Type),

                IsZip             = CheckIsZip(link.Type),
                Name              = catalogBookItemModel.Title,
                Author            = catalogBookItemModel.Author,
                Description       = catalogBookItemModel.Description,
                AcquisitionUrl    = catalogBookItemModel.AcquisitionLink != null ? catalogBookItemModel.AcquisitionLink.Url : null,
                AcquisitionType   = catalogBookItemModel.AcquisitionLink != null ? catalogBookItemModel.AcquisitionLink.Type : null,
                AcquisitionPrices = catalogBookItemModel.AcquisitionLink != null?catalogBookItemModel.AcquisitionLink.ToString() : null,
                                        DataSourceID  = catalogId,
                                        IsTrial       = !fullBook,
                                        CatalogItemId = catalogBookItemModel.Id
            };

            int oldQueueCount = _container.Count;

            _bookDownloadsRepository.Add(downloadModel);

            var downloadViewModel = new DownloadItemDataModel(downloadModel);

            _container.Enqueue(downloadViewModel);

            if (_container.Count == oldQueueCount)
            {
                int index = _container.GetDataModelIndex(downloadViewModel);
                if (index > -1)
                {
                    var viewModel = _container.Items[index];
                    if (viewModel.Status == DownloadStatus.Error)
                    {
                        viewModel.Status = DownloadStatus.Pending;
                        _bookDownloadsRepository.Remove(downloadModel.DownloadID);
                    }
                }
            }
            return(true);
        }
 public DownloadItemDataModel(BookDownloadModel model)
 {
     DownloadID        = model.DownloadID;
     DataSourceID      = model.DataSourceID;
     Path              = model.Path;
     Name              = model.Name;
     Type              = model.Type;
     IsZip             = model.IsZip;
     Status            = DownloadStatus.Pending;
     Canceled          = false;
     BookID            = Guid.Empty;
     IsTrial           = model.IsTrial;
     CatalogItemId     = model.CatalogItemId;
     Author            = model.Author;
     Description       = model.Description;
     AcquisitionUrl    = model.AcquisitionUrl;
     AcquisitionType   = model.AcquisitionType;
     AcquisitionPrices = model.AcquisitionPrices;
 }
 public void Remove(int downloadId)
 {
     lock (_locker)
     {
         using (BookDataContext context = BookDataContext.Connect())
         {
             try
             {
                 BookDownloadModel model = context.Downloads.FirstOrDefault(t => t.DownloadID == downloadId);
                 if (model != null)
                 {
                     context.Downloads.DeleteOnSubmit(model);
                     context.SubmitChanges(ConflictMode.ContinueOnConflict);
                 }
             }
             catch (ChangeConflictException)
             {
                 context.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                 context.SubmitChanges();
             }
         }
     }
 }