user()->account->reminders() ->paginate($this->getLimitPerPage()); return ReminderResource::collection($reminders); } /** * Get the detail of a given reminder. * @param Request $request * @return \Illuminate\Http\Response */ public function show(Request $request, $reminderId) { try { $reminder = Reminder::where('account_id', auth()->user()->account_id) ->where('id', $reminderId) ->firstOrFail(); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } return new ReminderResource($reminder); } /** * Store the reminder. * @param Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $isvalid = $this->validateUpdate($request); if ($isvalid !== true) { return $isvalid; } try { $reminder = Reminder::create($request->all()); } catch (QueryException $e) { return $this->respondNotTheRightParameters(); } $reminder->account_id = auth()->user()->account->id; $reminder->save(); return new ReminderResource($reminder); } /** * Update the reminder. * @param Request $request * @param int $reminderId * @return \Illuminate\Http\Response */ public function update(Request $request, $reminderId) { try { $reminder = Reminder::where('account_id', auth()->user()->account_id) ->where('id', $reminderId) ->firstOrFail(); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } $isvalid = $this->validateUpdate($request); if ($isvalid !== true) { return $isvalid; } try { $reminder->update($request->all()); } catch (QueryException $e) { return $this->respondNotTheRightParameters(); } return new ReminderResource($reminder); } /** * Validate the request for update. * * @param Request $request * @return mixed */ private function validateUpdate(Request $request) { // Validates basic fields to create the entry $validator = Validator::make($request->all(), [ 'title' => 'required|max:100000', 'description' => 'required|max:1000000', 'next_expected_date' => 'required|date', 'frequency_type' => [ 'required', Rule::in(['one_time', 'day', 'month', 'year']), ], 'frequency_number' => 'integer', 'contact_id' => 'required|integer', ]); if ($validator->fails()) { return $this->setErrorCode(32) ->respondWithError($validator->errors()->all()); } $date = \Carbon\Carbon::createFromFormat('Y-m-d', $request->get('next_expected_date'), auth()->user()->timezone); if ($date->isPast()) { return $this->setErrorCode(38) ->respondWithError('Date should be in the future'); } try { Contact::where('account_id', auth()->user()->account_id) ->where('id', $request->input('contact_id')) ->firstOrFail(); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } return true; } /** * Delete a reminder. * @param Request $request * @return \Illuminate\Http\Response */ public function destroy(Request $request, $reminderId) { try { $reminder = Reminder::where('account_id', auth()->user()->account_id) ->where('id', $reminderId) ->firstOrFail(); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } $reminder->delete(); return $this->respondObjectDeleted($reminder->id); } /** * Get the list of calls for the given contact. * * @return \Illuminate\Http\Response */ public function reminders(Request $request, $contactId) { try { $contact = Contact::where('account_id', auth()->user()->account_id) ->where('id', $contactId) ->firstOrFail(); } catch (ModelNotFoundException $e) { return $this->respondNotFound(); } $reminders = $contact->reminders() ->paginate($this->getLimitPerPage()); return ReminderResource::collection($reminders); } }