CreateOrUpdateRequest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Http\Requests\API\Project;
  3. use App\Models\Asset;
  4. use App\Models\Enums\AssetStatus;
  5. use App\Models\Plan;
  6. use App\Models\User;
  7. use Illuminate\Foundation\Http\FormRequest;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Validation\Rule;
  10. use Illuminate\Validation\Rules\Enum;
  11. class CreateOrUpdateRequest extends FormRequest
  12. {
  13. /**
  14. * Determine if the user is authorized to make this request.
  15. */
  16. public function authorize(): bool
  17. {
  18. return true;
  19. }
  20. /**
  21. * Get the validation rules that apply to the request.
  22. *
  23. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  24. */
  25. public function rules(): array
  26. {
  27. return [
  28. 'name' => 'required|max:150',
  29. 'code' => [
  30. 'required',
  31. 'max:50',
  32. ],
  33. 'const' => 'nullable|numeric',
  34. 'begin' => 'required|date',
  35. 'end' => 'required|date',
  36. 'type' => 'required|max:20',
  37. 'acl' => 'required|in:private,custom',
  38. 'available_days' => 'numeric',
  39. 'whitelist' => [
  40. 'array',
  41. function ($attribute, $value, $fail) {
  42. $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  43. if ($userCount != count($value)) {
  44. $fail('The selected user is invalid.');
  45. }
  46. }
  47. ],
  48. 'plans' => [
  49. 'array',
  50. function ($attribute, $value, $fail) {
  51. $count = Plan::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
  52. if ($count != count($value)) {
  53. $fail('The selected plan is invalid.');
  54. }
  55. }
  56. ],
  57. 'assets' => [
  58. 'array',
  59. function ($attribute, $value, $fail) {
  60. $assetIds = Asset::query()->whereIn('id', $value)->pluck('id')->toArray();
  61. if (count(array_diff($value, $assetIds)) > 0) {
  62. $fail('The selected asset is invalid.');
  63. }
  64. }
  65. // $count = Asset::query()
  66. // ->leftJoin("project_asset", "assets.id", "=", "project_asset.asset_id")
  67. // ->where("company_id", Auth::user()->company_id)
  68. // ->whereNull("project_asset.id")
  69. // ->whereIn('assets.id', $value)->count();
  70. //
  71. // if ($count != count($value)) {
  72. // $fail('The selected asset is invalid.');
  73. // }
  74. ],
  75. 'latitude' => 'numeric',
  76. 'longitude' => 'numeric',
  77. ];
  78. }
  79. }