Skip to content

Create REST APIs #38

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
55 changes: 55 additions & 0 deletions app/Http/Controllers/Api/V1/ProjectController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Project;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class ProjectController extends Controller
{
/* function to fetch all Projects */
public function index()
{
return Project::all();
}

/* function to fetch Project for which id given in route */
public function showProject($id)
{
return Project::find($id);
}

/* function to create new Project */
public function createProject()
{
$data = [
['name'=>'Project 4']
];

Project::insert($data);

echo "record inserted";
}

/* function to update Project with specific Id */
public function updateProject(Request $request)
{
$id = $request->id;
$data = ['name'=>'Project 5'];

Project::where(['id'=>$id])
->update($data);
return Project::find($id);
}

/* function to delete Project with given Id */
public function deleteProject(Request $request)
{
$id = $request->id;
Project::where(['id'=>$id])
->delete();
echo "Project deleted successfully";
}
}
55 changes: 55 additions & 0 deletions app/Http/Controllers/Api/V1/TaskController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Task;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class TaskController extends Controller
{
/* function to fetch all Tasks */
public function index()
{
return Task::all();
}

/* function to fetch Task for which id given in route */
public function showTask($id)
{
return Task::find($id);
}

/* function to create new Task */
public function createTask()
{
$data = [
['title'=>'Task 4', 'description'=> 'dummy text', 'status' => 1, 'project_id' => 1, 'user_id' => 1]
];

Task::insert($data);

echo "record inserted";
}

/* function to update Task with specific Id */
public function updateTask(Request $request)
{
$id = $request->id;
$data = ['title'=>'Task 5', 'description'=> 'dummy text updated', 'status' => 1, 'project_id' => 1, 'user_id' => 1];

Task::where(['id'=>$id])
->update($data);
return Task::find($id);
}

/* function to delete Task with given Id */
public function deleteTask(Request $request)
{
$id = $request->id;
Task::where(['id'=>$id])
->delete();
echo "Task deleted successfully";
}
}
55 changes: 55 additions & 0 deletions app/Http/Controllers/Api/V1/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{

/* function to fetch all users */
public function index()
{
return User::all();
}

/* function to fetch user for which id given in route */
public function showUser($id)
{
return User::find($id);
}

/* function to create new user */
public function createUser()
{
$data = [
['name'=>'Jason1', 'email'=> '[email protected]', 'password' => '123']
];

User::insert($data);

echo "record inserted";
}

/* function to update user with specific Id */
public function updateUser(Request $request)
{
$id = $request->id;
$data = ['name'=>'Jason2', 'email'=> '[email protected]', 'password' => '1234'];

User::where(['id'=>$id])
->update($data);
return User::find($id);
}

/* function to delete user with given Id */
public function deleteUser(Request $request)
{
$id = $request->id;
User::where(['id'=>$id])
->delete();
echo "User deleted successfully";
}
}
11 changes: 11 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

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

class Project extends Model
{
use HasFactory;
}
11 changes: 11 additions & 0 deletions app/Models/Task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

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

class Task extends Model
{
use HasFactory;
}
Loading