Skip to content
Draft
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
42 changes: 35 additions & 7 deletions php/laravel/api/app/Http/Controllers/MessageApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,50 @@

class MessageApiController extends Controller
{
public function index()
public function index(Request $request)
{
// SPAの場合でも考え方は同じ
// 処理は、 MessageController を参照
$tenantid = $request->userinfo['tenants'][0]['id'];

$messages = DB::table('messages')
->select('messages.*')
->where('tenant_id', "1")
->where('tenant_id', $tenantid)
->get();
return response()->json($messages);
}

public function post(Request $request)
{
$result = Message::create([
'tenant_id' => "1",
'user_id' => $request->user_id,
'message' => $request->message,
]);
$tenant_id = $request->userinfo['tenants'][0]['id'];
$plan_id = $request->userinfo['tenants'][0]['plan_id'];

// SaaSus SDKを使ってSaaSus APIを叩いて、各種情報を取得し、判断に使う
$client = new \AntiPatternInc\Saasus\Api\Client();
$pricingApi = $client->getPricingClient();
$res = $pricingApi->getPricingPlan($plan_id, $pricingApi::FETCH_RESPONSE);
$plan = json_decode($res->getBody(), true);

$meteringUnitName = "comment_count";
$res = $pricingApi->getMeteringUnitDateCountByTenantIdAndUnitNameToday($tenant_id, $meteringUnitName, $pricingApi::FETCH_RESPONSE);
$count = json_decode($res->getBody(), true);

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

$result = '';
// 現在契約中の料金プランの上限コメント数を超えていたら、投稿できなくする
if ($count['count'] < $upper || $upper === 0) {
$result = Message::create([
'tenant_id' => $tenant_id,
'user_id' => $request->userinfo['tenants'][0]['user_attribute']['username'],
'message' => $request->message,
]);
// メータリングAPIで、コメント数に1を足す
$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);
}

return response()->json($result);
}
Expand Down
46 changes: 36 additions & 10 deletions php/laravel/api/app/Http/Controllers/MessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,50 @@ class MessageController extends Controller
];
public const TENANT_NAME = 'テナント1';

public function index()
public function index(Request $request)
{
// 現在はtenant_idは1で固定する
$messages = Message::where('tenant_id', "1")->get();
return view('messageBoard.index', ['messages' => $messages, 'plans' => $this::PLANS, 'tenant_name' => $this::TENANT_NAME]);
// $request->userinfo に各種ユーザ情報、テナント情報が入ってくるので、それを使う
$messages = Message::where('tenant_id', $request->userinfo['tenants'][0]['id'])->get();
return view('messageBoard.index', ['messages' => $messages, 'plans' => $this::PLANS, 'tenant_name' => $request->userinfo['tenants'][0]['name']]);
}

public function post(Request $request)
{
$validated = $request->validate([
'message' => 'required|max:255'
]);
// 現在はtenant_idは1で固定する
$message = Message::create([
'tenant_id' => "1",
'user_id' => Auth::id(),
'message' => $request->message,
]);

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

// SaaSus SDKを使ってSaaSus APIを叩いて、各種情報を取得し、判断に使う
$client = new \AntiPatternInc\Saasus\Api\Client();
$pricingApi = $client->getPricingClient();
$res = $pricingApi->getPricingPlan($plan_id, $pricingApi::FETCH_RESPONSE);
$plan = json_decode($res->getBody(), true);

// メータリングのメータ、comment_count(コメント数)を使う
$meteringUnitName = "comment_count";
$res = $pricingApi->getMeteringUnitDateCountByTenantIdAndUnitNameToday($tenant_id, $meteringUnitName, $pricingApi::FETCH_RESPONSE);
// 今回は、1日ごとの上限コメント数として扱う
$count = json_decode($res->getBody(), true);

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

// 現在契約中の料金プランの上限コメント数を超えていたら、投稿できなくする
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,
]);
// メータリングAPIで、コメント数に1を足す
$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');
}
Expand Down
11 changes: 9 additions & 2 deletions php/laravel/api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.2",
"laravel/sanctum": "^2.14.1",
"laravel/tinker": "^2.7"
"laravel/tinker": "^2.7",
"saasus-platform/saasus-sdk-php": "dev-feature/api-request-error-handling"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
Expand Down Expand Up @@ -58,5 +59,11 @@
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"repositories": {
"saasus-platform/saasus-sdk-php": {
"type": "vcs",
"url": "https://github.com/saasus-platform/saasus-sdk-php"
}
}
}
Loading