user()->account_id) ->findOrFail($contactFieldId); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } return new ContactFieldResource($contactField); } /** * Store the contactField. * * @param Request $request * * @return ContactFieldResource|\Illuminate\Http\JsonResponse */ public function store(Request $request) { $isvalid = $this->validateUpdate($request); if ($isvalid !== true) { return $isvalid; } try { $contactField = ContactField::create( $request->all() + ['account_id' => auth()->user()->account_id] ); } catch (QueryException $e) { return $this->respondNotTheRightParameters(); } return new ContactFieldResource($contactField); } /** * Update the contactField. * * @param Request $request * @param int $contactFieldId * * @return ContactFieldResource|\Illuminate\Http\JsonResponse */ public function update(Request $request, $contactFieldId) { try { $contactField = ContactField::where('account_id', auth()->user()->account_id) ->findOrFail($contactFieldId); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } $isvalid = $this->validateUpdate($request); if ($isvalid !== true) { return $isvalid; } try { $contactField->update($request->all()); } catch (QueryException $e) { return $this->respondNotTheRightParameters(); } return new ContactFieldResource($contactField); } /** * Validate the request for update. * * @param Request $request * @return \Illuminate\Http\JsonResponse|true */ private function validateUpdate(Request $request) { // Validates basic fields to create the entry $validator = Validator::make($request->all(), [ 'data' => 'max:255|required', 'contact_field_type_id' => 'integer|required', 'contact_id' => 'required|integer', ]); if ($validator->fails()) { return $this->respondValidatorFailed($validator); } try { ContactFieldType::where('account_id', auth()->user()->account_id) ->findOrFail($request->input('contact_field_type_id')); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } try { Contact::where('account_id', auth()->user()->account_id) ->findOrFail($request->input('contact_id')); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } return true; } /** * Delete a contactField. * * @param Request $request * @param int $contactFieldId * * @return \Illuminate\Http\JsonResponse */ public function destroy(Request $request, $contactFieldId) { try { $contactField = ContactField::where('account_id', auth()->user()->account_id) ->where('id', $contactFieldId) ->firstOrFail(); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } $contactField->delete(); return $this->respondObjectDeleted($contactField->id); } /** * Get the list of contact fields for the given contact. * * @param Request $request * @param int $contactId * * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection|\Illuminate\Http\JsonResponse */ public function contactFields(Request $request, $contactId) { try { $contact = Contact::where('account_id', auth()->user()->account_id) ->where('id', $contactId) ->firstOrFail(); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } $contactFields = $contact->contactFields() ->paginate($this->getLimitPerPage()); return ContactFieldResource::collection($contactFields); } }