Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SELS-TASK] Implement the Displaying of Categories #8

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/Http/Controllers/Admin/CategoryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CategoryRequest;
use App\Repositories\CategoryInterface;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
protected $category;

public function __construct (CategoryInterface $category)
{
$this->category = $category;
}

public function store(CategoryRequest $request){

return $this->category->create($request->all());
}

public function all(){
return $this->category->paginate(10);
}

public function remove($id){
return $this->category->destroy($id);
}


public function update(CategoryRequest $request , $id){
return $this->category->update($request , $id);
}
}
28 changes: 28 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}

/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}
80 changes: 80 additions & 0 deletions app/Http/Controllers/PassportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PassportController extends Controller
{
/**
* Handles Registration Request
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function register(Request $request)
{
$this->validate($request, [
'name' => 'required|min:3',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);

$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password)
]);

$token = $user->createToken('sels')->accessToken;

return response()->json(['token' => $token , 'user' => $user], 200);
}

/**
* Handles Login Request
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function login(Request $request)
{
$credentials = [
'email' => $request->email,
'password' => $request->password
];

if (auth()->attempt($credentials)) {
$token = auth()->user()->createToken('sels')->accessToken;
return response()->json(['token' => $token , 'user' => auth()->user()], 200);
} else {
return response()->json(['errorMsg' => 'Incorrect Password or Email'], 401);
}
}

/**
* Returns Authenticated User Details
*
* @return \Illuminate\Http\JsonResponse
*/
public function details()
{
return response()->json(['user' => auth()->user()], 200);
}

/**
* Returns
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout(){

$user = Auth::user()->token();
$user->revoke();

return response()->json(['message' => 'Logout Successfully']);
}

}
31 changes: 31 additions & 0 deletions app/Http/Requests/CategoryRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CategoryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|unique:categories,title,'.request()->route('id'),
'description' => 'required',
];
}
}
10 changes: 10 additions & 0 deletions app/Models/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
protected $guarded = [];
}
5 changes: 4 additions & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

Expand All @@ -25,6 +26,8 @@ public function boot()
{
$this->registerPolicies();

//
Passport::routes();

Passport::tokensExpireIn(now()->addDays(15));
}
}
38 changes: 38 additions & 0 deletions app/Providers/RepositoryServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
// Base Repository
$this->app->singleton(
\App\Repositories\BaseInterface::class,
\App\Repositories\Eloquent\BaseRepository::class
);

// Category Repository
$this->app->singleton(
\App\Repositories\CategoryInterface::class,
\App\Repositories\Eloquent\CategoryRepository::class
);
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
//
}
}
77 changes: 77 additions & 0 deletions app/Repository/BaseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Repositories;

interface BaseInterface
{

/**
* Get number of records.
*
* @return array
*/
public function getCount();

/**
* Destroy a model.
*
* @param int $id
* @return void
*/
public function destroy($id);

/**
* Get Model by id.
*
* @param int $id
* @return App\Models\Model
*/
public function getById($id);

/**
* Get Model collection.
*
* @return App\Models\Model
*/
public function getAll();

/**
* // Eager load database relationships
*
* @param array $relationships
* @return App\Models\Model
*/
public function with($relationships);

/**
* Find Model collection with relationship.
*
* @param string $column , $data
* @return App\Models\Model
*/
public function findBy($column , $data);

/**
* Insert data to database
*
* @param array $request
* @return App\Models\Model
*/
public function create($request);

/**
* Get Model collection wth pagination
*
* @return App\Models\Model
*/
public function paginate($number);

/**
* Update data in database
*
* @param array $request
* @return App\Models\Model
*/
public function update($request , $id);

}
8 changes: 8 additions & 0 deletions app/Repository/CategoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Repositories;

interface CategoryInterface
{

}
Loading