Skip to content

Commit

Permalink
Save parent session for derived sessions
Browse files Browse the repository at this point in the history
This saves the parent session of derived sessions (e.g. "wrong answers
from session x") in an additional database column.
  • Loading branch information
levinuss committed May 8, 2024
1 parent f836910 commit 4f38128
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/SessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function newFromIncorrect(Request $request, Session $session)
$newSession->name = $deck->name;
$newSession->current_question_id = $deck->questions()->first()->id;
$newSession->user_id = Auth::id();
$newSession->parent_session_id = $session->id;
$newSession->save();

return response()->json($newSession);
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public function answerChoices()
{
return $this->hasMany(AnswerChoice::class);
}

public function parentSession()
{
return $this->belongsTo(Session::class, 'parent_session_id');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
public function up(): void
{
Schema::table('sessions', function (Blueprint $table) {
$table->bigInteger('parent_session_id')->unsigned()->nullable();
$table->foreign('parent_session_id')->references('id')->on('sessions');
});
}

public function down(): void
{
Schema::table('', function (Blueprint $table) {
//
});
}
};

0 comments on commit 4f38128

Please sign in to comment.