Skip to content

Commit

Permalink
Merge pull request #2 from ezra-obiwale/wip
Browse files Browse the repository at this point in the history
Wip
  • Loading branch information
ezra-obiwale authored Nov 24, 2017
2 parents 7c99188 + 9e92eca commit d423781
Show file tree
Hide file tree
Showing 24 changed files with 1,022 additions and 531 deletions.
5 changes: 0 additions & 5 deletions docs/landing.html

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Api Trait

<p class="tip">
This trait uses the [Crud trait](/controllers/traits/crud).
This trait uses the [Crud trait](/v1/controllers/traits/crud).
</p>

This trait implements the response methods of the [Crud trait](/controllers/traits/crud).
This trait implements the response methods of the [Crud trait](/v1/controllers/traits/crud).
It is meant to be used on controllers meant for API endpoints.

## Implemented Methods
Expand All @@ -15,7 +15,7 @@ It is meant to be used on controllers meant for API endpoints.
protected function indexResponse ( `mixed` $data ) : `Illuminate\Http\Response`
</p>

Returns the [Respond trait's paginatedList](/controllers/traits/respond#paginatedlist-)
Returns the [Respond trait's paginatedList](/v1/controllers/traits/respond#paginatedlist-)
method response after applying it to the `$data`.

### storeResponse ( `...` )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Attachable Trait

Shortcut methods to making working with related many-to-many models faster. It is
intended to be used with the [Api trait](/controllers/traits/api).
intended to be used with the [Api trait](/v1/controllers/traits/api).

<p class="tip">
See [Laravel Doc](https://laravel.com/docs/5.5/eloquent-relationships#updating-many-to-many-relationships)
Expand All @@ -11,7 +11,7 @@ intended to be used with the [Api trait](/controllers/traits/api).
These methods are meant to be called from within custom endpoint methods and
not used directly as the parameters would need to be customized.

For ready-made endpoint methods, use the [Pivotable trait](/controllers/traits/pivotable).
For ready-made endpoint methods, use the [Pivotable trait](/v1/controllers/traits/pivotable).
</p>

## Route Methods
Expand Down Expand Up @@ -65,20 +65,20 @@ models to attach to the base model.
protected function attachModel ( ) : string
</p>

The base model to be used for when attaching. Defaults to [Api trait's method model ( )](/controllers/traits/api#model-).
The base model to be used for when attaching. Defaults to [Api trait's method model ( )](/v1/controllers/traits/api#model-).

### detachModel ( )

<p class="tip no-bg">
protected function detachModel ( ) : string
</p>

The base model to be used for when detaching. Defaults to [Api trait's method model ( )](/controllers/traits/api#model-).
The base model to be used for when detaching. Defaults to [Api trait's method model ( )](/v1/controllers/traits/api#model-).

### syncModel ( )

<p class="tip no-bg">
protected function syncModel ( ) : string
</p>

The base model to be used for when synching. Defaults to [Api trait's method model ( )](/controllers/traits/api#model-).
The base model to be used for when synching. Defaults to [Api trait's method model ( )](/v1/controllers/traits/api#model-).
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Crud Trait

Adds all necessary methods for a complete CRUD operation on the given model.
It uses the [Respond](/controllers/traits/respond) trait.
It uses the [Respond](/v1/controllers/traits/respond) trait.

## Usage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
A collection of methods to help pass requests to another server. The
controller serves as a passage to the actual server that's to deal with the request.

It uses the [Api trait](/controllers/traits/api). Since the request would be passed
along, there won't be any need for the [Api trait's abstract method model( )](/controllers/traits/api#model-).
It uses the [Api trait](/v1/controllers/traits/api). Since the request would be passed
along, there won't be any need for the [Api trait's abstract method model( )](/v1/controllers/traits/api#model-).
The method has therefore been implemented.

The [abstract method validationRules ( `...` )](/controllers/traits/api#validationrules-)
The [abstract method validationRules ( `...` )](/v1/controllers/traits/api#validationrules-)
must however still be implemented as validations can be made on this server before
passing on the action processing server.

<p class="tip">
This trait also uses the [Http helper class](/helpers/http).
This trait also uses the [Http helper class](/v1/helpers/http).
</p>

## Properties
Expand Down Expand Up @@ -142,23 +142,23 @@ Called when deleting a model.
protected function httpRequest ( `string` $method, `string` $url, `array` $options = [] ) : `mixed`
</p>

A shortcut to [Http request( `...` )](/helpers/http#request-).
A shortcut to [Http request( `...` )](/v1/helpers/http#request-).

### httpResponse ( )

<p class="tip no-bg">
protected function httpResponse ( ) : `mixed`
</p>

A shortcut to [Http rawResponse( `...` )](/helpers/http#rawresponse-).
A shortcut to [Http rawResponse( `...` )](/v1/helpers/http#rawresponse-).

### httpStatusCode ( )

<p class="tip no-bg">
protected function httpStatusCode ( ) : `mixed`
</p>

A shortcut to [Http getStatusCode( `...` )](/helpers/http#getstatuscode-).
A shortcut to [Http getStatusCode( `...` )](/v1/helpers/http#getstatuscode-).

### methodMap ( )

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Pivotable Trait

This trait adds endpoint-ready methods for using the [Attachable trait](/controllers/traits/attachable).
This trait adds endpoint-ready methods for using the [Attachable trait](/v1/controllers/traits/attachable).

<p class="tip">
This trait uses the [Attachable trait](/controllers/traits/attachable).
This trait uses the [Attachable trait](/v1/controllers/traits/attachable).
</p>

## Endpoint Methods
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Web Trait

<p class="tip">
This trait uses the [Crud trait](/controllers/traits/crud).
This trait uses the [Crud trait](/v1/controllers/traits/crud).
</p>

This trait implements some response methods of the [Crud trait](/controllers/traits/crud).
This trait implements some response methods of the [Crud trait](/v1/controllers/traits/crud).
It is meant to be used on controllers meant for web endpoints.

## Implemented Methods
Expand Down Expand Up @@ -51,4 +51,4 @@ Redirects to the previous page with a status message bearing the number of succe

Redirects to the previous page with a status message and errors, if available.

This covers for all error messages coming from the [Crud trait](/controllers/traits/crud).
This covers for all error messages coming from the [Crud trait](/v1/controllers/traits/crud).
File renamed without changes.
26 changes: 26 additions & 0 deletions docs/v1/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
sidebar: false
---
# Laraquick

A collection of classes to be extended/used in laravel api applications for quick
development.


## Controller Traits

- [Api](/v1/controllers/traits/api)
- [Attachable](/v1/controllers/traits/attachable)
- [PassThrough](/v1/controllers/traits/pass-through)
- [Pivotable](/v1/controllers/traits/pivotable)
- [Referer](/v1/controllers/traits/referer)
- [Respond](/v1/controllers/traits/respond)

## Helpers

- [Http](/v1/helpers/http)

## Contribution

Contributions are absolutely welcome. Create a PR and I'll as swiftly as possible
merge it up.
14 changes: 12 additions & 2 deletions src/Controllers/Traits/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,25 @@ protected function updateResponse(Model $data)
return response()->json($data, 202);
}

protected function deleteResponse(Model $data)
protected function destroyResponse(Model $data)
{
return response()->json($data, 202);
}

protected function deleteManyResponse($deletedCount)
protected function forceDestroyResponse(Model $data)
{
return response()->json($data, 202);
}

protected function destroyManyResponse($deletedCount)
{
return response()->json([
"message" => "$deletedCount item(s) deleted successfully"
], 202);
}

protected function restoreDestroyedResponse(Model $data)
{
return response()->json($data, 202);
}
}
43 changes: 31 additions & 12 deletions src/Controllers/Traits/Attachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ protected function syncModel() {
return $this->model();
}

/**
* Fetches a paginated list of related items
*
* @param mixed $id
* @param string $relation
* @return Response
*/
public function attached($id, $relation) {
$model = $this->attachModel();
$model = is_object($model)
? $model->findOrFail($id)
: $model::findOrFail($id);
$list = $model->$relation()->simplePaginate();
return $this->paginatedList($list->toArray());
}

/**
* Attaches a list of items to the object at the given id
*
Expand All @@ -48,20 +64,21 @@ protected function syncModel() {
* @param string $paramKey
* @return Response
*/
public function attach($id, $relation, $paramKey = 'items')
public function attach($id, $relation, $paramKey = null)
{
$paramKey = $paramKey ?: $relation;
if (!$this->validate(request(), [
$paramKey => 'required|array'
]))
return $this->error($this->validationErrorMessage(), $this->validator->errors());
$model = $this->attachModel();
$group = is_object($model)
$model = is_object($model)
? $model->find($id)
: $model::find($id);
if (!$group) return $this->notFoundError();
if (!$model) return $this->notFoundError();
try {
$items = request()->input($paramKey);
$group->$relation()->syncWithoutDetaching($items);
$model->$relation()->syncWithoutDetaching($items);
return response()->json([
'status' => 'ok'
]);
Expand All @@ -80,20 +97,21 @@ public function attach($id, $relation, $paramKey = 'items')
* @param string $paramKey
* @return Response
*/
public function detach($id, $relation, $paramKey = 'items')
public function detach($id, $relation, $paramKey = null)
{
$paramKey = $paramKey ?: $relation;
if (!$this->validate(request(), [
$paramKey => 'required|array'
]))
return $this->error($this->validationErrorMessage(), $this->validator->errors());
$model = $this->detachModel();
$group = is_object($model)
$model = is_object($model)
? $model->find($id)
: $model::find($id);
if (!$group) return $this->notFoundError();
if (!$model) return $this->notFoundError();
try {
$items = request()->input($paramKey);
$group->$relation()->detach($items);
$model->$relation()->detach($items);
return response()->json([
'status' => 'ok'
]);
Expand All @@ -112,20 +130,21 @@ public function detach($id, $relation, $paramKey = 'items')
* @param string $paramKey
* @return Response
*/
public function sync($id, $relation, $paramKey = 'items')
public function sync($id, $relation, $paramKey = null)
{
$paramKey = $paramKey ?: $relation;
if (!$this->validate(request(), [
$paramKey => 'required|array'
]))
return $this->error($this->validationErrorMessage(), $this->validator->errors());
$model = $this->syncModel();
$group = is_object($model)
$model = is_object($model)
? $model->find($id)
: $model::find($id);
if (!$group) return $this->notFoundError();
if (!$model) return $this->notFoundError();
try {
$items = request()->input($paramKey);
$resp = $group->$relation()->sync($items);
$resp = $model->$relation()->sync($items);
$resp['added'] = $resp['attached'];
$resp['removed'] = $resp['detached'];
unset($resp['attached']);
Expand Down
Loading

0 comments on commit d423781

Please sign in to comment.