Merge remote-tracking branch 'origin/master' into mhmfajar

This commit is contained in:
Muhammad Fajar
2022-11-03 05:12:56 +07:00
5 changed files with 85 additions and 12 deletions

View File

@@ -19,9 +19,15 @@ class PersonController extends Controller
*/ */
public function index() public function index()
{ {
$personModel = Person::query()->with('avatar')->where('owner_user_id', auth()->user()->id)->get(); $user = auth()->user();
$user->load(['person', 'person.families', 'person.avatar']);
return $personModel; $families = $user->person->families;
$currentUserPerson = $user->person;
unset($currentUserPerson->families);
$families->prepend($currentUserPerson);
return PersonResource::collection($families);
} }
/** /**
@@ -29,12 +35,37 @@ class PersonController extends Controller
* @param Request $request * @param Request $request
* @return Renderable * @return Renderable
*/ */
public function store() public function store(Request $request)
{ {
$personModel = Person::query()->create([ $request->validate([]);
'owner_user_id' => auth()->user()->id,
'created_by' => auth()->user()->id, $personData = $request->only([
'owner_user_id',
'nik',
'name_prefix',
'name',
'name_suffix',
'phone',
'email',
'gender',
'birth_date',
'birth_place',
'citizenship',
'current_employment',
'last_education',
'religion',
'blood_type',
'is_deceased',
'deceased_at',
'marital_status',
'main_address_id',
'domicile_address_id',
]); ]);
$personData = array_merge($personData, [
'owner_user_id' => auth()->user()->id,
]);
$personModel = Person::query()->create($personData);
return response()->json([ return response()->json([
'status' => 'success', 'status' => 'success',
@@ -71,7 +102,29 @@ class PersonController extends Controller
*/ */
public function update(PersonRequest $request, Person $person) public function update(PersonRequest $request, Person $person)
{ {
$person->update($request->validated()); $personData = $request->only([
'owner_user_id',
'nik',
'name_prefix',
'name',
'name_suffix',
'phone',
'email',
'gender',
'birth_date',
'birth_place',
'citizenship',
'current_employment',
'last_education',
'religion',
'blood_type',
'is_deceased',
'deceased_at',
'marital_status',
'main_address_id',
'domicile_address_id',
]);
$person->update($personData);
if ($request->hasFile('user_avatar')) { if ($request->hasFile('user_avatar')) {
$pathFileAvatar = File::storeFile('avatar', $person->id, $request->file('user_avatar')); $pathFileAvatar = File::storeFile('avatar', $person->id, $request->file('user_avatar'));

View File

@@ -16,10 +16,6 @@ class PersonResource extends JsonResource
public function toArray($request) public function toArray($request)
{ {
return [ return [
'avatar' => [
'title' => $this->avatar->name ?? '',
'url' => url($this->avatar->path) ?? '',
],
'id' => $this->id, 'id' => $this->id,
'name' => $this->name, 'name' => $this->name,
'birth_place' => $this->birth_place, 'birth_place' => $this->birth_place,
@@ -29,12 +25,13 @@ class PersonResource extends JsonResource
'blood_type' => $this->blood_type, 'blood_type' => $this->blood_type,
'weight' => $this->weight, 'weight' => $this->weight,
'height' => $this->height, 'height' => $this->height,
'relation_with_owner' => $this->relation_with_owner, 'relation_with_owner' => ($this->user && $this->user->person_id == $this->id) ? 'Self' : $this->pivot->relation_with_owner ?? null,
'marital_status' => $this->marital_status, 'marital_status' => $this->marital_status,
'last_education' => $this->last_education, 'last_education' => $this->last_education,
'current_employment' => $this->current_employment, 'current_employment' => $this->current_employment,
'nik' => $this->nik, 'nik' => $this->nik,
'religion' => $this->religion, 'religion' => $this->religion,
'avatar_url' => $this->avatar->url ?? asset('images/default-doctor-avatar.png'),
]; ];
} }
} }

View File

@@ -20,6 +20,10 @@ class File extends Model
'path', 'path',
]; ];
public $appends = [
'url'
];
public static $file_directories = [ public static $file_directories = [
'import-temp' => 'import-temp/', 'import-temp' => 'import-temp/',
'avatar' => 'user-avatar/', 'avatar' => 'user-avatar/',
@@ -41,6 +45,11 @@ class File extends Model
return $type . '-' . $id . '-' . Str::random(10); return $type . '-' . $id . '-' . Str::random(10);
} }
public function getUrlAttribute()
{
return url($this->path);
}
public static function storeFile($type, $id, $file) public static function storeFile($type, $id, $file)
{ {
$fileName = self::getFileName($type, $id); $fileName = self::getFileName($type, $id);

View File

@@ -88,8 +88,18 @@ class Person extends Model
return $this->morphOne(File::class, 'fileable')->where('type', 'avatar')->latestOfMany(); return $this->morphOne(File::class, 'fileable')->where('type', 'avatar')->latestOfMany();
} }
public function families()
{
return $this->belongsToMany(Person::class, 'family_relations', 'owner_id', 'person_id')->withPivot(['relation_with_owner']);
}
public function familyOwner() public function familyOwner()
{ {
return $this->hasOne(Family::class, 'person_id'); return $this->hasOne(Family::class, 'person_id');
} }
public function user()
{
return $this->hasOne(User::class, 'person_id');
}
} }

View File

@@ -47,6 +47,10 @@ class User extends Authenticatable
'otp_created_at' => 'datetime' 'otp_created_at' => 'datetime'
]; ];
public $with = [
'metas'
];
public $appends = [ public $appends = [
'meta' 'meta'
]; ];