Skip to content

Commit

Permalink
delete students related and add try catch in register
Browse files Browse the repository at this point in the history
  • Loading branch information
koykoy027 committed Mar 16, 2024
1 parent 29ad7dc commit 34c6316
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 187 deletions.
34 changes: 21 additions & 13 deletions app/Http/Controllers/Api/Auth/AuthenticationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Traits\HttpResponses;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class AuthenticationController extends Controller
Expand Down Expand Up @@ -39,20 +40,27 @@ public function login(LoginRequest $request)

public function register(RegisterRequest $request)
{
$user = User::create([
'created_by' => Auth::user()->id,
'updated_by' => Auth::user()->id,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
try {
DB::beginTransaction();
$user = User::create([
'created_by' => Auth::user()->id,
'updated_by' => Auth::user()->id,
'email' => $request->email,
'password' => Hash::make($request->password),
]);

UserProfile::create([
'id' => $user->id,
'firstname' => $request->firstname,
'middlename' => $request->middlename,
'lastname' => $request->lastname,
'gender' => $request->gender,
]);
UserProfile::create([
'id' => $user->id,
'firstname' => $request->firstname,
'middlename' => $request->middlename,
'lastname' => $request->lastname,
'gender' => $request->gender,
]);
DB::commit();
} catch (\Exception $error) {
DB::rollBack();
return $error;
}

return $this->success([
'user' => $user,
Expand Down
36 changes: 0 additions & 36 deletions app/Http/Controllers/StudentController.php

This file was deleted.

29 changes: 0 additions & 29 deletions app/Models/Student.php

This file was deleted.

14 changes: 1 addition & 13 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
Expand All @@ -22,12 +20,7 @@ class User extends Authenticatable
* @var array<int, string>
*/
protected $table = 'users';
protected $fillable = [
'email',
'password',
'created_by',
'updated_by',
];
protected $guarded = [];

const ACTIVE = 1;
const INACTIVE = 0;
Expand All @@ -43,11 +36,6 @@ public function user_profile()
return $this->hasOne(UserProfile::class, 'id');
}

public function students()
{
return $this->hasMany(Student::class, 'created_by');
}

/**
* The attributes that should be hidden for serialization.
*
Expand Down
8 changes: 1 addition & 7 deletions app/Models/UserProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ class UserProfile extends Model
use HasFactory;

protected $table = "user_profiles";
protected $fillable = [
'id',
'firstname',
'middlename',
'lastname',
'gender',
];
protected $guarded = [];

public function user_profile()
{
Expand Down
31 changes: 0 additions & 31 deletions database/factories/StudentFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
public function up(): void
{
Schema::create('user_profiles', function (Blueprint $table) {
$table->foreignId('id')->primary()->comment('users table primary key');
$table->id();
$table->string('firstname');
$table->string('lastname');
$table->string('middlename')->nullable();
$table->integer('gender');
$table->timestamps(false);
});
}

Expand Down
36 changes: 0 additions & 36 deletions database/migrations/2023_07_14_110717_create_students_table.php

This file was deleted.

16 changes: 0 additions & 16 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,7 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// \App\Models\User::factory(10)->create();
User::factory(10)->create();
UserProfile::factory(10)->create();
Student::factory(10)->create();


// User::factory(10)->create(10)->each(function ($user){
// $userID = $user->id;

// UserProfile::factory()->create([
// 'id' => $userID,
// 'firstname' => fake()->firstName(),
// 'middlename' => fake()->lastName(),
// 'lastname' => fake()->lastName(),
// 'gender' => fake()->numberBetween(1, 2),
// ]);
// });

}
}
5 changes: 0 additions & 5 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@

// token required
Route::middleware(['auth:sanctum'])->group(function () {
Route::controller(StudentController::class)->group(function () {
Route::get('/students', 'index');
Route::get('/students/{id}', 'show');
Route::post('/students', 'store');
});

Route::controller(UserController::class)->group(function () {
Route::get('/users', 'index');
Expand Down

0 comments on commit 34c6316

Please sign in to comment.