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

219 行
7.7 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Child;
use App\Models\FatherRelation;
use App\Models\MeetingApprovals;
class ChildrenController extends Controller {
public function login () {}
public function registerTemporary () {}
public function registerMain () {}
public function requestPassword () {}
public function list () {
// 親一覧の取得に成功
if ($result = Child::orderBy('created_at', 'desc')->get()->toArray()) {
return ['status_code' => 200, 'params' => $result];
}
// 親一覧の取得に失敗
return ['status_code' => 400];
}
public function listOfFather (Request $r) {
$result = [];
$child_select = ['id', 'image', 'last_name', 'first_name'];
if ($list = FatherRelation::where('father_id', $r->father_id)->orderBy('created_at', 'desc')->get()->toArray()) {
foreach ($list as $l) {
$result[] = Child::select($child_select)->find($l['father_id']);
}
return ['status_code' => 200, 'params' => $result];
}
return ['status_code' => 400];
}
public function listOfMeeting (Request $r) {
$result = [];
$child_select = ['id', 'image', 'last_name', 'first_name'];
if ($list = MeetingApprovals::where('meeting_id', $r->meeting_id)->orderBy('created_at', 'desc')->get()->toArray()) {
foreach ($list as $l) {
$result[] = Child::select($child_select)->find($l['id']);
}
return ['status_code' => 200, 'params' => $result];
}
return ['status_code' => 400];
}
public function listOfMeetingNotifyUnapprovel (Request $r) {
if (!isset($r->meeting_id)) {
return ['status_code' => 400, 'error_messages' => ['画像の更新に失敗しました。']];
}
$result = [];
$child_select = ['id', 'image', 'last_name', 'first_name', 'tel'];
$meeting_approvals_select = ['approval_at'];
if ($list = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', $r->meeting_id)->whereNull('approval_at')->get()->toArray()) {
foreach ($list as $i => $l) {
$result[] = Child::select($child_select)->where('id', $l['child_id'])->get();
$result[$i]['meeting_approval'] = $l['approval_at'];
}
return ['status_code' => 200, 'params' => $result];
}
return ['status_code' => 400];
}
public function listOfMeetingNotifyApprovel (Request $r) {
if (!isset($r->meeting_id)) {
return ['status_code' => 400, 'error_messages' => ['画像の更新に失敗しました。']];
}
$result = [];
$child_select = ['id', 'image', 'last_name', 'first_name', 'tel'];
$meeting_approvals_select = ['approval_at'];
if ($list = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', $r->meeting_id)->whereNotNull('approval_at')->get()->toArray()) {
foreach ($list as $i => $l) {
$result[] = Child::select($child_select)->where('id', $l['child_id'])->get();
$result[$i]['meeting_approval'] = $l['approval_at'];
}
return ['status_code' => 200, 'params' => $result];
}
return ['status_code' => 400];
}
public function detail (Request $r, $child_id) {
$result = [];
$child_select = ['email', 'tel', 'last_name', 'first_name', 'image', 'company'];
$father_relation_select = ['hire_at'];
// 親詳細の取得に成功
if ($list = Child::where('id', $child_id)->orderBy('created_at', 'desc')->get()->toArray()) {
foreach ($list as $i => $l) {
$result[] = Child::select($child_select)->find($l['id']);
if (isset($r->father_id)) {
$result[$i]['father_relation'] = FatherRelation::select($father_relation_select)->where('father_id', $r->father_id)->first();
}
}
return ['status_code' => 200, 'params' => $result];
}
// 親詳細の取得に失敗
return ['status_code' => 400];
}
public function updateImage (Request $r, $child_id) {
if (!isset($r->image) || !isset($child_id)) {
return ['status_code' => 400, 'error_messages' => ['プロフィールの更新にしました。']];
}
// バリデーションエラー
$validate = Validator::make($r->all(), ['image' => 'max:1024|mimes:jpg,png,gif']);
if ($validate->fails()) {
return ['status_code' => 422, 'error_messages' => $validate->errors()];
}
try {
Child::where('id', $child_id)->update($r->all());
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
return ['status_code' => 400, 'error_messages' => ['プロフィールの更新に失敗しました。']];
}
// 成功
return ['status_code' => 200, 'success_messages' => ['プロフィールの更新に成功しました。']];
}
public function updateProfile (Request $r, $child_id) {
if (!isset($child_id)) {
return ['status_code' => 400, 'error_messages' => ['画像の更新に失敗しました。']];
}
// バリデーションエラー
$validate = Validator::make($r->all(), [
'email' => 'required|unique:children|max:255|email|alpha_num',
'tel' => 'required|unique:children|max:11|numeric|starts_with:0',
'last_name' => 'required|max:100',
'first_name' => 'required|max:100',
'company' => 'max:100',
]);
if ($validate->fails()) {
return ['status_code' => 422, 'error_messages' => $validate->errors()];
}
try {
Child::where('id', $child_id)->update($r->all());
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
return ['status_code' => 400, 'error_messages' => ['画像の更新に失敗しました。']];
}
// 成功
return ['status_code' => 200, 'success_messages' => ['画像の更新に成功しました。']];
}
public function updatePassword (Request $r, $child_id) {
if (!isset($r->image) || !isset($child_id)) {
return ['status_code' => 400, 'error_messages' => ['画像の更新に失敗しました。']];
}
// バリデーションエラー
$validate = Validator::make($r->all(), [
'password' => 'required|min:8|max:72|confirmed',
]);
$validate->after(function ($validate) {
if (count($r->image) > 10) {
$validate->errors()->add('count', '10枚以上登録できません。');
}
});
if ($validate->fails()) {
return ['status_code' => 422, 'error_messages' => $validate->errors()];
}
try {
Child::where('id', $child_id)->update($r->all());
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
return ['status_code' => 400, 'error_messages' => ['パスワードの更新に失敗しました。']];
}
// 成功
return ['status_code' => 200, 'success_messages' => ['パスワードの更新に成功しました。']];
}
public function withdrawal ($child_id) {
// 削除成功
if (Child::where('id', $child_id)->delete()) {
return ['status_code' => 200];
}
// 削除失敗
return ['status_code' => 400];
}
}