Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Victor Tesoura Júnior edited this page May 9, 2021 · 13 revisions

Let's suppose that you want to create an user CRUD

Model example

interface User{
 id: number;
 name: string; 
 profile: Profile;
}
interface Profile{
 id: number;
 birthdate: date; 
}

As this library use a repository pattern, to create a CRUD we should have the following folder structure:

app
|--Http
| |--Controllers
| | |--UserController
| |
| |--Requests
| | |--UserStoreRequest
| | |--UserUpdateRequest
| |
| |--Resources
| | |--UserResource
| 
|--Models
| |--User
| |--Profile
|
|--Respositories
| |--UserRespository
| 
|--Services
 |--UserService

Controller example

<?php
namespace App\Http\Controllers;
use App\Http\Resources\UserResource;
use App\Repositories\UserRepository;
use App\Services\UserService;
use Txsoura\Core\Http\Controllers\Traits\CRUDMethodsController;
use Txsoura\Core\Http\Controllers\Traits\SoftDeleteMethodsController;
class Usercontroller extends Controller
{
 use CRUDMethodsController;
 use SoftDeleteMethodsController; //when softDelete
 /**
 * @var UserRepository
 */
 protected $repository;
 /**
 * @var UserResource
 */
 protected $resource = UserResource::class;
 /**
 * @var UserService
 */
 protected $service;
 /**
 * UserController constructor.
 */
 public function __construct(UserService $service, UserRepository $repository)
 {
 $this->service = $service;
 $this->repository = $repository;
 }
}

Request example

<?php
namespace App\Http\Requests;
use Illuminate\Support\Str;
use Txsoura\Core\Http\Requests\CoreRequest;
class UserStoreRequest extends CoreRequest
{
 /**
 * Prepare the data for validation.
 *
 * @return void
 */
 protected function prepareForValidation()
 {
 return $this->replace([
 'name' => ucwords($this->name)
 ]);
 }
 /**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
 public function rules()
 {
 return [
 'name' => 'required|string'
 ];
 }
}

Resource example

<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
 /**
 * Transform the resource into an array.
 *
 * @param \Illuminate\Http\Request $request
 * @return array
 */
 public function toArray($request)
 {
 return [
 'id' => $this->id,
 'name' => $this->name,
 'created_at' => $this->created_at
 ];
 }
}

Service example

<?php
namespace App\Services;
use App\Http\Requests\UserStoreRequest;
use App\Http\Requests\UserUpdateRequest;
use App\Models\User;
use App\Repositories\UserRepository;
use Txsoura\Core\Services\CoreService;
use Txsoura\Core\Services\Traits\CRUDMethodsService;
use Txsoura\Core\Services\Traits\SoftDeleteMethodsService;
class UserService extends CoreService
{
 use CRUDMethodsService, SoftDeleteMethodsService;
 protected $storeRequest = UserStoreRequest::class;
 protected $updateRequest = UserUpdateRequest::class;
 /**
 * @var UserRepository
 */
 protected $repository;
 /**
 * UserService constructor.
 * @param UserRepository $repository
 */
 public function __construct(UserRepository $repository)
 {
 $this->repository = $repository;
 }
 /**
 * Model class for crud.
 *
 * @return string
 */
 protected function model(): string
 {
 return User::class;
 }
}

Repository example

<?php
namespace App\Repositories;
use App\Models\User;
use Txsoura\Core\Repositories\CoreRepository;
class UserRepository extends CoreRepository
{
 protected function model(): string
 {
 return User::class;
 }
 /**
 * Allow model relations to use in include
 * @var array
 */
 protected $possibleRelationships = ['profile'];
 /**
 * Allowed model columns to use in conditional query
 * @var array
 */
 protected $allow_where = array('name');
 /**
 * Allowed model columns to use in sort
 * @var array
 */
 protected $allow_order = array('name','created_at');
 /**
 * Allowed model columns to use in query search
 * @var array
 */
 protected $allow_like = array('name');
 /**
 * Allowed model columns to use in filter by date
 * @var array
 */
 protected $allow_between_dates = array('created_at');
 /**
 * Allowed model columns to use in filter by value
 * @var array
 */
 protected $allow_between_values = array();
}

P.S: See the usage of query params filter in Query Params

Routes example

Add the following routes, according to your need.

CRUD

Below is a table with base routes of APIs for a CRUD:

METHOD URI NAME
GET HEAD api/v1/users users.index
POST api/v1/users users.store
GET HEAD api/v1/users/{id} users.show
PUT PATCH api/v1/users/{id} users.update
DELETE api/v1/users/{id} users.destroy

Soft Delete

In some cases, generally, when the model in question is part of the main functionality of the system, we do not want to lose the model's activity trail so we prefer to work with * SOFT DELETE *, which consists of activating or deactivating the model. This practice is very common in the case of customers or products, in this case in addition to traditional methods, two more must be created, they are:

METHOD URI NAME
DELETE api/v1/users/{id}/force users.forceDestroy
PUT api/v1/users/{id}/restore users.restore

Note: In APIs that use SOFT DELETE by default, listings should only bring active models.

In the case of APIS that choose to use SOFT DELETE, the API by default will only list active models and that is why two important filters that must be created are:

  • ?onlyTrashed=1: return only deactivated models
  • ?withTrashed=1: return models of all status (active and deactivated)

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /