Exemple #1
0
        public async Task <IBook> AddBookAsync(IStorageFile file)
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            var document = await DjvuDocument.LoadAsync(file);

            var bookDto = new EfBookDto
            {
                PageCount       = document.PageCount,
                Title           = Path.GetFileNameWithoutExtension(file.Name),
                LastOpeningTime = DateTime.Now
            };

            _context.Books.Add(bookDto);
            await _context.SaveChangesAsync();

            var booksFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Books", CreationCollisionOption.OpenIfExists);

            var bookFile = await file.CopyAsync(booksFolder, $"{bookDto.Id}.djvu", NameCollisionOption.ReplaceExisting);

            bookDto.BookPath = bookFile.Path;
            await _context.SaveChangesAsync();

            var book = new EfBook(bookDto, _context, _books);
            await book.UpdateThumbnailAsync();

            _books.Add(book);
            return(book);
        }
Exemple #2
0
        public async Task RemoveAsync()
        {
            if (!_books.Contains(this))
            {
                return;
            }

            _books.Remove(this);
            _context.Books.Remove(_efBookDto);
            await _context.SaveChangesAsync();

            var bookFile = await StorageFile.GetFileFromPathAsync(BookPath);

            await bookFile.DeleteAsync();

            var thumbnailFile = await StorageFile.GetFileFromPathAsync(ThumbnailPath);

            await thumbnailFile.DeleteAsync();
        }
        public async Task MigrateAsync()
        {
            SqliteBook[]     books     = null;
            SqliteBookmark[] bookmarks = null;

            await Task.Factory.StartNew(() =>
            {
                using (var connection = new SQLiteConnection(DbPath))
                {
                    books     = connection.Table <SqliteBook>().ToArray();
                    bookmarks = connection.Table <SqliteBookmark>().ToArray();
                }
            });

            using (var context = new EfBooksContext())
            {
                context.Books.AddRange(books.Select(book => new EfBookDto
                {
                    BookPath        = book.Path,
                    LastOpenedPage  = book.LastOpenedPage,
                    LastOpeningTime = book.LastOpeningTime,
                    PageCount       = book.PageCount,
                    ThumbnailPath   = book.ThumbnailPath,
                    Title           = book.Title,
                    Bookmarks       = bookmarks
                                      .Where(bookmark => bookmark.BookId == book.Id)
                                      .Select(bookmark => new EfBookmarkDto
                    {
                        PageNumber = bookmark.PageNumber,
                        Title      = bookmark.Title
                    }).ToList()
                }));
                await context.SaveChangesAsync();
            }

            var dbFile = await StorageFile.GetFileFromPathAsync(DbPath);

            await dbFile.DeleteAsync();
        }
Exemple #4
0
 public async Task SaveChangesAsync()
 {
     await _context.SaveChangesAsync();
 }