diff --git a/app/Actions/Students/CreateStudent.php b/app/Actions/Students/CreateStudent.php new file mode 100644 index 0000000..0233900 --- /dev/null +++ b/app/Actions/Students/CreateStudent.php @@ -0,0 +1,39 @@ +user()->school_id; + } + if (Student::where('first_name', $firstName)->where('last_name', $lastName) + ->where('school_id', $school_id)->exists()) { + throw new AuditionAdminException('Student already exists'); + } + + $newStudent = Student::create([ + 'first_name' => $firstName, + 'last_name' => $lastName, + 'grade' => $grade, + 'school_id' => $school_id, + 'optional_data' => $optionalData, + ]); + + return $newStudent; + } +} diff --git a/tests/Feature/app/Actions/Students/CreateStudentTest.php b/tests/Feature/app/Actions/Students/CreateStudentTest.php new file mode 100644 index 0000000..58ee283 --- /dev/null +++ b/tests/Feature/app/Actions/Students/CreateStudentTest.php @@ -0,0 +1,73 @@ +creator = app(CreateStudent::class); +}); + +it('can create a student with basic data', function () { + ($this->creator)( + 'John', + 'Doe', + 8, + [], + School::factory()->create()->id + ); + expect(Student::first())->toBeInstanceOf(Student::class); +}); + +it('can include optional data', function () { + ($this->creator)( + 'John', + 'Doe', + 8, + ['shirt_size' => 'M'], + School::factory()->create()->id + ); + $student = Student::first(); + expect($student->optional_data['shirt_size'])->toEqual('M'); +}); + +it('uses the current users school if none is specified', function () { + $user = User::factory()->create(); + $school = School::factory()->create(); + $user->school_id = $school->id; + $user->save(); + $this->actingAs($user); + ($this->creator)( + 'John', + 'Doe', + 8, + ['shirt_size' => 'M'], + ); + $student = Student::first(); + expect($student->school_id)->toEqual($school->id); +}); + +it('throws an error if we try to create a duplicate student (same name and school)', function () { + $school = School::factory()->create(); + ($this->creator)( + 'John', + 'Doe', + 8, + ['shirt_size' => 'M'], + $school->id + ); + ($this->creator)( + 'John', + 'Doe', + 11, + ['shirt_size' => 'XL'], + $school->id + ); +})->throws(AuditionAdminException::class, 'Student already exists');