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

125 行
4.5 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;
use Illuminate\Support\Facades\DB;
2021-10-15 10:37:47 +09:00
use Illuminate\Support\Facades\Log;
2021-11-17 02:45:26 +09:00
use App\Models\Child;
use App\Models\FatherRelation;
use App\Models\Meeting;
use App\Models\MeetingApprovals;
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 {
2021-11-17 19:46:40 +09:00
if (null === ($fr = FatherRelation::where('child_id', $child->id)->where('father_id', (int)$r->father_id)->first())) {
2021-11-17 13:09:21 +09:00
FatherRelation::create($create);
if (null !== ($meet = Meeting::select('id', 'is_favorite')->where('father_id', (int)$r->father_id)->get())) {
foreach ($meet as $m) {
if ($m->is_favorite) {
$addapprove = [
'meeting_id' => $m->id,
'child_id' => $child->id,
'approval_at' => null,
];
MeetingApprovals::create($addapprove);
}
}
}
2021-11-17 13:09:21 +09:00
}
else {
2021-11-17 14:01:08 +09:00
return ['status_code' => 400, 'error_messages' => ['すでに追加されました']];
2021-11-17 13:09:21 +09:00
}
2021-11-04 13:28:58 +09:00
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
return ['status_code' => 400, 'error_messages' => ['子の追加に失敗しました。']];
}
2021-11-17 13:09:21 +09:00
return ['status_code' => 200, 'success_messages' => ['子の追加に成功しました。'], 'params' => ['child_id' => $child->id]];
2021-11-04 13:28:58 +09:00
}
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 {
2021-11-17 13:51:32 +09:00
FatherRelation::where('father_id', (int)$r->father_id)->where('child_id', (int)$child_id)->update($update);
2021-10-06 14:50:13 +09:00
} 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 {
DB::beginTransaction();
$rel = FatherRelation::where('father_id', session()->get('fathers')['id'])->where('child_id', (int)$child_id);
foreach (Meeting::where('father_id', session()->get('fathers')['id'])->get() as $m) {
$apr = MeetingApprovals::where('child_id', (int)$child_id)->where('meeting_id', $m->id);
$apr->delete();
}
$rel->delete();
DB::commit();
2021-11-04 13:28:58 +09:00
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
DB::rollback();
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' => ['子の削除に成功しました。']];
}
}