このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
kikikan/backend/app/Http/Controllers/Api/FatherRelationsController.php

94 行
3.4 KiB
PHP
Raw 通常表示 履歴

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
2021-11-17 02:37:02 +09:00
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
2021-10-15 10:37:47 +09:00
use Illuminate\Support\Facades\Log;
use App\Models\FatherRelation;
class FatherRelationsController extends Controller {
2021-11-17 01:50:51 +09:00
public function register (Request $r) {
2021-11-17 02:18:36 +09:00
if (!isset($r->father_id)) {
2021-11-04 13:28:58 +09:00
return ['status_code' => 400, 'error_messages' => ['子の追加に失敗しました。']];
}
2021-11-17 01:57:46 +09:00
$validate = Validator::make($r->all(), [
'identity' => 'required|max:20|alpha_num',
]);
if ($validate->fails()) {
return ['status_code' => 422, 'error_messages' => $validate->errors()];
}
2021-11-04 13:28:58 +09:00
if (null === ($child = Child::select('id')->where('identity', $r->identity)->first())) {
return ['status_code' => 400, 'error_messages' => ['子の追加に失敗しました。']];
}
$create = [
'father_id' => $r->father_id,
'child_id' => $child->id,
'hire_at' => date('Y-m-d H:i:s', time()),
];
try {
FatherRelation::create($create);
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
return ['status_code' => 400, 'error_messages' => ['子の追加に失敗しました。']];
}
return ['status_code' => 200, 'success_messages' => ['子の追加に成功しました。'], 'params' => ['child_id' => $child_id]];
// 1.POSTで受け取ったidentityと紐づくchildrenのデータのidを取得。
// 2.1で取得したidとPOSTで受け取ったfather_idをfather_relatoinsに登録。(hire_atはPOST時の日時)
}
2021-10-01 14:18:52 +09:00
public function updateHireDate (Request $r, $child_id) {
2021-11-17 02:29:26 +09:00
if (!isset($child_id) || !isset($r->father_id)) {
2021-11-17 01:20:01 +09:00
return ['status_code' => 400, 'error_messages' => ['子の入社日の更新に失敗しました。']];
2021-10-01 14:18:52 +09:00
}
2021-11-17 01:57:46 +09:00
$validate = Validator::make($r->all(), [
'hire_at' => 'date',
]);
if ($validate->fails()) {
return ['status_code' => 422, 'error_messages' => $validate->errors()];
}
2021-11-04 13:28:58 +09:00
$update = [
'hire_at' => date('Y-m-d H:i:s', strtotime($r->hire_at)),
];
2021-10-06 14:50:13 +09:00
try {
FatherRelation::where('father_id', $r->father_id)->where('child_id', $child_id)->update($update);
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
2021-11-17 01:20:01 +09:00
return ['status_code' => 400, 'error_messages' => ['子の入社日の更新に失敗しました。']];
2021-10-01 14:18:52 +09:00
}
2021-10-06 14:50:13 +09:00
return ['status_code' => 200, 'success_messages' => ['子の入社日の更新に成功しました。']];
}
2021-11-04 13:28:58 +09:00
public function deleteRelationChild ($child_id) {
if (!isset($child_id)) {
2021-11-17 01:20:01 +09:00
return ['status_code' => 400, 'error_messages' => ['子の削除に失敗しました。']];
2021-11-04 13:28:58 +09:00
}
try {
2021-11-16 22:34:46 +09:00
FatherRelation::where('father_id', session()->get('fathers')['id'])->where('child_id', $child_id)->delete();
2021-11-04 13:28:58 +09:00
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
2021-11-17 01:20:01 +09:00
return ['status_code' => 400, 'error_messages' => ['子の削除に失敗しました。']];
2021-11-04 13:28:58 +09:00
}
return ['status_code' => 200, 'success_messages' => ['子の削除に成功しました。']];
}
}