app/Http/Controllers/ApplicantController.php
/**
     * Store a new applicant instance.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a new applicant instance.
Store a new applicant instance .
public function store(Request $request)
    {
        $data = $request->only(['name', 'email', 'isHired']);
        $validator = Validator::make($data, [
            'name'  => 'required|max:45',
            'email'  => 'required|max:65',
            'isHired' => 'required|boolean'
        ]);
        if($validator->fails()) throw new ValidationException($validator);
        $applicant = Applicant::where('email', $data['email'])->first();
        if($applicant) {
            if($applicant->isHired) return abort(400, __('apiMsg.already_hired'));
            if($applicant->created_at > Carbon::now()->subMonths(6)) {
                abort(400, __('apiMsg.reapply'));
            } else {
                $data['created_at'] = now();
                $applicant->update($data);
                return response()->json([
                    'success' => true,
                    'message' => __('apiMsg.applicant_updated'),
                    'data'    => new ApplicantResource($applicant)
                ]);
            }
        }
        $applicant = Applicant::create($data);
       return response()->json([
        'success' => true,
        'message' => __('apiMsg.applicant_created'),
        'data'    => new ApplicantResource($applicant)
    ]);
    }
public function store ( Request $ request ) { $ data = $ request -> only ( [ 'name' , 'email' , 'isHired' ] ) ; $ validator = Validator :: make ( $ data , [ 'name' => 'required|max:45' , 'email' => 'required|max:65' , 'isHired' => 'required|boolean' ] ) ; if ( $ validator -> fails ( ) ) throw new ValidationException ( $ validator ) ; $ applicant = Applicant :: where ( 'email' , $ data [ 'email' ] ) -> first ( ) ; if ( $ applicant ) { if ( $ applicant -> isHired ) return abort ( 400 , __ ( 'apiMsg.already_hired' ) ) ; if ( $ applicant -> created_at > Carbon :: now ( ) -> subMonths ( 6 ) ) { abort ( 400 , __ ( 'apiMsg.reapply' ) ) ; } else { $ data [ 'created_at' ] = now ( ) ; $ applicant -> update ( $ data ) ; return response ( ) -> json ( [ 'success' => true , 'message' => __ ( 'apiMsg.applicant_updated' ) , 'data' => new ApplicantResource ( $ applicant ) ] ) ; } } $ applicant = Applicant :: create ( $ data ) ; return response ( ) -> json ( [ 'success' => true , 'message' => __ ( 'apiMsg.applicant_created' ) , 'data' => new ApplicantResource ( $ applicant ) ] ) ; }
Store a new applicant instance.
Store a new applicant instance .
//get $request data //validate data //throw exception if validation fails //check for the applicant if exists //prevent already hired applicants //if last application date is still within 6 months //update application status //return applicant //create a new applicant //return applicant