Files
monica/app/Http/Controllers/Api/ApiCallController.php
T
Régis Freyd 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

171 lines
4.7 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Call;
use Validator;
use App\Contact;
use Illuminate\Http\Request;
use Illuminate\Database\QueryException;
use App\Http\Resources\Call\Call as CallResource;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class ApiCallController extends ApiController
{
/**
* Get the list of calls.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$calls = auth()->user()->account->calls()
->paginate($this->getLimitPerPage());
return CallResource::collection($calls);
}
/**
* Get the detail of a given call.
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function show(Request $request, $callId)
{
try {
$call = Call::where('account_id', auth()->user()->account_id)
->where('id', $callId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
return new CallResource($call);
}
/**
* 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(), [
'content' => 'required|max:100000',
'called_at' => 'required|date',
'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 {
$call = Call::create($request->all());
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
}
$call->account_id = auth()->user()->account->id;
$call->save();
return new CallResource($call);
}
/**
* Update the call.
* @param Request $request
* @param int $callId
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $callId)
{
try {
$call = Call::where('account_id', auth()->user()->account_id)
->where('id', $callId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
// Validates basic fields to create the entry
$validator = Validator::make($request->all(), [
'content' => 'required|max:100000',
'called_at' => 'required|date',
'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 {
$call->update($request->all());
} catch (QueryException $e) {
return $this->respondNotTheRightParameters();
}
return new CallResource($call);
}
/**
* Delete a note.
* @param Request $request
* @return \Illuminate\Http\Response
*/
public function destroy(Request $request, $callId)
{
try {
$call = Call::where('account_id', auth()->user()->account_id)
->where('id', $callId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
$call->delete();
return $this->respondObjectDeleted($call->id);
}
/**
* Get the list of calls for the given contact.
*
* @return \Illuminate\Http\Response
*/
public function calls(Request $request, $contactId)
{
try {
$contact = Contact::where('account_id', auth()->user()->account_id)
->where('id', $contactId)
->firstOrFail();
} catch (ModelNotFoundException $e) {
return $this->respondNotFound();
}
$calls = $contact->calls()
->paginate($this->getLimitPerPage());
return CallResource::collection($calls);
}
}