CustomerRewardsRESTAPI/Controller/API/PurchaseController.php

54 lines
1.9 KiB
PHP
Raw Normal View History

2024-04-15 11:38:59 -05:00
<?php
header("Content-Type: application/json");
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
/**
* Description of PurchaseController
*
* @author SCTN4
*/
class PurchaseController extends BaseController{
/**
* "/purchase/list" Endpoint - Get list of purchases
*/
public function listAction()
{
$strErrorDesc = '';
$requestMethod = $_SERVER["REQUEST_METHOD"];
$arrQueryStringParams = $this->getQueryStringParams();
if (strtoupper($requestMethod) == 'GET') {
try {
$purchaseModel = new PurchaseModel();
$intLimit = 10;
$dtDate = date("M/d/yy");
if (isset($arrQueryStringParams['limit']) && $arrQueryStringParams['limit']) {
$intLimit = $arrQueryStringParams['limit'];
$dtDate = $arrQueryStringParams['date'];
}
$arrPurchases = $purchaseModel->getPurchases($dtDate, $intLimit);
$responseData = json_encode($arrPurchases);
} catch (Error $e) {
$strErrorDesc = $e->getMessage().'Something went wrong! Please contact support.';
$strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
}
} else {
$strErrorDesc = 'Method not supported';
$strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity';
}
// send output
if (!$strErrorDesc) {
$this->sendOutput(
$responseData,
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
);
} else {
$this->sendOutput(json_encode(array('error' => $strErrorDesc)),
array('Content-Type: application/json', $strErrorHeader)
);
}
}
}