127 lines
3.2 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Http\Controllers\Api\Common;
use App\Abstracts\Http\ApiController;
use App\Http\Requests\Common\Contact as Request;
2022-06-01 10:15:55 +03:00
use App\Http\Resources\Common\Contact as Resource;
2019-11-16 10:21:14 +03:00
use App\Jobs\Common\CreateContact;
use App\Jobs\Common\DeleteContact;
use App\Jobs\Common\UpdateContact;
use App\Models\Common\Contact;
use App\Traits\Uploads;
class Contacts extends ApiController
{
use Uploads;
/**
* Display a listing of the resource.
*
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function index()
{
$contacts = Contact::collect();
2022-06-01 10:15:55 +03:00
return Resource::collection($contacts);
2019-11-16 10:21:14 +03:00
}
/**
* Display the specified resource.
*
* @param int|string $id
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function show($id)
{
// Check if we're querying by id or email
if (is_numeric($id)) {
$contact = Contact::find($id);
} else {
$contact = Contact::where('email', $id)->first();
}
if (! $contact instanceof Contact) {
//return $this->noContent();
return $this->errorInternal('No query results for model [' . Contact::class . '] ' . $id);
}
2022-06-01 10:15:55 +03:00
return new Resource($contact);
2019-11-16 10:21:14 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function store(Request $request)
{
$contact = $this->dispatch(new CreateContact($request));
2022-06-01 10:15:55 +03:00
return $this->created(route('api.contacts.show', $contact->id), new Resource($contact));
2019-11-16 10:21:14 +03:00
}
/**
* Update the specified resource in storage.
*
* @param $contact
* @param $request
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function update(Contact $contact, Request $request)
{
$contact = $this->dispatch(new UpdateContact($contact, $request));
2022-06-01 10:15:55 +03:00
return new Resource($contact->fresh());
2019-11-16 10:21:14 +03:00
}
/**
* Enable the specified resource in storage.
*
* @param Contact $contact
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function enable(Contact $contact)
{
$contact = $this->dispatch(new UpdateContact($contact, request()->merge(['enabled' => 1])));
2022-06-01 10:15:55 +03:00
return new Resource($contact->fresh());
2019-11-16 10:21:14 +03:00
}
/**
* Disable the specified resource in storage.
*
* @param Contact $contact
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\JsonResponse
2019-11-16 10:21:14 +03:00
*/
public function disable(Contact $contact)
{
try {
$contact = $this->dispatch(new UpdateContact($contact, request()->merge(['enabled' => 0])));
2022-06-01 10:15:55 +03:00
return new Resource($contact->fresh());
2019-11-16 10:21:14 +03:00
} catch(\Exception $e) {
2022-06-01 10:15:55 +03:00
$this->errorUnauthorized($e->getMessage());
2019-11-16 10:21:14 +03:00
}
}
/**
* Remove the specified resource from storage.
*
* @param Contact $contact
2022-06-01 10:15:55 +03:00
* @return \Illuminate\Http\Response
2019-11-16 10:21:14 +03:00
*/
public function destroy(Contact $contact)
{
try {
$this->dispatch(new DeleteContact($contact));
2022-06-01 10:15:55 +03:00
return $this->noContent();
2019-11-16 10:21:14 +03:00
} catch(\Exception $e) {
2022-06-01 10:15:55 +03:00
$this->errorUnauthorized($e->getMessage());
2019-11-16 10:21:14 +03:00
}
}
}