Skip to content

Feature/rest api #27

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

Closed
Closed
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
39 changes: 39 additions & 0 deletions app/Helpers/ValidationHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Helpers;

class ValidationHelper
{
public static function getLoginRules()
{
return [
'email' => 'required|exists:users,email',
'password' => 'required'
];
}

public static function getUserDetailsRules()
{
return [
'id' => 'required|uuid'
];
}

public static function getCreateUserRules()
{
return [
'email' => 'required|email|unique:users,email',
'password' => 'nullable',
'name' => 'required|string'
];
}

public static function getUpdateUserRules()
{
return [
'id' => 'required|exists:users,id',
'email' => 'required|email|unique:users,email',
'name' => 'required|string'
];
}
}
35 changes: 35 additions & 0 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use Illuminate\Support\Facades\Validator;
use App\Helpers\ValidationHelper;
use App\Services\AuthServices;

class LoginController extends BaseController
{
public $authService;

public function __construct()
{
$this->authService = new AuthServices();
}
public function login(Request $request)
{
$input = $request->all();
$validator = Validator::make($input, ValidationHelper::getLoginRules());

if ($validator->fails()) {
return $this->sendError('Unprocessable Entity.', $validator->errors(), 422);
}

try {
$token = $this->authService->login($input);
return $this->sendResponse('Login successfully', $token);
} catch (\Exception $e) {
return $this->sendError('Login failed.', $e->getMessage, 500);
}
}
}
31 changes: 31 additions & 0 deletions app/Http/Controllers/BaseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Controllers;

class BaseController extends Controller
{
public function sendResponse($message = null, $result = null, $code = 200)
{
$response = [
'success' => true,
'data' => $result,
'message' => $message,
];

return response()->json($response, $code);
}

public function sendError($error, $errorMessages = [], $code = 500)
{
$response = [
'success' => false,
'message' => $error,
];

if (!empty($errorMessages)) {
$response['error'] = $errorMessages;
}

return response()->json($response, $code);
}
}
112 changes: 112 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;
use App\Services\UserServices;
use Illuminate\Support\Facades\Validator;
use App\Helpers\ValidationHelper;

class UserController extends BaseController
{
private $userService;

public function __construct()
{
$this->userService = new UserServices();
}

public function getUsers()
{
try {
$data = $this->userService->getUsers();
return $this->sendResponse('Retrieved users successfully', $data);
} catch (\Exception $e) {
return $this->sendError('Retrieved users failed.', $e->getMessage, 500);
}
}

public function getUser(Request $request, $user_id)
{
try {
$id = $user_id;
$validator = Validator::make(
[
'id' => $id
],
[
'id' => 'required|uuid|exists:users,id'
]
);

if ($validator->fails()) {
return $this->sendError('Unprocessable Entity.', $validator->errors(), 422);
}

$data = $this->userService->getUser($id);
return $this->sendResponse('Retrieved user successfully', $data);
} catch (\Exception $e) {
return $this->sendError('Retrieved user failed.', $e->getMessage, 500);
}
}

public function createUser(Request $request)
{
try {
$input = $request->all();
$validator = Validator::make($input, ValidationHelper::getCreateUserRules());

if ($validator->fails()) {
return $this->sendError('Unprocessable Entity.', $validator->errors(), 422);
}

$data = $this->userService->createUser($input);
return $this->sendResponse('Created user successfully', $data);
} catch (\Exception $e) {
return $this->sendError('Create user failed', $e->getMessage());
}
}

public function updateUser(Request $request, $user_id)
{
try {
$input = $request->all();
$input['id'] = $user_id;
$validator = Validator::make($input, ValidationHelper::getUpdateUserRules());

if ($validator->fails()) {
return $this->sendError('Unprocessable Entity.', $validator->errors(), 422);
}

$data = $this->userService->updateUser($input);
return $this->sendResponse('Updated user successfully', $data);
} catch (\Exception $e) {
return $this->sendError('Updated user failed.', $e->getMessage, 500);
}
}

public function deleteUser(Request $request, $user_id)
{
try {
$id = $user_id;
$validator = Validator::make(
[
'id' => $id
],
[
'id' => 'required|uuid|exists:users,id'
]
);

if ($validator->fails()) {
return $this->sendError('Unprocessable Entity.', $validator->errors(), 422);
}

$data = $this->userService->deleteUser($id);
return $this->sendResponse('Deleted user successfully');
} catch (\Exception $e) {
return $this->sendError('Deleted user failed.', $e->getMessage, 500);
}
}
}
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Kernel extends HttpKernel
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
Expand Down
23 changes: 23 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Project extends Model
{
use HasFactory;

protected $table = 'projects';

protected $fillable = [
'id',
'name'
];

public function tasks()
{
return $this->hasMany(Task::class, 'project_id');
}
}
32 changes: 32 additions & 0 deletions app/Models/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
use HasFactory;

protected $table = 'tasks';

protected $fillable = [
'id',
'title',
'description',
'status',
'project_id',
'user_id'
];

public function project()
{
return $this->belongsTo(Project::class, 'project_id');
}

public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
13 changes: 12 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes;
// use App\Traits\Uuid;

class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
use HasApiTokens, HasFactory, Notifiable, SoftDeletes;

public $incrementing = false;

/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'id',
'name',
'email',
'password',
Expand All @@ -39,6 +44,12 @@ class User extends Authenticatable
* @var array<string, string>
*/
protected $casts = [
'id' => 'string',
'email_verified_at' => 'datetime',
];

public function tasks()
{
return $this->hasMany(Task::class, 'user_id');
}
}
25 changes: 25 additions & 0 deletions app/Services/AuthServices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Services;

use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class AuthServices
{
public function login($input)
{
try {
if (Auth::attempt(['email' => $input['email'], 'password' => $input['password']])) {
$user = auth()->user();
$token = $user->createToken($user->id);

return $token->plainTextToken;
}
} catch (\Exception $e) {
Log::error('(Error) Login failed. Error: ' . PHP_EOL . $e->getMessage());
throw new \Exception('Login failed.');
}
}
}
Loading