Files
monica/app/Http/Controllers/Api/ApiJournalController.php
T
Régis FreydandGitHub 7e1cd96bf5 Add API (#606)
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.
2017-10-30 08:38:15 -04:00

131 lines
3.5 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Entry;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Database\QueryException;
use App\Http\Resources\Journal\Entry as JournalResource;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class ApiJournalController extends ApiController
{
/**
* Get the list of journal entries.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$entries = auth()->user()->account->entries()
->paginate($this->getLimitPerPage());
return JournalResource::collection($entries);
}
/**
* Get the detail of a given journal entry.
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function show(Request $request, $entryId)
{
try {
$entry = Entry::where('account_id', auth()->user()->account_id)
->where('id', $entryId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
return new JournalResource($entry);
}
/**
* Store the call.
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'title' => 'required|max:255',
'post' => 'required|max:1000000',
]);
if ($validator->fails()) {
return $this->setErrorCode(32)
->respondWithError($validator->errors()->all());
}
try {
$entry = Entry::create($request->all());
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
}
$entry->account_id = auth()->user()->account->id;
$entry->save();
return new JournalResource($entry);
}
/**
* Update the note.
* @param Request $request
* @param int $entryId
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $entryId)
{
try {
$entry = Entry::where('account_id', auth()->user()->account_id)
->where('id', $entryId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'title' => 'required|max:255',
'post' => 'required|max:1000000',
]);
if ($validator->fails()) {
return $this->setErrorCode(32)
->respondWithError($validator->errors()->all());
}
try {
$entry->update($request->all());
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
}
return new JournalResource($entry);
}
/**
* Delete a journal entry.
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $entryId)
{
try {
$entry = Entry::where('account_id', auth()->user()->account_id)
->where('id', $entryId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
$entry->delete();
return $this->respondObjectDeleted($entry->id);
}
}