Skip to content

Feature/rest api #57

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

Open
wants to merge 2 commits into
base: main
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
52 changes: 0 additions & 52 deletions .env.example

This file was deleted.

117 changes: 117 additions & 0 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ProjectRequest;
use App\Repositories\ProjectRepository;
use App\Traits\ResponseTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class ProjectController extends Controller
{
/**
* Response trait to handle return responses.
*/
use ResponseTrait;

/**
* Project Repository class.
*
* @var ProjectRepository
*/
public $projectRepository;

public function __construct(ProjectRepository $projectRepository)
{
// $this->middleware('auth:api', ['except' => ['indexAll']]);
$this->projectRepository = $projectRepository;
}

public function index(): JsonResponse
{
try {
$data = $this->projectRepository->getAll();
return $this->responseSuccess($data, 'Project List Fetch Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function indexAll(Request $request): JsonResponse
{
try {
$data = $this->projectRepository->getPaginatedData($request->perPage);
return $this->responseSuccess($data, 'Project List Fetched Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function search(Request $request): JsonResponse
{
try {
$data = $this->projectRepository->searchProject($request->search, $request->perPage);
return $this->responseSuccess($data, 'Project List Fetched Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function store(ProjectRequest $request): JsonResponse
{
try {
$project = $this->projectRepository->create($request->all());
return $this->responseSuccess($project, 'New Project Created Successfully !');
} catch (\Exception $exception) {
return $this->responseError(null, $exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function show($id): JsonResponse
{
try {
$data = $this->projectRepository->getByID($id);
if (is_null($data)) {
return $this->responseError(null, 'Project Not Found', Response::HTTP_NOT_FOUND);
}

return $this->responseSuccess($data, 'Project Details Fetch Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
public function update(ProjectRequest $request, $id): JsonResponse
{
try {
$data = $this->projectRepository->update($id, $request->all());
if (is_null($data))
return $this->responseError(null, 'Project Not Found', Response::HTTP_NOT_FOUND);

return $this->responseSuccess($data, 'Project Updated Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function destroy($id): JsonResponse
{
try {
$project = $this->projectRepository->getByID($id);
if (empty($project)) {
return $this->responseError(null, 'Project Not Found', Response::HTTP_NOT_FOUND);
}

$deleted = $this->projectRepository->delete($id);
if (!$deleted) {
return $this->responseError(null, 'Failed to delete the project.', Response::HTTP_INTERNAL_SERVER_ERROR);
}

return $this->responseSuccess($project, 'Project Deleted Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}
117 changes: 117 additions & 0 deletions app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\TaskRequest;
use App\Repositories\TaskRepository;
use App\Traits\ResponseTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;

class TaskController extends Controller
{
/**
* Response trait to handle return responses.
*/
use ResponseTrait;

/**
* Task Repository class.
*
* @var TaskRepository
*/
public $taskRepository;

public function __construct(TaskRepository $taskRepository)
{
// $this->middleware('auth:api', ['except' => ['indexAll']]);
$this->taskRepository = $taskRepository;
}

public function index(): JsonResponse
{
try {
$data = $this->taskRepository->getAll();
return $this->responseSuccess($data, 'Task List Fetch Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function indexAll(Request $request): JsonResponse
{
try {
$data = $this->taskRepository->getPaginatedData($request->perPage);
return $this->responseSuccess($data, 'Task List Fetched Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function search(Request $request): JsonResponse
{
try {
$data = $this->taskRepository->searchTask($request->search, $request->perPage);
return $this->responseSuccess($data, 'Task List Fetched Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function store(TaskRequest $request): JsonResponse
{
try {
$task = $this->taskRepository->create($request->all());
return $this->responseSuccess($task, 'New Task Created Successfully !');
} catch (\Exception $exception) {
return $this->responseError(null, $exception->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function show($id): JsonResponse
{
try {
$data = $this->taskRepository->getByID($id);
if (is_null($data)) {
return $this->responseError(null, 'Task Not Found', Response::HTTP_NOT_FOUND);
}

return $this->responseSuccess($data, 'Task Details Fetch Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
public function update(TaskRequest $request, $id): JsonResponse
{
try {
$data = $this->taskRepository->update($id, $request->all());
if (is_null($data))
return $this->responseError(null, 'Task Not Found', Response::HTTP_NOT_FOUND);

return $this->responseSuccess($data, 'Task Updated Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}

public function destroy($id): JsonResponse
{
try {
$task = $this->taskRepository->getByID($id);
if (empty($task)) {
return $this->responseError(null, 'Task Not Found', Response::HTTP_NOT_FOUND);
}

$deleted = $this->taskRepository->delete($id);
if (!$deleted) {
return $this->responseError(null, 'Failed to delete the task.', Response::HTTP_INTERNAL_SERVER_ERROR);
}

return $this->responseSuccess($task, 'Task Deleted Successfully !');
} catch (\Exception $e) {
return $this->responseError(null, $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}
Loading