Skip to content

Commit

Permalink
Controller refactoring 9. (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
simjanos-dev committed Feb 28, 2024
1 parent 85a0eb0 commit deb9212
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 58 deletions.
66 changes: 12 additions & 54 deletions app/Http/Controllers/VocabularyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use App\Http\Requests\Vocabulary\CreatePhraseRequest;
use App\Http\Requests\Vocabulary\UpdatePhraseRequest;
use App\Http\Requests\Vocabulary\GetPhraseRequest;
use App\Http\Requests\Vocabulary\DeletePhraseRequest;

class VocabularyController extends Controller
{
Expand Down Expand Up @@ -119,12 +120,12 @@ public function createPhrase(CreatePhraseRequest $request) {
$translation = is_null($request->translation) ? '' : $request->translation;

try {
$this->vocabularyService->createPhrase($userId, $language, $words, $stage, $reading, $translation);
$phraseId = $this->vocabularyService->createPhrase($userId, $language, $words, $stage, $reading, $translation);
} catch (\Exception $e) {
abort(404, $e->getMessage());
}

return response()->json('Phrase has been successfully created.', 200);
return response()->json($phraseId, 200);
}

public function updatePhrase(UpdatePhraseRequest $request) {
Expand Down Expand Up @@ -162,61 +163,18 @@ public function updatePhrase(UpdatePhraseRequest $request) {
return response()->json('Phrase has been successfully updated.', 200);
}

public function deletePhrase(Request $request) {
$selectedLanguage = Auth::user()->selected_language;
$phraseId = $request->id;
public function deletePhrase(DeletePhraseRequest $request) {
$userId = Auth::user()->id;
$language = Auth::user()->selected_language;
$phraseId = $request->post('phraseId');

$lessons = Lesson
::where('user_id', Auth::user()->id)
->where('language', $selectedLanguage)
->get();

foreach($lessons as $lesson) {
$words = $lesson->getProcessedText();
$lessonChanged = false;

// delete phrase id from lesson words
foreach ($words as $word) {
$index = array_search($phraseId, $word->phrase_ids);
if ($index !== false) {
$modifiedPhraseIds = $word->phrase_ids;
array_splice($modifiedPhraseIds, $index, 1);
$word->phrase_ids = $modifiedPhraseIds;
$lessonChanged = true;
}
}

// save lesson if changed
if ($lessonChanged) {
$lesson->setProcessedText($words);
$lesson->save();
}
}

// delete phrase id from example sentences
$exampleSentences = ExampleSentence
::where('user_id', Auth::user()->id)
->where('language', $selectedLanguage)
->get();

DB::beginTransaction();
foreach ($exampleSentences as $exampleSentence) {
$exampleSentence->deletePhraseId($phraseId);
try {
$this->vocabularyService->deletePhrase($userId, $language, $phraseId);
} catch (\Exception $e) {
abort(404, $e->getMessage());
}

DB::commit();

ExampleSentence
::where('user_id', Auth::user()->id)
->where('target_type', 'phrase')
->where('target_id', $request->id)
->delete();

Phrase
::where('user_id', Auth::user()->id)
->where('language', $selectedLanguage)
->where('id', $request->id)
->delete();
return response()->json('Phrase has been successfully deleted.', 200);
}

public function getExampleSentence($targetId, $targetType) {
Expand Down
30 changes: 30 additions & 0 deletions app/Http/Requests/Vocabulary/DeletePhraseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests\Vocabulary;

use Illuminate\Foundation\Http\FormRequest;

class DeletePhraseRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'phraseId' => 'required|numeric|gte:0'
];
}
}
70 changes: 69 additions & 1 deletion app/Services/VocabularyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function createPhrase($userId, $language, $words, $stage, $reading, $tran

DB::commit();

return true;
return $phrase->id;
}

public function updatePhrase($userId, $phraseId, $phraseData, $phraseStage = null) {
Expand Down Expand Up @@ -148,4 +148,72 @@ public function getPhrase($userId, $phraseId) {

return $phrase;
}

public function deletePhrase($userId, $language, $phraseId) {

$phrase = Phrase
::where('user_id', $userId)
->where('language', $language)
->where('id', $phraseId)
->first();

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

// remove phrase ids from text words
$chapters = Lesson
::where('user_id', $userId)
->where('language', $language)
->get();

foreach($chapters as $chapter) {
$words = $chapter->getProcessedText();
$chapterChanged = false;

// delete phrase id from lesson words
foreach ($words as $word) {
$index = array_search($phraseId, $word->phrase_ids);
if ($index !== false) {
$modifiedPhraseIds = $word->phrase_ids;
array_splice($modifiedPhraseIds, $index, 1);
$word->phrase_ids = $modifiedPhraseIds;
$chapterChanged = true;
}
}

// save lesson if changed
if ($chapterChanged) {
$chapter->setProcessedText($words);
$chapter->save();
}
}

// remove phrase ids from example sentence words
$exampleSentences = ExampleSentence
::where('user_id', $userId)
->where('language', $language)
->get();

DB::beginTransaction();
foreach ($exampleSentences as $exampleSentence) {
$exampleSentence->deletePhraseId($phraseId);
}

DB::commit();

ExampleSentence
::where('user_id', $userId)
->where('target_type', 'phrase')
->where('target_id', $phraseId)
->delete();

Phrase
::where('user_id', $userId)
->where('language', $language)
->where('id', $phraseId)
->delete();

return true;
}
}
4 changes: 2 additions & 2 deletions resources/js/components/Text/TextBlockGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -993,8 +993,8 @@
// delete phrase
this.text.phrases.splice(deletedPhraseIndex, 1);
axios.post('/vocabulary/phrase/delete', {
id: deletedPhraseId
axios.post('/vocabulary/phrases/delete', {
phraseId: deletedPhraseId
}).then(function (response) {
});
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
Route::get ('/vocabulary/phrases/get/{phraseId}', [App\Http\Controllers\VocabularyController::class, 'getPhrase']);
Route::post('/vocabulary/phrases/create', [App\Http\Controllers\VocabularyController::class, 'createPhrase']);
Route::post('/vocabulary/phrases/update', [App\Http\Controllers\VocabularyController::class, 'updatePhrase']);
Route::post('/vocabulary/phrase/delete', [App\Http\Controllers\VocabularyController::class, 'deletePhrase']);
Route::post('/vocabulary/phrases/delete', [App\Http\Controllers\VocabularyController::class, 'deletePhrase']);
Route::post('/vocabulary/save-example-sentence', [App\Http\Controllers\VocabularyController::class, 'saveExampleSentence']);
Route::post('/vocabulary/search', [App\Http\Controllers\VocabularyController::class, 'search']);
Route::post('/vocabulary/export-to-csv', [App\Http\Controllers\VocabularyController::class, 'exportToCsv']);
Expand Down

0 comments on commit deb9212

Please sign in to comment.