Skip to main content

Laravel

Laravel implementation

Modify the post method in api/app/Http/Controllers/MessageController.php.

   public function post(Request $request)
{
$validated = $request->validate([
'message' => 'required|max:255'
]);

$tenant_id = $request->userinfo['tenants'][0]['id'];
$plan_id = $request->userinfo['tenants'][0]['plan_id'];

// Use the SaaSus SDK to hit the SaaSus API, acquire various information, and use it for judgment
$client = new \AntiPatternInc\Saasus\Api\Client();
$pricingApi = $client->getPricingClient();
$res = $pricingApi->getPricingPlan($plan_id, $pricingApi::FETCH_RESPONSE);
$plan = json_decode($res->getBody(), true);

// use metering meter, comment_count
$meteringUnitName = "comment_count";
$res = $pricingApi->getMeteringUnitDateCountByTenantIdAndUnitNameToday($tenant_id, $meteringUnitName, $pricingApi::FETCH_RESPONSE);
// This time, treat it as the maximum number of comments per day
$count = json_decode($res->getBody(), true);

$upper = \AntiPatternInc\Saasus\Api\Lib::findUpperCountByMeteringUnitName($plan, $meteringUnitName);

// Disable posting if the number of comments exceeds the maximum number of comments for the current contracted price plan
if ($count['count'] < $upper || $upper === 0) {
$message = Message::create([
'tenant_id' => $tenant_id,
'user_id' => $request->userinfo['tenants'][0]['user_attribute']['username'],
'message' => $request->message,
]);
// add 1 to the number of comments in the metering API
$param = new \AntiPatternInc\Saasus\Sdk\Pricing\Model\UpdateMeteringUnitTimestampCountNowParam();
$param->setMethod('add');
$param->setCount(1);
$res = $pricingApi->updateMeteringUnitTimestampCountNow($request->userinfo['tenants'][0]['id'], $meteringUnitName, $param, $pricingApi::FETCH_RESPONSE);
}

$request->session()->regenerateToken();
return redirect()->route('board');
}