Skip to content

Instantly share code, notes, and snippets.

@hamannjames
Created July 19, 2017 20:44
Show Gist options
  • Save hamannjames/ce1a9a32218ef5e37d109e33fb567ba0 to your computer and use it in GitHub Desktop.
Save hamannjames/ce1a9a32218ef5e37d109e33fb567ba0 to your computer and use it in GitHub Desktop.
The start of a laravel api that tracks leads. This is the controller for leads showing get and post requests (functions index and store)
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Lead;
use App\Models\LeadMeta;
use App\Transformers\LeadTransformer;
class ApiLeadsController extends Controller
{
// The transformer reformats the data to uncouple it from the shcema structure
function __construct(LeadTransformer $leadTransformer) {
$this->leadTransformer = $leadTransformer;
}
public function index() {
$leads = Lead::all(); // Retrieve the entire collection of leads, and let the transformer handle pagination
return response()->json([
'data' => $this->leadTransformer->transformCollection($leads->all())
], 200);
}
public function show($id) {
$lead = Lead::find($id);
// Simple response for if a lead is not found. All "code" responses correspond with a readme entry
if (!$lead) {
return response()->json([
'error' => array(
'message' => 'Resource not found',
'code' => '415'
)
], 404);
}
// If there is a lead, pass it to the transformer for de-coupling
return response()->json([
'data' => $this->leadTransformer->transformCollection($lead)
], 200);
}
public function store(Request $request) {
// We only want to retrieve certain objects from the request at different stages. Empty string are automatically set to null by middleware
// Step 1 is to make sure a tracking number has been provided. This ensures we are only storing leads we can track through cookies
$trackingNumber = $request->tracking_number;
if (!trackingNumber) {
return response()->json([
'error' => array(
'message' => 'Tracking number not present or undefined',
'code' => '425',
'Data Sent' => $request->json()
)
], 400);
}
// Step 2 is determining if a lead already exists in the database
$leadID = $request->leadID;
if (!leadID) {
// If there is no lead id in the post request we can still determine if we are working with an existing lead by a tracking number
$lead = Lead::where('tracking_number', $trackingNumber;
if (!$lead) {
// If we are here we know that we have a tracking number that is new, meaning we can create a new lead
$lead = new Lead;
$lead->tracking_number = $trackingNumber;
$lead->status = 0; // All new leads start with a status of 0
// We need to make sure the lead saves. if it doesn't we need to return an error stating there is problem with the model
try{
$lead->save();
}
catch(\Exception $e){
// this is much better than a generic 500 error
return response()->json([
'error' => array(
'message' => $e->getMessage();,
'code' => '530',
)
], 500);
}
// Here we make sure the LeadMeta item is saved in the database, with the newly saved lead id as a foreignkey
$leadMeta = new LeadMeta;
$leadMeta->lead_id = $lead->id;
try{
$leadMeta->save();
}
catch(\Exception $e){
// this is much better than a generic 500 error
$lead->delete(); // we want to delete the lead if there is an error creating it's meta. A lead must have meta
return response()->json([
'error' => array(
'message' => $e->getMessage();,
'code' => '530',
)
], 500);
}
return response()->json([
'success' => array(
'message' => 'Lead saved',
'leadID' => $lead->id
)
], 201);
}
// We end up here if the lead already exists
else {
return response()->json([
'error' => array(
'message' => 'Lead already exists',
'code' => '260'
)
], 400);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment