Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions app/Services/ChapterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public function getChaptersForBook($userId, $bookId) {
::where('id', $bookId)
->where('user_id', $userId)
->first();

if (!$book) {
throw new \Exception('Book does not exist, or it belongs to a different user.');
}

$chapters = Chapter
::select(['id', 'name', 'read_count', 'word_count', 'unique_word_ids', 'processing_status'])
::select(['id', 'name', 'read_count', 'word_count', 'unique_word_ids', 'processing_status', 'updated_at'])
->where('book_id', $bookId)
->where('user_id', $userId)
->get();
Expand All @@ -54,7 +54,7 @@ public function getChaptersForBook($userId, $bookId) {
$chapters[$i]->wordCount->highlighted = -1;
$chapters[$i]->wordCount->new = -1;
}

$data = new \stdClass();
$data->book = $book;
$data->chapters = $chapters;
Expand All @@ -67,7 +67,7 @@ public function getChaptersBookCount($userId, $userUuid, $bookId) {
::where('id', $bookId)
->where('user_id', $userId)
->first();

if (!$book) {
throw new \Exception('Book does not exist, or it belongs to a different user.');
}
Expand Down Expand Up @@ -102,10 +102,10 @@ public function getChaptersBookCount($userId, $userUuid, $bookId) {
$chaptersWithWordCounts = [];
}
}

return true;
}

public function getChapterForEditor($userId, $chapterId) {
$chapter = Chapter::
select(['name', 'raw_text', 'type'])
Expand All @@ -118,7 +118,7 @@ public function getChapterForEditor($userId, $chapterId) {
}

$chapter->raw_text = str_replace(" NEWLINE \r\n", "\r\n", $chapter->raw_text);

return $chapter;
}

Expand All @@ -129,7 +129,7 @@ public function getChapterForReader($userId, $language, $languagesWithoutSpaces,
->where('language', $language)
->where('processing_status', ChapterProcessingStatusEnum::PROCESSED->value)
->first();

if (!$chapter) {
throw new \Exception('Chapter could not be found.');
}
Expand All @@ -140,7 +140,7 @@ public function getChapterForReader($userId, $language, $languagesWithoutSpaces,
->first();

$chapters = Chapter
::select(['id', 'name', 'read_count', 'word_count', 'unique_word_ids', 'processing_status'])
::select(['id', 'name', 'read_count', 'word_count', 'unique_word_ids', 'processing_status', 'updated_at'])
->where('user_id', $userId)
->where('book_id', $book->id)
->get();
Expand All @@ -163,7 +163,7 @@ public function getChapterForReader($userId, $language, $languagesWithoutSpaces,
$chapters[$i]->wordCount->known = -1;
$chapters[$i]->wordCount->highlighted = -1;
$chapters[$i]->wordCount->new = -1;

if ($chapters[$i]->processing_status !== ChapterProcessingStatusEnum::PROCESSED->value) {
continue;
}
Expand Down Expand Up @@ -191,7 +191,7 @@ public function getChapterForReader($userId, $language, $languagesWithoutSpaces,
$data->languageSpaces = !in_array($language, $languagesWithoutSpaces, true);
$data->chapters = $chapters;
$data->wordCount = $chapter->word_count;

return $data;
}

Expand All @@ -203,7 +203,7 @@ public function finishChapter($userId, $chapterId, $autoMoveWordsToKnown, $uniqu
foreach ($uniqueWords as $uniqueWordData) {
$saveData = [];
$saveData['read_count'] = $uniqueWordData->read_count;

if ($uniqueWordData->stage == 2) {
$saveData['stage'] = 0;
}
Expand Down Expand Up @@ -266,7 +266,7 @@ public function finishChapter($userId, $chapterId, $autoMoveWordsToKnown, $uniqu
}

$word->setStage($word->stage + 1);
$word->save();
$word->save();
}

return true;
Expand Down Expand Up @@ -298,14 +298,14 @@ public function createChapter($userId, $userUuid, $bookId, $chapterName, $chapte
$chapter->save();

$this->updateChapter($userId, $userUuid, $chapter->id, $chapter->name, $chapterText);

return true;
}

// updates the name and text of a chapter
public function updateChapter($userId, $userUuid, $chapterId, $chapterName, $chapterText) {
DB::disableQueryLog();

// retrieve chapter
$chapter = Chapter
::where('id', $chapterId)
Expand All @@ -315,15 +315,15 @@ public function updateChapter($userId, $userUuid, $chapterId, $chapterName, $cha
if (!$chapter) {
throw new \Exception('Chapter does not exist, or it belongs to a different user.');
}

// update chapter data
$chapter->raw_text = $chapterText;
$chapter->name = $chapterName;
$chapter->processing_status = ChapterProcessingStatusEnum::UNPROCESSED->value;
$chapter->save();

\App\Jobs\ProcessChapter::dispatch($userId, $userUuid, $chapter->id, $chapter->language);

return true;
}

Expand All @@ -343,10 +343,10 @@ public function processChapterText($userId, $chapterId) {
if (!$chapter) {
throw new \Exception('Chapter does not exist, or it belongs to a different user.');
}

// process text
$textBlock = new TextBlockService($userId, $chapter->language);
$textBlock = new TextBlockService($userId, $chapter->language);

if ($chapter->type == 'text') {
$textBlock->rawText = $chapter->raw_text;
$textBlock->tokenizeRawText();
Expand All @@ -355,7 +355,7 @@ public function processChapterText($userId, $chapterId) {
$textBlock->rawText = $chapter->raw_text;
$timeStamps = $textBlock->tokenizeRawSubtitles();
}

$textBlock->processTokenizedWords();
$textBlock->collectUniqueWords();
$textBlock->updateAllPhraseIds();
Expand All @@ -379,15 +379,15 @@ public function processChapterText($userId, $chapterId) {
$chapter->subtitle_timestamps = json_encode($timeStamps);
$chapter->processing_status = ChapterProcessingStatusEnum::PROCESSED->value;
$chapter->save();
$bookId = $chapter->book_id;

$bookId = $chapter->book_id;
});

$this->bookService->updateBookWordCount($userId, $bookId);
}

public function deleteChapter($userId, $chapterId) {

// retrieve chapter
$chapter = Chapter
::where('user_id', $userId)
Expand All @@ -409,7 +409,7 @@ public function deleteChapter($userId, $chapterId) {
}

public function retryFailedChapters($userId, $userUuid, $bookId) {

$chapters = Chapter
::where('user_id', $userId)
->where('book_id', $bookId)
Expand All @@ -428,4 +428,4 @@ public function retryFailedChapters($userId, $userUuid, $bookId) {

return true;
}
}
}
25 changes: 25 additions & 0 deletions database/factories/BookFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Database\Factories;

use App\Models\Book;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Book>
*/
class BookFactory extends Factory
{
protected $model = Book::class;

public function definition(): array
{
return [
'user_id' => 1,
'name' => fake()->sentence(3),
'cover_image' => '',
'language' => 'english',
'word_count' => 0,
];
}
}
34 changes: 34 additions & 0 deletions database/factories/ChapterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Database\Factories;

use App\Models\Chapter;
use App\Enums\ChapterProcessingStatusEnum;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Chapter>
*/
class ChapterFactory extends Factory
{
protected $model = Chapter::class;

public function definition(): array
{
return [
'user_id' => 1,
'book_id' => 1,
'name' => fake()->sentence(3),
'read_count' => 0,
'word_count' => fake()->numberBetween(50, 500),
'language' => 'english',
'raw_text' => fake()->paragraph(),
'processed_text' => '',
'unique_words' => '[]',
'unique_word_ids' => '[]',
'type' => 'text',
'subtitle_timestamps' => '',
'processing_status' => ChapterProcessingStatusEnum::PROCESSED->value,
];
}
}
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<env name="APP_MAINTENANCE_DRIVER" value="file"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_STORE" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
Expand Down
Loading