-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
unknown
committed
May 25, 2021
1 parent
92676db
commit 3f0865c
Showing
9 changed files
with
408 additions
and
12 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
app/Http/Controllers/Api/CRM/Contact/ContactsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\CRM\Contact; | ||
|
||
use App\Models\Contact; | ||
use App\Traits\Api\ApiResponser; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\CRM\Contact\StoreRequest; | ||
use App\Http\Requests\CRM\Contact\DeleteRequest; | ||
use App\Http\Requests\CRM\Contact\UpdateRequest; | ||
|
||
class ContactsController extends Controller | ||
{ | ||
use ApiResponser; | ||
|
||
private Contact $contact; | ||
|
||
public function __construct(Contact $contact) | ||
{ | ||
$this->contact = $contact; | ||
$this->middleware(['auth:api', 'permission:Manage Contacts']); | ||
} | ||
|
||
/** | ||
* Display a listing of the resource. | ||
* | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function index() | ||
{ | ||
$result = $this->contact->all(); | ||
|
||
return !$result->count() | ||
? $this->noContent() | ||
: $this->success($result); | ||
} | ||
|
||
/** | ||
* Store a newly created resource in storage. | ||
* | ||
* @param StoreRequest $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function store(StoreRequest $request) | ||
{ | ||
$contact = $this->contact->create($request->validated()); | ||
|
||
return $this->success($contact, 'Contact created successfully.'); | ||
} | ||
|
||
/** | ||
* Display the specified resource. | ||
* | ||
* @param integer $id | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function show(Contact $contact) | ||
{ | ||
return !$contact | ||
? $this->noContent() | ||
: $this->success($contact); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
* | ||
* @param UpdateRequest $request | ||
* @param Contact $contact | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function update(UpdateRequest $request, Contact $contact) | ||
{ | ||
$contact->update($request->validated()); | ||
|
||
return $this->success(null, 'Contact updated successfully.'); | ||
} | ||
|
||
/** | ||
* Remove the specified resource from storage. | ||
* | ||
* @param DeleteRequest $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function destroy(DeleteRequest $request) | ||
{ | ||
$this->contact->whereIn('id', $request->ids)->delete(); | ||
|
||
return $this->success(null, 'Contact or contacts deleted successfully.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\CRM\Contact; | ||
|
||
use App\Http\Requests\BaseRequest; | ||
|
||
class DeleteRequest extends BaseRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'ids.*' => ['required', 'integer', 'distinct', 'exists:contacts,id'] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\CRM\Contact; | ||
|
||
use App\Http\Requests\BaseRequest; | ||
|
||
class StoreRequest extends BaseRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'name' => ['required', 'string'], | ||
'owner' => ['required', 'string'], | ||
'email' => ['required', 'email', 'unique:contacts,email'], | ||
'phone' => ['required', 'string', 'unique:contacts,phone'], | ||
'stage' => ['required', 'string'], | ||
'mobile' => ['required', 'string', 'unique:contacts,mobile'], | ||
'website' => ['required', 'string'], | ||
'fax_number' => ['required', 'string', 'unique:contacts,fax_number'], | ||
'source' => ['required', 'string'], | ||
'address' => ['required', 'string'], | ||
'born_at' => ['required', 'string'] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\CRM\Contact; | ||
|
||
use App\Http\Requests\BaseRequest; | ||
|
||
class UpdateRequest extends BaseRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'id' => ['required', 'integer', 'exists:contacts,id'], | ||
'name' => ['required', 'string'], | ||
'owner' => ['required', 'string'], | ||
'email' => ['required', 'email', 'unique:contacts,email,' . $this->id], | ||
'phone' => ['required', 'string', 'unique:contacts,phone,' . $this->id], | ||
'stage' => ['required', 'string'], | ||
'mobile' => ['required', 'string', 'unique:contacts,mobile,' . $this->id], | ||
'website' => ['required', 'string'], | ||
'fax_number' => ['required', 'string', 'unique:contacts,fax_number,' . $this->id], | ||
'source' => ['required', 'string'], | ||
'address' => ['required', 'string'], | ||
'born_at' => ['required', 'string'] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Contact extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
'name', | ||
'owner', | ||
'email', | ||
'phone', | ||
'stage', | ||
'mobile', | ||
'website', | ||
'fax_number', | ||
'source', | ||
'address', | ||
'born_at' | ||
]; | ||
|
||
protected $hidden = [ | ||
'updated_at' | ||
]; | ||
|
||
/** | ||
* getCreatedAtAttribute | ||
* | ||
* @param mixed $value | ||
* @return string | ||
*/ | ||
public function getCreatedAtAttribute($value): string | ||
{ | ||
return Carbon::parse($value)->format('Y-m-d'); | ||
} | ||
|
||
/** | ||
* getBornAtAttribute | ||
* | ||
* @param mixed $value | ||
* @return string | ||
*/ | ||
public function getBornAtAttribute($value): string | ||
{ | ||
return Carbon::parse($value)->format('Y-m-d'); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
database/migrations/2021_05_25_181101_create_contacts_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateContactsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('contacts', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name'); | ||
$table->string('owner'); | ||
$table->string('email'); | ||
$table->string('phone'); | ||
$table->string('stage'); | ||
$table->string('mobile'); | ||
$table->string('website'); | ||
$table->string('fax_number'); | ||
$table->string('source'); | ||
$table->string('address'); | ||
$table->timestamp('born_at')->default(now()); | ||
$table->timestamps(); | ||
|
||
$table->unique([ | ||
'email', | ||
'phone', | ||
'fax_number', | ||
]); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('contacts'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.