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.
192 lines
5.3 KiB
PHP
192 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Debt;
|
|
use Validator;
|
|
use App\Contact;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Database\QueryException;
|
|
use App\Http\Resources\Debt\Debt as DebtResource;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
class ApiDebtController extends ApiController
|
|
{
|
|
/**
|
|
* Get the list of debts.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$debts = auth()->user()->account->debts()
|
|
->paginate($this->getLimitPerPage());
|
|
|
|
return DebtResource::collection($debts);
|
|
}
|
|
|
|
/**
|
|
* Get the detail of a given debt.
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(Request $request, $debtId)
|
|
{
|
|
try {
|
|
$debt = Debt::where('account_id', auth()->user()->account_id)
|
|
->where('id', $debtId)
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
return new DebtResource($debt);
|
|
}
|
|
|
|
/**
|
|
* Store the debt.
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
// Validates basic fields to create the entry
|
|
$validator = Validator::make($request->all(), [
|
|
'in_debt' => [
|
|
'required',
|
|
'string',
|
|
Rule::in(['yes', 'no']),
|
|
],
|
|
'status' => [
|
|
'required',
|
|
'string',
|
|
Rule::in(['inprogress', 'completed']),
|
|
],
|
|
'amount' => 'required|integer',
|
|
'reason' => 'string|max:1000000|nullable',
|
|
'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 {
|
|
$debt = Debt::create($request->all());
|
|
} catch (QueryException $e) {
|
|
return $this->respondNotTheRightParameters();
|
|
}
|
|
|
|
$debt->account_id = auth()->user()->account->id;
|
|
$debt->save();
|
|
|
|
return new DebtResource($debt);
|
|
}
|
|
|
|
/**
|
|
* Update the debt.
|
|
* @param Request $request
|
|
* @param int $debtId
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, $debtId)
|
|
{
|
|
try {
|
|
$debt = Debt::where('account_id', auth()->user()->account_id)
|
|
->where('id', $debtId)
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
// Validates basic fields to create the entry
|
|
$validator = Validator::make($request->all(), [
|
|
'in_debt' => [
|
|
'required',
|
|
'string',
|
|
Rule::in(['yes', 'no']),
|
|
],
|
|
'status' => [
|
|
'required',
|
|
'string',
|
|
Rule::in(['inprogress', 'completed']),
|
|
],
|
|
'amount' => 'required|integer',
|
|
'reason' => 'string|max:1000000|nullable',
|
|
'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 {
|
|
$debt->update($request->all());
|
|
} catch (QueryException $e) {
|
|
return $this->respondNotTheRightParameters();
|
|
}
|
|
|
|
return new DebtResource($debt);
|
|
}
|
|
|
|
/**
|
|
* Delete a debt.
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(Request $request, $debtId)
|
|
{
|
|
try {
|
|
$debt = Debt::where('account_id', auth()->user()->account_id)
|
|
->where('id', $debtId)
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
$debt->delete();
|
|
|
|
return $this->respondObjectDeleted($debt->id);
|
|
}
|
|
|
|
/**
|
|
* Get the list of debts for the given contact.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function debts(Request $request, $contactId)
|
|
{
|
|
try {
|
|
$contact = Contact::where('account_id', auth()->user()->account_id)
|
|
->where('id', $contactId)
|
|
->firstOrFail();
|
|
} catch (ModelNotFoundException $e) {
|
|
return $this->respondNotFound();
|
|
}
|
|
|
|
$debts = $contact->debts()
|
|
->paginate($this->getLimitPerPage());
|
|
|
|
return DebtResource::collection($debts);
|
|
}
|
|
}
|