All Projects → jsdecena → baserepo

jsdecena / baserepo

Licence: MIT license
Base repository

Programming Languages

PHP
23972 projects - #3 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to baserepo

GraphQL.RepoDB
A set of extensions for working with HotChocolate GraphQL and Database access with micro-orms such as RepoDb (or Dapper). This extension pack provides access to key elements such as Selections/Projections, Sort arguments, & Paging arguments in a significantly simplified facade so this logic can be leveraged in the Serivces/Repositories that enca…
Stars: ✭ 25 (-64.79%)
Mutual labels:  repository, repository-pattern
Model
Ruby persistence framework with entities and repositories
Stars: ✭ 399 (+461.97%)
Mutual labels:  repository, repository-pattern
laravel-repository
Repository pattern implementation for Laravel
Stars: ✭ 49 (-30.99%)
Mutual labels:  repository, repository-pattern
Bandit
Human-friendly unit testing for C++11
Stars: ✭ 240 (+238.03%)
Mutual labels:  tdd, test-driven-development
laravel-snowflake
This Laravel package to generate 64 bit identifier like the snowflake within Twitter.
Stars: ✭ 94 (+32.39%)
Mutual labels:  lumen, lumen-package
laravository
Simplified Repository pattern implementation in Laravel
Stars: ✭ 14 (-80.28%)
Mutual labels:  repository, repository-pattern
Android Architecture Components
The template project that uses Android Architecture Components with Repository pattern. The simple app that uses awesome Fuel library instead of Retrofit for perfoming HTTP request. The app also persists data using the Room library and display data in RecyclerView.
Stars: ✭ 329 (+363.38%)
Mutual labels:  repository, repository-pattern
Junit Dataprovider
A TestNG like dataprovider runner for JUnit with many additional features
Stars: ✭ 226 (+218.31%)
Mutual labels:  tdd, test-driven-development
Hexagonal-architecture-ASP.NET-Core
App generator API solution template which is built on Hexagnonal Architecture with all essential feature using .NET Core
Stars: ✭ 57 (-19.72%)
Mutual labels:  repository, repository-pattern
receptacle
minimalistic implementation of the repository pattern
Stars: ✭ 18 (-74.65%)
Mutual labels:  repository, repository-pattern
Auth Tests
Always-current tests for Laravel's authentication system. Curated by the community.
Stars: ✭ 230 (+223.94%)
Mutual labels:  tdd, laravel-5-package
PixelTest
Fast, modern, simple iOS snapshot testing written purely in Swift.
Stars: ✭ 56 (-21.13%)
Mutual labels:  tdd, test-driven-development
Mockito Scala
Mockito for Scala language
Stars: ✭ 231 (+225.35%)
Mutual labels:  tdd, test-driven-development
eloquent-repository
Repository pattern for Eloquent ORM with focus in cache.
Stars: ✭ 30 (-57.75%)
Mutual labels:  repository, repository-pattern
Aprenda Go Com Testes
Aprenda Go com desenvolvimento orientado a testes
Stars: ✭ 230 (+223.94%)
Mutual labels:  tdd, test-driven-development
BetterRepository
Better Enhanced Repository Pattern Implementation in .NET C#
Stars: ✭ 27 (-61.97%)
Mutual labels:  repository, repository-pattern
Learn Go With Tests
Learn Go with test-driven development
Stars: ✭ 16,056 (+22514.08%)
Mutual labels:  tdd, test-driven-development
Javascript Todo List Tutorial
✅ A step-by-step complete beginner example/tutorial for building a Todo List App (TodoMVC) from scratch in JavaScript following Test Driven Development (TDD) best practice. 🌱
Stars: ✭ 212 (+198.59%)
Mutual labels:  tdd, test-driven-development
Onion Architecture Asp.net Core
WhiteApp API solution template which is built on Onion Architecture with all essential feature using .NET 5!
Stars: ✭ 196 (+176.06%)
Mutual labels:  repository, repository-pattern
laravel-repository-pattern
Files autogenerator for repositorry pattern
Stars: ✭ 46 (-35.21%)
Mutual labels:  repository, repository-pattern

Base Repository Package

master Latest Stable Version Total Downloads License FOSSA Status

Sign-up with Digital Ocean and get $20 discount!

Buy me a coffeee 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.

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.
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

License

FOSSA Status

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].