Files
Global-Jain/app/Http/Requests/Api/Vihar/UpdateViharRequest.php
2025-11-05 10:37:10 +05:30

93 lines
2.9 KiB
PHP

<?php
namespace App\Http\Requests\Api\Vihar;
use App\Rules\ViharCanNotAfterTwoHours;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use App\Rules\ViharDoesNotStoreForSameDateTime;
use App\Rules\ViharStorePerDay;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Log;
class UpdateViharRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function getValidatorInstance()
{
$this->request->set('per_day', 2);
$this->request->set('date_time', 1);
$this->request->set('next_vihar', 1);
return parent::getValidatorInstance();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(Request $request)
{
$array = [
'sant_id' => 'exists:sants,id',
'from' => 'required',
'to' => 'required',
'start_date' => 'required|date_format:Y-m-d',
'start_time' => 'required|date_format:H:i',
// 'end_date' => 'required|date_format:Y-m-d|after_or_equal:start_date',
// 'end_time' => 'date_format:H:i|after:start_time',
// 'end_date' => 'required|date_format:Y-m-d|after_or_equal:start_date',
'date_time' => [new ViharDoesNotStoreForSameDateTime($request->all(), $this->vihar->id)],
'per_day' => [new ViharStorePerDay($request->all(), $this->vihar->id)],
'next_vihar' => [new ViharCanNotAfterTwoHours($request->all(), $this->vihar->id)],
];
$startDate = Carbon::createFromFormat('Y-m-d H:i:s', $request->start_date .' '. '00:00:00');
$todayDate = Carbon::createFromFormat('Y-m-d H:i:s', Carbon::now()->format('Y-m-d') .' '. '00:00:00');
$result = $todayDate->eq($startDate);
if ($result) {
$array['start_time'] = 'date_format:H:i|after:'.Carbon::now()->format('H:i');
}
return $array;
}
protected function failedValidation(Validator $validator)
{
if (Request::wantsJson()) {
throw new HttpResponseException(response()->json(['message' => $validator->errors()->first(), 'error' => true, 'status' => 422], 422));
} else {
throw new ValidationException($validator);
}
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function messages()
{
return [
'start_time.after' => 'Please enter a time after the current time.',
];
}
}