Skip to content

Commit

Permalink
add create action
Browse files Browse the repository at this point in the history
  • Loading branch information
POMXARK committed Jun 4, 2023
1 parent a1fbd8c commit aa24937
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 21 deletions.
18 changes: 18 additions & 0 deletions app/Http/Controllers/PatientController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Http\Controllers;

use App\Models\Patient;

class PatientController extends Controller
{
public function create()
{
$data = request()->validate([
'first_name' => 'required',
'last_name' => 'required',
'birthdate' => 'required',
]);
Patient::create($data);
}
}
13 changes: 13 additions & 0 deletions app/Models/Patient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Patient extends Model
{
use HasFactory;

protected $fillable = ['first_name','last_name','birthdate'];
}
94 changes: 94 additions & 0 deletions app/Policies/PatientPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Policies;

use App\Models\Patient;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class PatientPolicy
{
use HandlesAuthorization;

/**
* Determine whether the user can view any models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function viewAny(User $user)
{
//
}

/**
* Determine whether the user can view the model.
*
* @param \App\Models\User $user
* @param \App\Models\Patient $patient
* @return \Illuminate\Auth\Access\Response|bool
*/
public function view(User $user, Patient $patient)
{
//
}

/**
* Determine whether the user can create models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function create(User $user)
{
//
}

/**
* Determine whether the user can update the model.
*
* @param \App\Models\User $user
* @param \App\Models\Patient $patient
* @return \Illuminate\Auth\Access\Response|bool
*/
public function update(User $user, Patient $patient)
{
//
}

/**
* Determine whether the user can delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\Patient $patient
* @return \Illuminate\Auth\Access\Response|bool
*/
public function delete(User $user, Patient $patient)
{
//
}

/**
* Determine whether the user can restore the model.
*
* @param \App\Models\User $user
* @param \App\Models\Patient $patient
* @return \Illuminate\Auth\Access\Response|bool
*/
public function restore(User $user, Patient $patient)
{
//
}

/**
* Determine whether the user can permanently delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\Patient $patient
* @return \Illuminate\Auth\Access\Response|bool
*/
public function forceDelete(User $user, Patient $patient)
{
//
}
}
23 changes: 23 additions & 0 deletions database/factories/PatientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Patient>
*/
class PatientFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
//
];
}
}
34 changes: 34 additions & 0 deletions database/migrations/2023_06_03_234547_create_patients_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('patients', function (Blueprint $table) {
$table->id();
$table->string('first_name', 50);
$table->string('last_name', 50);
$table->date('birthdate');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('patients');
}
};
19 changes: 19 additions & 0 deletions database/seeders/PatientSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class PatientSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
}
}
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\PatientController;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -16,3 +17,5 @@
Route::get('/', function () {
return view('welcome');
});

Route::post('/patient/create', [PatientController::class, 'create']);
21 changes: 0 additions & 21 deletions tests/Feature/ExampleTest.php

This file was deleted.

45 changes: 45 additions & 0 deletions tests/Feature/PatientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Tests\Feature;

use App\Models\Patient;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PatientTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function the_application_returns_a_successful_response()
{
$this->withoutExceptionHandling();
$response = $this->get('/');
$response->assertStatus(200);
}

/** @test */
public function a_patient_can_be_added()
{
$this->withoutExceptionHandling();
$response = $this->post('/patient/create', [
'first_name' => 'Markus',
'last_name' => 'Cobb',
'birthdate' => '16/11/2002',
]);

$response->assertOk();
$this->assertCount(1, Patient::all());
}

/** @test */
public function is_required()
{
$response = $this->post('/patient/create', [
'first_name' => '',
'last_name' => '',
'birthdate' => '',
]);

$response->assertSessionHasErrors(['first_name', 'last_name', 'birthdate']);
}
}

0 comments on commit aa24937

Please sign in to comment.