Skip to content

Commit c5ec431

Browse files
authored
Merge pull request #3 from amirsahra/feature/start
Feature/start
2 parents 45d7918 + 389c2d1 commit c5ec431

File tree

12 files changed

+360
-96
lines changed

12 files changed

+360
-96
lines changed

README.md

Lines changed: 242 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,263 @@
1-
<p align="center"><a href="https://laravel.com" target="_blank"><img src="https://raw.githubusercontent.com/laravel/art/master/logo-lockup/5%20SVG/2%20CMYK/1%20Full%20Color/laravel-logolockup-cmyk-red.svg" width="400"></a></p>
1+
# Todo task
22

3-
<p align="center">
4-
<a href="https://travis-ci.org/laravel/framework"><img src="https://travis-ci.org/laravel/framework.svg" alt="Build Status"></a>
5-
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/dt/laravel/framework" alt="Total Downloads"></a>
6-
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/v/laravel/framework" alt="Latest Stable Version"></a>
7-
<a href="https://packagist.org/packages/laravel/framework"><img src="https://img.shields.io/packagist/l/laravel/framework" alt="License"></a>
8-
</p>
3+
Example RESTFUll API in Laravel
4+
## Overview
95

10-
## About Laravel
6+
This repository serves as a comprehensive example of a meticulously crafted RESTful API using Laravel. It not only showcases the implementation of RESTful endpoints but also embodies best practices in coding, leveraging Laravel's robust features.
117

12-
Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel takes the pain out of development by easing common tasks used in many web projects, such as:
8+
### Features Showcased:
139

14-
- [Simple, fast routing engine](https://laravel.com/docs/routing).
15-
- [Powerful dependency injection container](https://laravel.com/docs/container).
16-
- Multiple back-ends for [session](https://laravel.com/docs/session) and [cache](https://laravel.com/docs/cache) storage.
17-
- Expressive, intuitive [database ORM](https://laravel.com/docs/eloquent).
18-
- Database agnostic [schema migrations](https://laravel.com/docs/migrations).
19-
- [Robust background job processing](https://laravel.com/docs/queues).
20-
- [Real-time event broadcasting](https://laravel.com/docs/broadcasting).
10+
- **Implementation of RESTful API Endpoints:** Demonstrates a well-defined structure and functionality of various API endpoints adhering to RESTful principles.
11+
- **Leveraging Laravel's Response and Request Handling:** Utilizes Laravel's built-in functionalities effectively for handling responses, managing requests, and ensuring secure data flow.
12+
- **Exemplary Demonstration of Clean and Structured Code:** Emphasizes clean, maintainable, and structured coding practices, serving as a guide for developers to create organized and scalable applications.
2113

22-
Laravel is accessible, powerful, and provides tools required for large, robust applications.
14+
This example repository offers an insightful reference for implementing RESTful APIs in Laravel. Dive into the codebase to gain a deeper understanding of utilizing Laravel's features effectively and implementing industry-best practices.
2315

24-
## Learning Laravel
16+
### Purpose
2517

26-
Laravel has the most extensive and thorough [documentation](https://laravel.com/docs) and video tutorial library of all modern web application frameworks, making it a breeze to get started with the framework.
18+
Primarily, this source code repository serves as an educational resource, exemplifying how to architect and structure a robust RESTful API project using Laravel. It aims to aid developers in comprehending the implementation of Laravel features and adopting optimal coding practices for their projects.
2719

28-
If you don't feel like reading, [Laracasts](https://laracasts.com) can help. Laracasts contains over 1500 video tutorials on a range of topics including Laravel, modern PHP, unit testing, and JavaScript. Boost your skills by digging into our comprehensive video library.
20+
Feel free to explore, learn, and adapt the showcased patterns and practices to enhance your own projects.
2921

30-
## Laravel Sponsors
22+
## Installation
3123

32-
We would like to extend our thanks to the following sponsors for funding Laravel development. If you are interested in becoming a sponsor, please visit the Laravel [Patreon page](https://patreon.com/taylorotwell).
24+
### Prerequisites
3325

34-
### Premium Partners
26+
- PHP ^7.3 | ^8.0
27+
- laravel 8
3528

36-
- **[Vehikl](https://vehikl.com/)**
37-
- **[Tighten Co.](https://tighten.co)**
38-
- **[Kirschbaum Development Group](https://kirschbaumdevelopment.com)**
39-
- **[64 Robots](https://64robots.com)**
40-
- **[Cubet Techno Labs](https://cubettech.com)**
41-
- **[Cyber-Duck](https://cyber-duck.co.uk)**
42-
- **[Many](https://www.many.co.uk)**
43-
- **[Webdock, Fast VPS Hosting](https://www.webdock.io/en)**
44-
- **[DevSquad](https://devsquad.com)**
45-
- **[Curotec](https://www.curotec.com/services/technologies/laravel/)**
46-
- **[OP.GG](https://op.gg)**
47-
- **[WebReinvent](https://webreinvent.com/?utm_source=laravel&utm_medium=github&utm_campaign=patreon-sponsors)**
48-
- **[Lendio](https://lendio.com)**
29+
### Steps
4930

50-
## Contributing
31+
1. Clone the repository:
32+
```bash
33+
git clone https://github.com/amirsahra/todo-task-api.git
34+
```
35+
2. Install dependencies:
36+
```bash
37+
composer install
38+
```
39+
3. Set up environment variables by creating a `.env` file:
40+
```bash
41+
cp .env.example .env
42+
```
43+
Update `.env` with your configuration (database, app key, etc.).
44+
45+
4. Run migrations and seeders (if applicable):
46+
```bash
47+
php artisan migrate --seed
48+
```
49+
50+
## Usage
51+
52+
Explain how to use your API:
53+
54+
1. Start the development server:
55+
```bash
56+
php artisan serve
57+
```
58+
59+
2. Use tools like Postman or curl to interact with the API endpoints.
60+
61+
## API Endpoints
62+
63+
### List of Available Endpoints
64+
65+
#### Auth Endpoints
66+
67+
- **Login**
68+
- Method: `POST`
69+
- Description: Logs in a user with their credentials.
70+
- Example:
71+
```bash
72+
POST http://127.0.0.1:8000/api/V1/login
73+
74+
Body:
75+
{
76+
"email": "vhammes@example.com",
77+
"password": "123456789"
78+
}
79+
```
80+
81+
- **Logout**
82+
- Method: `POST`
83+
- Description: Logs out the currently logged-in user.
84+
- Example:
85+
```bash
86+
POST http://127.0.0.1:8000/api/V1/logout
87+
88+
Body:
89+
{
90+
"email": "vhammes@example.com",
91+
"password": "123456789"
92+
}
93+
```
94+
95+
- **Register**
96+
- Method: `POST`
97+
- Description: Registers a new user.
98+
- Example:
99+
```bash
100+
POST http://127.0.0.1:8000/api/V1/register
101+
102+
Body:
103+
{
104+
"name": "amir",
105+
"email": "amirhosein.sahra@gmail.com",
106+
"password": "123456789"
107+
}
108+
```
109+
110+
- **Show Current User**
111+
- Method: `GET`
112+
- Description: Retrieves information about the currently logged-in user.
113+
- Example:
114+
```sql
115+
GET http://127.0.0.1:8000/api/user
116+
117+
Headers:
118+
{
119+
"Authorization": "Bearer 1|pY6W2ys7VWlTgaSr4pAbc0XOVIRWcbdNihqaWCMU",
120+
"Accept": "application/json",
121+
"Referer": "localhost"
122+
}
123+
```
124+
125+
#### Task Endpoints
51126

52-
Thank you for considering contributing to the Laravel framework! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
127+
- **Index**
128+
- Method: `GET`
129+
- Description: Retrieves all tasks.
130+
- Example:
131+
```bash
132+
GET http://127.0.0.1:8000/api/V1/tasks
53133
54-
## Code of Conduct
134+
Headers:
135+
{
136+
"Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2",
137+
"Accept": "application/json"
138+
}
139+
```
55140

56-
In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct).
141+
- **Show**
142+
- Method: `GET`
143+
- Description: Retrieves details of a specific task by ID.
144+
- Example:
145+
```bash
146+
GET http://127.0.0.1:8000/api/V1/tasks/1
57147
58-
## Security Vulnerabilities
148+
Headers:
149+
{
150+
"Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2",
151+
"Accept": "application/json"
152+
}
153+
```
154+
155+
- **Store**
156+
- Method: `POST`
157+
- Description: Creates a new task.
158+
- Example:
159+
```css
160+
POST http://127.0.0.1:8000/api/V1/tasks
161+
162+
Headers:
163+
{
164+
"Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2",
165+
"Accept": "application/vnd.api+json"
166+
}
167+
Body:
168+
{
169+
"name": "amir hossein sahra"
170+
}
171+
```
172+
173+
- **Update**
174+
- Method: `PUT`
175+
- Description: Updates an existing task by ID.
176+
- Example:
177+
```css
178+
PUT http://127.0.0.1:8000/api/V1/tasks/13
179+
180+
Headers:
181+
{
182+
"Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2",
183+
"Accept": "application/json"
184+
}
185+
Body:
186+
{
187+
"name": "new name update"
188+
}
189+
```
190+
191+
- **Completed**
192+
- Method: `PATCH`
193+
- Description: Marks a task as completed by ID.
194+
- Example:
195+
```bash
196+
PATCH http://127.0.0.1:8000/api/V1/tasks/10/complete
197+
198+
Headers:
199+
{
200+
"Authorization": "Bearer 1|pY6W2ys7VWlTgaSr4pAbc0XOVIRWcbdNihqaWCMU",
201+
"Accept": "application/json"
202+
}
203+
```
204+
205+
- **Destroy**
206+
- Method: `DELETE`
207+
- Description: Deletes a task by ID.
208+
- Example:
209+
```bash
210+
DELETE http://127.0.0.1:8000/api/V1/tasks/11
211+
212+
Headers:
213+
{
214+
"Authorization": "Bearer 3|vJjX0aKJYOgcXodNqWqpWbImfE3MYUPxEwjPlMN2"
215+
}
216+
```
217+
218+
## Authentication
219+
### Token-Based Authentication
220+
221+
This API utilizes Token-Based Authentication, specifically using the Bearer Token for authentication. In this method, a token serves as the authentication credential for each request sent.
222+
223+
#### Examples of Authentication
224+
225+
##### Login Endpoint
226+
227+
To log in, a POST request is used at this address:
228+
229+
```http
230+
POST http://127.0.0.1:8000/api/V1/login
231+
232+
Body:
233+
{
234+
"email": "vhammes@example.com",
235+
"password": "123456789"
236+
}
237+
```
238+
This successful request provides the user with a token of type Bearer,
239+
which should be included in the headers of all subsequent requests:
240+
```http
241+
Authorization: Bearer YOUR_ACCESS_TOKEN
242+
```
243+
#### Authentication Example in Endpoints
244+
For instance, in the headers of the following request,
245+
the token used for authentication is sent as a Bearer in the request:
246+
```http
247+
GET http://127.0.0.1:8000/api/user
248+
249+
Headers:
250+
{
251+
"Authorization": "Bearer YOUR_ACCESS_TOKEN",
252+
"Accept": "application/json",
253+
"Referer": "localhost"
254+
}
255+
```
256+
257+
## Contributing
59258

60-
If you discover a security vulnerability within Laravel, please send an e-mail to Taylor Otwell via [taylor@laravel.com](mailto:taylor@laravel.com). All security vulnerabilities will be promptly addressed.
259+
Explain how other developers can contribute to the project. Include information about submitting issues, pull requests, coding standards, etc.
61260

62261
## License
63262

64-
The Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
263+
Specify the license for your project.

app/Exceptions/Handler.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace App\Exceptions;
44

5+
use Illuminate\Auth\AuthenticationException;
6+
use Illuminate\Database\Eloquent\ModelNotFoundException;
7+
use Illuminate\Database\QueryException;
58
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
9+
use Illuminate\Validation\ValidationException;
610
use Throwable;
711

812
class Handler extends ExceptionHandler
@@ -38,4 +42,45 @@ public function register()
3842
//
3943
});
4044
}
45+
46+
public function render($request, Throwable $exception)
47+
{
48+
if ($exception instanceof AuthenticationException) {
49+
return $this->generateExceptionErrorResponse($exception, 401);
50+
}
51+
52+
if ($exception instanceof ValidationException) {
53+
return $this->generateExceptionErrorResponse($exception, 422);
54+
}
55+
56+
if ($exception instanceof QueryException) {
57+
return $this->generateExceptionErrorResponse($exception, 500);
58+
}
59+
60+
if ($exception instanceof ModelNotFoundException) {
61+
return $this->generateExceptionErrorResponse($exception, 404);
62+
}
63+
64+
// Handle other exceptions or errors here
65+
// For example, you can check for other types of exceptions and return appropriate responses
66+
67+
return parent::render($request, $exception);
68+
}
69+
70+
private function generateExceptionErrorResponse(Throwable $exception, int $status)
71+
{
72+
return response()->customJson(
73+
[
74+
'message' => $exception->getMessage(),
75+
'errors' => (isset($exception->validator)) ? $exception->validator->errors()->toArray() : array(),
76+
'line' => $exception->getLine(),
77+
'file' => $exception->getFile(),
78+
'code' => $exception->getCode(),
79+
'previous' => $exception->getPrevious(),
80+
'trace' => $exception->getTrace(),
81+
],
82+
$exception->getMessage(),
83+
$status
84+
);
85+
}
4186
}

app/Http/Controllers/Api/V1/CompleteTaskController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@
55
use App\Http\Controllers\Controller;
66
use App\Http\Resources\TaskResource;
77
use App\Models\Task;
8+
9+
810
use Illuminate\Http\Request;
911

1012
class CompleteTaskController extends Controller
1113
{
12-
public function __invoke(Request $request, Task $task): TaskResource
14+
public function __invoke(Request $request, Task $task)
1315
{
1416
$task->is_completed = true;
1517
$task->save();
1618

17-
return TaskResource::make($task);
19+
return response()->customJson(
20+
['task' => TaskResource::make($task)],
21+
'Task Complete Successfully',
22+
200
23+
);
24+
1825
}
1926
}

0 commit comments

Comments
 (0)