/// <summary> /// Sets the current track for the parent book to this track /// Generates BookProgress if required /// </summary> public static void SetCurrentTrack(string userId, Guid trackId) { using (var db = new HonyomiContext()) { IndexedBook book = db.Files.Include(x => x.Book).SingleOrDefault(x => x.IndexedFileId == trackId)?.Book; if (book == null) { //can't update progress on a book the doesn't exist return; } BookProgress prog = db.BookProgresses.SingleOrDefault(x => x.BookId == book.IndexedBookId && x.UserId == userId); if (prog == null) { db.BookProgresses.Add(new BookProgress() { BookId = book.IndexedBookId, FileId = trackId, UserId = userId }); } else { prog.FileId = trackId; } db.SaveChanges(); } }
public static BookWithProgress GetUserBookProgress(string userId, Guid bookId) { using (var db = new HonyomiContext()) { IndexedBook book = db.Books.Include(x => x.Files).Single(x => x.IndexedBookId == bookId); if (book == null) { return(null); } BookProgress bookp = db.BookProgresses.SingleOrDefault(x => x.BookId == bookId && x.UserId == userId); BookWithProgress result = new BookWithProgress { FileProgresses = book.Files.Select(x => GetUserFileProgress(userId, x.IndexedFileId)) .ToArray(), Guid = book.IndexedBookId, CurrentTrackGuid = bookp?.FileId ?? book.Files.First().IndexedFileId, Title = book.Title, Author = book.Author, ISBN = book.ISBN }; return(result); } }
internal static void InsertNewBooks(IEnumerable <ScannedBook> books) { using (var db = new HonyomiContext()) { foreach (ScannedBook book in books) { if (!db.Books.Any(x => x.DirectoryPath == book.Path)) { var ibook = new IndexedBook() { DirectoryPath = book.Path, Title = book.Name }; var bookInfo = book.Files.Select(x => BookSearch.Search(x.Name)).FirstOrDefault(x => x != null); if (bookInfo != null) { ibook.Author = bookInfo.Author; ibook.ISBN = bookInfo.ISBN; ibook.Title = bookInfo.Title; } else if (book.Files.Count > 1) { ibook.Title = Util.LCS(book.Files[0].Name, book.Files[1].Name); } var files = book.Files.Select(x => new IndexedFile() { TrackIndex = x.Index, Filename = x.Name, Title = x.Name, FilePath = x.Path, MimeType = x.MimeType, Duration = x.Duration.TotalSeconds }).ToList(); ibook.Files = files; db.Books.Add(ibook); } } db.SaveChanges(); } }