Skip to content

models&rest apis from vaishu-Feature/rest api #41

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

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Auth;
use App\Models\User;
use App\Models\Project;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
//$projects = Project::get();
$q = $request->q; // search keyword, will search for name
$pageIndex = $request->pageIndex ? $request->pageIndex : 0; // the index of the page to shown, default 0
$pageSize = $request->pageSize ? $request->pageSize : 3; // how many items to return, default 3
$sortBy = $request->sortBy ? $request->sortBy : 'name' ; // attribute to sort, default name
$sortDirection = $request->sortDirection ? $request->sortDirection : 'ASC'; // direction of the sort, default ASC
$query =Project::query();
// Search by name
if (isset($q)) {
$query->where('name', 'LIKE', '%'.$q.'%');
}
// search by sort by and direction
$query->orderBy($sortBy, $sortDirection);
$projects = $query->paginate($pageSize);
return response()->json(['status' => 'success','projects'=>$projects]);
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$validator = Validator::make($request->all(), [
'name' => 'required|unique:projects,name|min:5|max:10',
]);

if ($validator->fails()) {
return response()->json(['errors' => $validator->messages()], 422);
}
if ($validator->passes()) {
$create = Project::create([
'name' => $request['name']
]);
return response()->json(['status' => 'Project created successfully'], 201);
}
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$project = Project::find($id);
return response()->json(['status' => 'success','project'=>$project]);
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{

}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$validator = Validator::make($request->all(), [
'name' => 'required|string|min:5|max:50'
]);

if ($validator->fails()) {
return response()->json(['errors' => $validator->messages()], 422);
}
if ($validator->passes()) {
$updateProject = Project::where('id',$id)->firstOrFail();
$updateProject->update([
'name' => $request['name']
]);
return response()->json(['status' => 'Project updated successfully'], 200);
}
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$project = Project::find($id);
$project->delete();
return response()->json(['status' => 'Project deleted successfully'], 200);
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}
}
195 changes: 195 additions & 0 deletions app/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Auth;
use App\Models\User;
use App\Models\Project;
use App\Models\Task;
class TaskController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$tasks = Task::get();
return response()->json(['status' => 'success','tasks'=>$tasks]);
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$validator = Validator::make($request->all(), [
'title' => 'required|unique:tasks,title|min:5|max:10',
'description' => 'required|string|min:5|max:50',
'project_id' => 'required'
]);

if ($validator->fails()) {
return response()->json(['errors' => $validator->messages()], 422);
}
if ($validator->passes()) {
$create = Task::create([
'title' => $request['title'],
'description' => $request['description'],
'project_id' => $request['project_id'],
]);
return response()->json(['status' => 'Task created successfully'], 201);
}
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$task = Task::find($id);
return response()->json(['status' => 'success','task'=>$task]);
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$validator = Validator::make($request->all(), [
'title' => 'required|min:5|max:10',
'description' => 'required|string|min:5|max:50',
'project_id' => 'required'
]);

if ($validator->fails()) {
return response()->json(['errors' => $validator->messages()], 422);
}
if ($validator->passes()) {
$updateTask = Task::where('id',$id)->firstOrFail();
$updateTask->update([
'title' => $request['title'],
'description' => $request['description'],
'project_id' => $request['project_id'],
]);
return response()->json(['status' => 'Task updated successfully'], 200);
}
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$task = Task::find($id);
$task->delete();
return response()->json(['status' => 'Task deleted successfully'], 200);
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}
public function getTeams(){
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$users = User::where('user_type',User::ROLE['TEAM_MEMBER'])->get();
return response()->json(['status' => 'success','users'=>$users]);
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

public function assignTask(Request $request){
if(Auth::user()->user_type == User::ROLE['PRODUCT_OWNER']){
$validator = Validator::make($request->all(), [
'team_user_id' => 'required',
'task_id' => 'required'
]);

if ($validator->fails()) {
return response()->json(['errors' => $validator->messages()], 422);
}
if ($validator->passes()) {
$updateTask = Task::find($request['task_id']);
$updateTask->user_id = $request['team_user_id'];
$updateTask->save();
return response()->json(['status' => 'Task assigned successfully'], 200);
}
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}

public function updateStatus(Request $request){
if(Auth::user()->user_type == User::ROLE['TEAM_MEMBER']){
$validator = Validator::make($request->all(), [
'status' => 'required',
'task_id' => 'required'
]);

if ($validator->fails()) {
return response()->json(['errors' => $validator->messages()], 422);
}
if ($validator->passes()) {
$updateTask = Task::find($request['task_id']);
$updateTask->status = $request['status'];
$updateTask->save();
return response()->json(['status' => 'Task status updated successfully'], 200);
}
}else{
return response()->json(['errors' => 'You does not have an access'], 401);
}
}
}
Loading