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

Feature/felix branch #1

Open
wants to merge 4 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ Output
![A successful test should look like this in your Vs code terminal](https://github.com/InventorsDev/Laravel_SDG_2023_Intermediate_Challenge/assets/39954854/119e6ee5-58aa-471e-8c33-b5fad0cde412)


### Email

```
ayangefelix8@gmail.com
```


10 changes: 10 additions & 0 deletions app/Enum/TaskStatusEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Enum;

class TaskStatusEnum
{
const PENDING = 'pending';
const COMPLETED = 'completed';
const CANCELLED = 'cancelled';
}
49 changes: 48 additions & 1 deletion app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
use App\Traits\ApiResponses;
use Psy\Exception\FatalErrorException;
use Illuminate\Validation\ValidationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler
{
use ApiResponses;
/**
* A list of exception types with their corresponding custom log levels.
*
Expand Down Expand Up @@ -44,5 +51,45 @@ public function register(): void
$this->reportable(function (Throwable $e) {
//
});

$this->renderable(function (Throwable $exception, $request) {
return $this->handleException($exception, $request);
});
}


/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Throwable $exception
*
* @return \Symfony\Component\HttpFoundation\Response|\Illuminate\Http\JsonResponse
*
* @throws \Throwable
*/
public function handleException(Throwable $exception, $request)
{
if ($exception instanceof NotFoundHttpException) {
return $this->notFoundAlert('We cannot access this resource you\'re looking for', 'resource_not_found');
}

if ($exception instanceof ModelNotFoundException) {
return $this->notFoundAlert('Unable to locate model resource', 'model_not_found');
}

if ($exception instanceof HttpException) {
return $this->httpErrorAlert($exception->getMessage(), $exception);
}

if ($exception instanceof FatalErrorException) {
return $this->serverErrorAlert('An error occurred processing your request, Try again later... ', $exception);
}

if ($exception instanceof ValidationException) {
return $this->formValidationErrorAlert($exception->errors());
}

return $this->serverErrorAlert('An error occurred processing your request, Try again later... ', $exception);
}
}
45 changes: 42 additions & 3 deletions app/Http/Controllers/Api/AlgorithmController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,34 @@ class AlgorithmController extends Controller
// Question 1:

// Write a function called getMaxSum that takes an array of integers as input and returns the maximum sum of any contiguous subarray of the given array. If the array is empty or contains only negative integers, the function should return 0.
public function getMaxSum($arr) {
public function getMaxSum($arr)
{
$arr = explode(',', $arr);

$negativeIntegers = true;

foreach ($arr as $num) {
if ($num >= 0) {
$negativeIntegers = false;
break;
}
}

if (count($arr) === 0 || $negativeIntegers) {
return 0;
}

$maxSum = $arr[0];
$currentSum = $arr[0];

$length = count($arr);

for ($i = 1; $i < $length; $i++) {
$currentSum = max($arr[$i], $currentSum + $arr[$i]);
$maxSum = max($maxSum, $currentSum);
}

return $maxSum;
}

// Question 2:
Expand All @@ -20,8 +46,21 @@ public function getMaxSum($arr) {

// For example, if the input string is "hello world", the function should return "helo wrd".

public function uniqueChars($str) {
public function uniqueChars($str)
{
if (empty($str) || ctype_space($str)) {
return '';
}

}
$unique_chars = [];

for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
if (!in_array($char, $unique_chars)) {
$unique_chars[] = $char;
}
}

return '"' . implode('', $unique_chars) . '"';
}
}
65 changes: 65 additions & 0 deletions app/Http/Controllers/Api/TaskController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Task;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\TaskResource;
use App\Http\Requests\CreateTaskRequest;
use App\Http\Requests\UpdateTaskRequest;

class TaskController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$tasks = Task::query()->get();

return TaskResource::collection($tasks);
}

/**
* Store a newly created resource in storage.
*/
public function store(CreateTaskRequest $request)
{
$newTask = Task::create($request->validated());

$task = Task::find($newTask->id);

return $this->createdResponse("Task created successfully", new TaskResource($task));
}

/**
* Display the specified resource.
*/
public function show(Task $task)
{
$taskResource = new TaskResource($task);

return $this->successResponse("Task retrieved successfully", $taskResource);
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateTaskRequest $request, Task $task)
{
$task->update($request->validated());

return $this->successResponse("Task updated successfully", new TaskResource($task));
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Task $task)
{
$task->delete();

return $this->noContentResponse();
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace App\Http\Controllers;

use App\Traits\ApiResponses;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
use AuthorizesRequests, ValidatesRequests, ApiResponses;
}
32 changes: 32 additions & 0 deletions app/Http/Requests/CreateTaskRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use App\Enum\TaskStatusEnum;
use Illuminate\Foundation\Http\FormRequest;

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

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
'title' => 'required|string|min:5|max:255|unique:tasks,title',
'description' => 'required|string',
'dueDate' => 'required|string',
'status' => 'string|in:' . TaskStatusEnum::PENDING . ',' . TaskStatusEnum::COMPLETED . ',' . TaskStatusEnum::CANCELLED,
];
}
}
33 changes: 33 additions & 0 deletions app/Http/Requests/UpdateTaskRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Requests;

use App\Enum\TaskStatusEnum;
use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

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

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
public function rules(): array
{
return [
'title' => ['filled', Rule::unique('tasks')->ignore($this->task)],
'description' => 'filled|string',
'dueDate' => 'filled|string',
'status' => 'filled|in:' . TaskStatusEnum::PENDING . ',' . TaskStatusEnum::COMPLETED . ',' . TaskStatusEnum::CANCELLED,
];
}
}
6 changes: 3 additions & 3 deletions app/Http/Resources/TaskResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class TaskResource extends JsonResource
{
/**
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
Expand All @@ -21,8 +21,8 @@ public function toArray($request)
'description' => $this->description,
'status' => $this->status,
'dueDate' => $this->dueDate,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'created_at' => $this->created_at->format('Y-m-d h:i:s'),
'updated_at' => $this->updated_at->format('Y-m-d h:i:s'),
];
}
}
Loading