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

Added delete route #2

Open
wants to merge 18 commits into
base: master
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Homestead.yaml
npm-debug.log
yarn-error.log
.env
.env.testing
18 changes: 18 additions & 0 deletions app/AuthorizationLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AuthorizationLevel extends Model
{
protected $table = "authorization_levels";

/**
* Every authorization level has many users assigned to it
* @return [collection] App::User
*/
public function users(){
return $this->hasMany('App\Users');
}
}
155 changes: 155 additions & 0 deletions app/Complaint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;

use Exception;

class Complaint extends Model
{
protected $table = 'complaints';

/**
* All Complaints are assigned a status
* @return App::ComplaintStatus
*/
public function complaintStatus(){
return $this->belongsTo('App\ComplaintStatus','status_id');
}

/**
* Each Complaints are made by a User
* @return App::User
*/
public function user(){
return $this->belongsTo('App\User');
}

/**
* By using the params - userID, startDate and endDate, the complaints are retieved by the
* available combinations of parameters of startDate & endDate.
* @param userID
* @param startDate
* @param endDate
* @return [array] complaints
*/
static public function getUserComplaints($startDate, $endDate, $hostel = NULL){

$userID = User::getUserID();
if(! $userID)
throw new Exception("user not logged in", 1);

$complaints = Complaint::select('id','title','description','image_url','created_at')
->where('user_id',$userID)
->get();
if(isset($startDate))
$complaints = $complaints->where('created_at','>=',$startDate);
if(isset($endDate))
$complaints = $complaints->where('created_at','<=',$endDate);

return $complaints->values()->all();
}


/**
* This is for the admin GET route.
* By using the complaint->user, complaint->status, user->hostel relationships,
* the data for the admin's complaint-feed is retrieved.
* @param startDate
* @param endDate
* @param hostel
* @param status
* @return [array] response
*/
static public function getAllComplaints($startDate, $endDate, $hostel, $status){

$userID = User::getUserID();
if(! $userID)
throw new Exception("user not logged in", 1);

if(! User::isUserAdmin())
throw new Exception("user not admin", 2);

$complaints = Complaint::select('id','user_id','title','description',
'status_id','image_url','created_at')
->get();

foreach ($complaints as $complaint) {
$complaint->status = $complaint->complaintStatus()->select('name','message')->first();
$complaint->user = $complaint->user()->select('username','name','room_no','hostel_id',
'phone_contact','whatsapp_contact','email')
->first();

$complaint->user->hostel = $complaint->user->hostel()->value('name');
}

if(isset($startDate))
$complaints = $complaints->where('created_at','>=',$startDate);
if(isset($endDate))
$complaints = $complaints->where('created_at','<=',$endDate);
if(isset($status))
$complaints = $complaints->filter(function($complaint) use($status){
return $complaint->status->name == $status;
});
if(isset($hostel))
$complaints = $complaints->filter(function($complaint) use($hostel){
return $complaint->user->hostel == $hostel;
});

return $complaints->values()->all();
}

/*
* This is for the user DELETE route.
* By using the complaint id,
* the instance of the table with given id is deleted
* @param id
* @param userID
* @return 1 for sucessfully created and 0 if not
*/

static public function deleteComplaint($id){

$userID = User::getUserID();
$isUserAdmin = User::isUserAdmin();


if(! $userID)
throw new Exception("user not logged in", 1);

if(! User::isUserAdmin())
throw new Exception("user not admin", 2);

if (! $id)
throw new Exception("complaint doesn't exist",3);

Complaint::findOrFail($id)->delete();
ComplainComment::findOrFail($id)->delete();
ComplaintReply::findOrFail($id)->delete();
ComplaintStatus::findOrFail($id)->delete();
}
}






















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

namespace App;

use Illuminate\Database\Eloquent\Model;

class ComplaintComment extends Model
{
protected $table = "complaints_comments";

/**
* Each Comment Comment is a part of a Complaint
* @return App::Complaint
*/
public function complaint(){
return $this->belongsTo('App\Complaint');
}

/**
* Every Complaint Comment is made by a User
* @return App::User
*/
public function user(){
return $this->belongsTo('App\User');
}
}
26 changes: 26 additions & 0 deletions app/ComplaintReply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ComplaintReply extends Model
{
protected $table = "complaints_replies";

/**
* Each Comment Reply must belong to a comment thread
* @return App::ComplaintComment
*/
public function comment(){
return $this->belongsTo('App\ComplaintComment');
}

/**
* Each Comment Reply is made by a user
* @return App::User
*/
public function user(){
return $this->belongsTo('App\User');
}
}
18 changes: 18 additions & 0 deletions app/ComplaintStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ComplaintStatus extends Model
{
protected $table = "complaints_status";

/**
* There are multiple Complaints in every Complaint Status
* @return [collection] App::Complaint
*/
public function complaints(){
return $this->hasMany('App\Complaint');
}
}
18 changes: 18 additions & 0 deletions app/Hostel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Hostel extends Model
{
protected $table = "hostels";

/**
* Every Hostel has multiple users in it
* @return [collection] App::User
*/
public function users(){
return $this->hasMany('App\User');
}
}
101 changes: 101 additions & 0 deletions app/Http/Controllers/ComplaintController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Complaint;
use App\User;
use App\Hostel;
use App\AuthorizationLevel;
use App\ComplaintComment;
use App\ComplaintReply;
use App\ComplaintStatus;

use Exception;

class DeleteController extends Controller
{
/**
* By using the ID of complaint given by user, the function deletes the * complaint, complaintComment and complaintStatus from the Complaint,
* ComplaintComment and ComplaintStatus tables respectively
* @param $id
* @return previous state, updated after deletion
*/

public function deleteComplaint(Request $request){

try{
$response = Complaint::deleteComplaint($userID, $request['id']);
return response()->json([
"message" => "complaint deleted",
"data" => $response,
], 200);
} catch (Exception $e) {
if ($e->getCode() == 1)
return response()->json([
"message" => $e->getMessage(),
], 401);
if ($e->getCode() == 2)
return response()->json([
"message" => $e->getMessage(),
], 403);

if ($e->getCode() == 3)
return response()->json([
"message" => $e->getMessage(),
], 404);
}
}
}

class ComplaintController extends Controller
{
/**
* By using the User ID from the session, this function fetches the complaints made
* by the user in accordance with request parameters
* @param Request $request - start_date, end_date
* @return [collection of complaints]
*/
public function getUserComplaints(Request $request) {
try {
$response = Complaint::getUserComplaints($request['start_date'], $request['end_date']);
return response()->json([
"message" => "complaints available",
"data" => $response,
], 200);
}
catch (Exception $e){
if($e->getCode() == 1)
return response()->json([
"message" => $e->getMessage(),
], 401);
}
}

/**
* By using the session data, the user is checked for logged in and admin.
* If both are true, then all the complaints are retrieved for the admin for the
* sake of populating the admin feed.
* @param Request $request
* @return [json]
*/
public function getAllComplaints(Request $request) {
try {
$response = Complaint::getAllComplaints($request['start_date'], $request['end_date'], $request['hostel'], $request['status']);
return response()->json([
"message" => "complaints available",
"data" => $response,
], 200);
} catch (Exception $e) {
if ($e->getCode() == 1)
return response()->json([
"message" => $e->getMessage(),
], 401);
if ($e->getCode() == 2)
return response()->json([
"message" => $e->getMessage(),
], 403);
}
}

}
Loading