Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jsdecena/baserepo
Base repository
https://github.com/jsdecena/baserepo
laravel laravel-5-package lumen lumen-package php php7 repository repository-pattern tdd test-driven-development
Last synced: 26 days ago
JSON representation
Base repository
- Host: GitHub
- URL: https://github.com/jsdecena/baserepo
- Owner: jsdecena
- License: mit
- Created: 2018-07-16T00:58:51.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-05-03T08:03:55.000Z (over 2 years ago)
- Last Synced: 2024-09-29T20:22:33.511Z (about 1 month ago)
- Topics: laravel, laravel-5-package, lumen, lumen-package, php, php7, repository, repository-pattern, tdd, test-driven-development
- Language: PHP
- Size: 60.5 KB
- Stars: 76
- Watchers: 6
- Forks: 42
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Base Repository Package
[![master](https://github.com/jsdecena/baserepo/actions/workflows/master.yaml/badge.svg)](https://github.com/jsdecena/baserepo/actions/workflows/master.yaml)
[![Latest Stable Version](https://poser.pugx.org/jsdecena/baserepo/v/stable)](https://packagist.org/packages/jsdecena/baserepo)
[![Total Downloads](https://poser.pugx.org/jsdecena/baserepo/downloads)](https://packagist.org/packages/jsdecena/baserepo)
[![License](https://poser.pugx.org/jsdecena/baserepo/license)](https://packagist.org/packages/jsdecena/baserepo)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fjsdecena%2Fbaserepo.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fjsdecena%2Fbaserepo?ref=badge_shield)## Sign-up with [Digital Ocean and get $20 discount](https://m.do.co/c/bce94237de96)!
## Buy me a [coffeee](https://ko-fi.com/G2G0ADEK) so I can continue development of this package
## How to install
- Run in your terminal `composer require jsdecena/baserepo`
- In your repository class, extend it so you can use the methods readily available.
```php
namespace App\Repositories;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Database\QueryException;
use Jsdecena\Baserepo\BaseRepository;class UserRepository extends BaseRepository {
public function __construct(User $user)
{
parent::__construct($user);
}
public function createUser(array $data) : User
{
try {
return $this->create($data);
} catch (QueryException $e) {
throw new \Exception($e);
}
}
}
```- Then, use it in your controller.
```php
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
use App\User;class MyController extends Controller {
private $userRepository;
/**
*
* Inject your repository or the interface here
*/
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}public function index()
{
$user = $this->userRepository->all();return response()->json($user);
}
public function store(Request $request)
{
// do data validation
try {
$user = $this->userRepository->createUser($request->all());
return response()->json($user, 201);
} catch (Illuminate\Database\QueryException $e) {
return response()->json([
'error' => 'user_cannot_create',
'message' => $e->getMessage()
]);
}
}public function show($id)
{
// do data validation
try {
$user = $this->userRepository->findOneOrFail($id);
return response()->json($user);
} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response()->json([
'error' => 'user_no_found',
'message' => $e->getMessage()
]);
}
}
public function update(Request $request, $id)
{
// do data validation
try {
$user = $this->userRepository->findOneOrFail($id);
// You can also do this now, so you would not have to instantiate again the repository
$this->userRepository->update($request->all(), $user);
return response()->json($user);
} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response()->json([
'error' => 'user_no_found',
'message' => $e->getMessage()
]);
} catch (Illuminate\Database\QueryException $e) {
return response()->json([
'error' => 'user_cannot_update',
'message' => $e->getMessage()
]);
}
}
public function destroy($id)
{
// do data validation
try {
$user = $this->userRepository->findOneOrFail($id);
// Create an instance of the repository again
// but now pass the user object.
// You can DI the repo to the controller if you do not want this.
$userRepo = new UserRepository($user);
$userRepo->delete()
return response()->json(['data' => 'User deleted.']);
} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response()->json([
'error' => 'user_no_found',
'message' => $e->getMessage()
]);
} catch (Illuminate\Database\QueryException $e) {
return response()->json([
'error' => 'user_cannot_delete',
'message' => $e->getMessage()
]);
}
}
}
```# Testing
- Run `make test`
# Author
[Jeff Simons Decena](https://jsdecena.me)
## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fjsdecena%2Fbaserepo.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fjsdecena%2Fbaserepo?ref=badge_large)