public async Task <ActionResult> Update() { DirectoryScanner directoryScanner = new DirectoryScanner(); BookInfoScanner infoScanner = new BookInfoScanner(); foreach (MediaDirectory dir in _context.MediaDirectories.ToList()) { List <AudioBook> scannedBooks = directoryScanner.ScanForBooks(dir.DirectoryPath); foreach (AudioBook scannedBook in scannedBooks) { if (_context.AudioBooks.Any(x => x.AudioBookDirectoryPath == scannedBook.AudioBookDirectoryPath)) { continue; } //todo: info scanning EntityEntry <AudioBook> bookEntry = _context.AudioBooks.Add(scannedBook); var entitesCreated = await _context.SaveChangesAsync(); //todo: log entries created } } return(Ok()); }
public async Task <ActionResult <AudioFileProgress> > Progress([FromBody] AudioFileProgressUpdateRequest request) { if (request.ProgressID.HasValue) { AudioFileProgress progress = await _context.FileProgresses.SingleOrDefaultAsync(x => x.Id == request.ProgressID.Value && x.UserId == User.GetUserId()); progress.Progress = TimeSpan.FromTicks(request.Ticks); progress.IsFinished = request.IsFinished; EntityEntry <AudioFileProgress> updateEntity = _context.FileProgresses.Update(progress); await _context.SaveChangesAsync(); return(updateEntity.Entity); } AudioBookProgress bookProgress = await _context.BookProgresses.SingleOrDefaultAsync(x => x.AudioBookId == request.AudioBookID && x.UserId == User.GetUserId()); if (bookProgress == null) { var audioFileInfo = await _context.AudioFiles.SingleOrDefaultAsync(x => x.Id == request.AudioFileID); if (audioFileInfo == null) { _logger.LogError($"Couldn't create progress for file without database entry"); return(BadRequest("The requested audio file could not be found")); } EntityEntry <AudioBookProgress> bookProgressEntity = await _context.BookProgresses.AddAsync(new AudioBookProgress() { AudioBookId = request.AudioBookID, CurrentTrack = audioFileInfo.TrackNumber, IsFinished = false, UserId = User.GetUserId(), FileProgresses = new List <AudioFileProgress>() { new AudioFileProgress() { UserId = User.GetUserId(), AudioFileId = request.AudioFileID, IsFinished = request.IsFinished, Progress = TimeSpan .FromTicks(request .Ticks) } } }); await _context.SaveChangesAsync(); return(bookProgressEntity.Entity.FileProgresses.First()); } EntityEntry <AudioFileProgress> progressEntity = await _context.FileProgresses.AddAsync(new AudioFileProgress() { UserId = User.GetUserId(), AudioFileId = request.AudioFileID, IsFinished = request.IsFinished, Progress = TimeSpan.FromTicks(request.Ticks), BookProgressId = bookProgress.Id }); await _context.SaveChangesAsync(); return(progressEntity.Entity); }
public async Task <ActionResult> Confirm([FromBody] BookInfoConfirmationModel model) { try { BookInfoScanner scanner = new BookInfoScanner(); var gBook = await scanner.GetGoogleBookAsync(model.GoogleBooksId); if (gBook == null) { return(BadRequest($"Google volume '{model.GoogleBooksId}' not found")); } AudioBook kBook = await _context.AudioBooks.SingleOrDefaultAsync(x => x.Id == model.KikiBookId); if (kBook == null) { return(BadRequest($"Kiki audiobook with ID '{model.KikiBookId.ToString()}' not found")); } kBook.Title = gBook.Title; foreach (string newAuthor in gBook.Authors.Where(x => kBook.Authors.All(a => a.Author.Name != x))) { if (_context.Authors.All(x => x.Name != newAuthor)) { kBook.Authors.Add(new BookAuthor(kBook, new Author(newAuthor))); } else { var author = await _context.Authors.SingleOrDefaultAsync(x => x.Name == newAuthor); kBook.Authors.Add(new BookAuthor(kBook, author)); } } kBook.Description = gBook.Description; if (!string.IsNullOrEmpty(gBook.ThumbnailLink)) { using (HttpClient client = new HttpClient()) { var thumbnailBytes = await client.GetByteArrayAsync(gBook.ThumbnailLink); kBook.ThumbnailData = Convert.ToBase64String(thumbnailBytes); } } kBook.Language = gBook.Language; if (!string.IsNullOrEmpty(gBook.Publisher)) { if (_context.Publishers.All(x => x.Name != gBook.Publisher)) { kBook.Publisher = new Publisher() { Name = gBook.Publisher }; } else { kBook.Publisher = await _context.Publishers.SingleOrDefaultAsync(x => x.Name == gBook.Publisher); } } if (gBook.Published.HasValue) { kBook.Year = gBook.Published.Value.Year; } kBook.GoogleBooksID = gBook.GoogleBooksID; foreach (var identifier in gBook.IndustryIdentifiers) { if (identifier.IdentificationType == "ISBN10") { kBook.ISBN10 = identifier.IdentificationCode; } else if (identifier.IdentificationType == "ISBN13") { kBook.ISBN13 = identifier.IdentificationCode; } } var changed = await _context.SaveChangesAsync(); return(Ok()); } catch (Exception ex) { if (ex is GoogleApiException gae) { string err = $"Google API error: {gae.Error.Message}"; _logger.LogError(err); return(BadRequest(err)); } if (ex is DbUpdateException due) { _logger.LogError($"Error saving changes to book with ID {model.KikiBookId} from Google book {model.GoogleBooksId}: {due.Message}"); } else { _logger.LogError($"Unexpected error while updating book: {ex.Message}"); } return(StatusCode((int)HttpStatusCode.InternalServerError)); } }