This is the first part of the API. It supports most of contacts, as well as the journal. This PR also brings Monica to Laravel 5.5, as the API required it with the new concept of resources. It needs to run `php artisan migrate` as well as `php artisan passport:keys` to generate the access tokens needed for the API.
169 lines
4.6 KiB
PHP
169 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Note;
|
|
use Validator;
|
|
use App\Contact;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Database\QueryException;
|
|
use App\Http\Resources\Note\Note as NoteResource;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
class ApiNoteController extends ApiController
|
|
{
|
|
/**
|
|
* Get the list of notes.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$notes = auth()->user()->account->notes()
|
|
->paginate($this->getLimitPerPage());
|
|
|
|
return NoteResource::collection($notes);
|
|
}
|
|
|
|
/**
|
|
* Get the detail of a given note.
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(Request $request, $id)
|
|
{
|
|
try {
|
|
$note = Note::where('account_id', auth()->user()->account_id)
|
|
->where('id', $id)
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
return new NoteResource($note);
|
|
}
|
|
|
|
/**
|
|
* Store the note.
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
// Validates basic fields to create the entry
|
|
$validator = Validator::make($request->all(), [
|
|
'body' => 'required|max:100000',
|
|
'contact_id' => 'required|integer',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return $this->setErrorCode(32)
|
|
->respondWithError($validator->errors()->all());
|
|
}
|
|
|
|
try {
|
|
$contact = Contact::where('account_id', auth()->user()->account_id)
|
|
->where('id', $request->input('contact_id'))
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
try {
|
|
$note = Note::create($request->all());
|
|
} catch (QueryException $e) {
|
|
return $this->respondNotTheRightParameters();
|
|
}
|
|
|
|
$note->account_id = auth()->user()->account->id;
|
|
$note->save();
|
|
|
|
return new NoteResource($note);
|
|
}
|
|
|
|
/**
|
|
* Update the note.
|
|
* @param Request $request
|
|
* @param int $noteId
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $noteId)
|
|
{
|
|
try {
|
|
$note = Note::where('account_id', auth()->user()->account_id)
|
|
->where('id', $noteId)
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
// Validates basic fields to create the entry
|
|
$validator = Validator::make($request->all(), [
|
|
'body' => 'required|max:100000',
|
|
'contact_id' => 'required|integer',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return $this->setErrorCode(32)
|
|
->respondWithError($validator->errors()->all());
|
|
}
|
|
|
|
try {
|
|
$contact = Contact::where('account_id', auth()->user()->account_id)
|
|
->where('id', $request->input('contact_id'))
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
try {
|
|
$note->update($request->all());
|
|
} catch (QueryException $e) {
|
|
return $this->respondNotTheRightParameters();
|
|
}
|
|
|
|
return new NoteResource($note);
|
|
}
|
|
|
|
/**
|
|
* Delete a note.
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Request $request, $noteId)
|
|
{
|
|
try {
|
|
$note = Note::where('account_id', auth()->user()->account_id)
|
|
->where('id', $noteId)
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
$note->delete();
|
|
|
|
return $this->respondObjectDeleted($note->id);
|
|
}
|
|
|
|
/**
|
|
* Get the list of notes for the given contact.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function notes(Request $request, $contactId)
|
|
{
|
|
try {
|
|
$contact = Contact::where('account_id', auth()->user()->account_id)
|
|
->where('id', $contactId)
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
$notes = $contact->notes()
|
|
->paginate($this->getLimitPerPage());
|
|
|
|
return NoteResource::collection($notes);
|
|
}
|
|
}
|