Ejemplo n.º 1
0
        public AuthorResource EnsureAuthor(string authorId, string goodreadsEditionId, string authorName, bool?monitored = null)
        {
            var result = Author.All().FirstOrDefault(v => v.ForeignAuthorId == authorId);

            if (result == null)
            {
                var lookup = Author.Lookup("readarr:" + goodreadsEditionId);
                var author = lookup.First();
                author.QualityProfileId  = 1;
                author.MetadataProfileId = 1;
                author.Path       = Path.Combine(AuthorRootFolder, author.AuthorName);
                author.Monitored  = true;
                author.AddOptions = new Core.Books.AddAuthorOptions();
                Directory.CreateDirectory(author.Path);

                result = Author.Post(author);
                Commands.WaitAll();
                WaitForCompletion(() => Books.GetBooksInAuthor(result.Id).Count > 0);
            }

            var changed = false;

            if (result.RootFolderPath != AuthorRootFolder)
            {
                changed = true;
                result.RootFolderPath = AuthorRootFolder;
                result.Path           = Path.Combine(AuthorRootFolder, result.AuthorName);
            }

            if (monitored.HasValue)
            {
                if (result.Monitored != monitored.Value)
                {
                    result.Monitored = monitored.Value;
                    changed          = true;
                }
            }

            if (changed)
            {
                result.NextBook = result.LastBook = null;
                Author.Put(result);
            }

            return(result);
        }