Fix Person

This commit is contained in:
R
2022-11-15 10:43:30 +07:00
parent 961be7568d
commit 897a531326
4 changed files with 56 additions and 8 deletions

View File

@@ -64,13 +64,15 @@ class PersonController extends Controller
'main_address_id',
'domicile_address_id',
]);
$personData['last_weight_kg'] = $request->weight ?? null;
$personData['last_height_cm'] = $request->height ?? null;
$personData = array_merge($personData, [
'owner_user_id' => auth()->user()->id,
]);
$personModel = Person::query()->create($personData);
return Helper::responseJson(['persons' => $personModel], Response::HTTP_CREATED, 'Data berhasil di buat');
return Helper::responseJson(['persons' => new PersonResource($personModel)], Response::HTTP_CREATED, 'Data berhasil di buat');
}
/**
@@ -114,8 +116,11 @@ class PersonController extends Controller
'main_address_id',
'domicile_address_id',
]);
$personData['last_weight_kg'] = $request->weight ?? null;
$personData['last_height_cm'] = $request->height ?? null;
$family->update($personData);
$family->fill($personData);
$family->save();
if ($request->hasFile('user_avatar')) {
$pathFileAvatar = File::storeFile('avatar', $family->id, $request->file('user_avatar'));
@@ -154,7 +159,7 @@ class PersonController extends Controller
]);
}
return Helper::responseJson(data: ['persons' => $family], message: 'Data Berhasil di update');
return Helper::responseJson(data: ['persons' => PersonResource::make($family)], message: 'Data Berhasil di update');
} elseif (Gate::forUser(auth()->user())->denies('update-person', $family)) {
abort(Response::HTTP_FORBIDDEN, 'Tidak bisa update karena bukan pemilik!');
}

View File

@@ -17,21 +17,30 @@ class PersonResource extends JsonResource
{
return [
'id' => $this->id,
'name_prefix' => $this->name_prefix,
'name' => $this->name,
'name_suffix' => $this->name_suffix,
'full_name' => $this->full_name,
'birth_place' => $this->birth_place,
'gender' => $this->gender,
'birth_date' => $this->birth_date,
'gender' => !empty($this->gender) ? ($this->gender == 'M' ? 'P' : 'W') : null, // For App
'citizenship' => $this->citizenship,
'phone' => $this->phone,
'email' => $this->email,
'blood_type' => $this->blood_type,
'weight' => $this->weight,
'height' => $this->height,
'weight' => (float) $this->last_weight_kg,
'height' => (float) $this->last_height_cm,
'relation_with_owner' => ($this->user && $this->user->person_id == $this->id) ? 'Self' : $this->pivot->relation_with_owner ?? null,
'marital_status' => $this->marital_status,
'last_education' => $this->last_education,
'current_employment' => $this->current_employment,
'nik' => $this->nik,
'religion' => $this->religion,
'is_deceased' => $this->is_deceased == 1,
'deceased_at' => $this->deceased_at,
'avatar_url' => $this->avatar->url ?? asset('images/default-doctor-avatar.png'),
'main_address' => $this->current_address,
'domicile_address' => $this->domicile_address,
];
}
}

View File

@@ -27,8 +27,8 @@ class Person extends Model
'last_education',
'religion',
'blood_type',
'weight',
'height',
'last_weight_kg',
'last_height_cm',
'is_deceased',
'deceased_at',
'marital_status',

View File

@@ -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::table('persons', function (Blueprint $table) {
$table->float('last_weight_kg', 5, 2, true)->nullable()->after('blood_type');
$table->float('last_height_cm', 5, 2, true)->nullable()->after('blood_type');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('persons', function (Blueprint $table) {
$table->dropColumn('last_weight_kg');
$table->dropColumn('last_height_cm');
});
}
};