Skip to content

Commit

Permalink
better apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Oct 13, 2023
1 parent eb2e535 commit 4a3a110
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,13 @@ public function removeAuthor(string $author): static

public function addTag(string $tag): static
{
$tag = ucwords(strtolower($tag), self::UCWORDS_SEPARATORS);
if (null === $this->tags) {
$this->tags = [];
}

if (!in_array($tag, $this->tags, true) && '' !== $tag) {
$this->tags[] = ucwords(strtolower($tag), self::UCWORDS_SEPARATORS);
if ('' !== $tag && !in_array($tag, $this->tags, true)) {
$this->tags[] = $tag;
}
$this->tags = array_values($this->tags);

Expand Down
38 changes: 20 additions & 18 deletions src/Service/BookSuggestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,36 @@ public function __construct(private ParameterBagInterface $parameterBag, private
*/
public function getCategorySuggestions(Book $book): array
{
$key = $this->parameterBag->get('GOOGLE_API_KEY');

$suggestions = self::EMPTY_SUGGESTIONS;

if ('' === $key || !is_string($key)) {
return $suggestions;
}
$cache = new FilesystemAdapter();

$mainAuthor = current($book->getAuthors());

$query = ['q' => 'title:'.$book->getTitle().' author:'.$mainAuthor, 'fields' => 'title,author_name,key,cover_i,subject'];

$client = new \GuzzleHttp\Client();
$cacheKey = $this->slugger->slug('openlib-'.implode('-', $query));

$results = $client->request('GET', 'https://openlibrary.org/search.json', ['query' => $query])->getBody()->getContents();
return $cache->get($cacheKey, function (ItemInterface $item) use ($query): array {
$item->expiresAfter(3600);

$results = json_decode($results, true);
$client = new \GuzzleHttp\Client();

if (!is_array($results) || !array_key_exists('docs', $results)) {
return $suggestions;
}
foreach ($results['docs'] as $result) {
foreach ($result['subject'] as $category) {
$suggestions['tags'][$category] = $category;
$results = $client->request('GET', 'https://openlibrary.org/search.json', ['query' => $query])->getBody()->getContents();

$results = json_decode($results, true);

$suggestions = self::EMPTY_SUGGESTIONS;

if (!is_array($results) || !array_key_exists('docs', $results)) {
return $suggestions;
}
foreach ($results['docs'] as $result) {
foreach ($result['subject'] as $category) {
$suggestions['tags'][$category] = $category;
}
}
}

return $suggestions;
return $suggestions;
});
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Twig/InlineEditBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function activateEditing(): void
}

#[LiveAction]
public function usesuggestion(#[LiveArg] string $field, #[LiveArg] string $suggestion): void
public function usesuggestion(#[LiveArg] string $field, #[LiveArg] string $suggestion, EntityManagerInterface $entityManager): void
{
$this->isEditing = true;
$to_call = 'set'.ucfirst($field);
Expand All @@ -60,6 +60,11 @@ public function usesuggestion(#[LiveArg] string $field, #[LiveArg] string $sugge
$this->book->$to_call($value);
}
}
$entityManager->flush();
$this->dispatchBrowserEvent('manager:flush');
$this->isEditing = false;

$this->flashMessage = ' book updated';
}

/**
Expand Down

0 comments on commit 4a3a110

Please sign in to comment.