Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit

Permalink
feat: Add /api/search/stats route
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Apr 27, 2020
1 parent 367e656 commit e5e2749
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/Http/Controllers/ApiDataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Http\Controllers;

use App\Http\Requests\SearchRequest;
use App\Models\Commit;
use App\Models\IssueComment;
use App\Models\PullRequest;
use Illuminate\Support\Facades\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Carbon\Carbon;

Expand Down Expand Up @@ -48,6 +50,21 @@ public function getSearchIndex(Request $request)
]);
}

/**
* Get API stats data as a JSON string
* @return JsonResponse
*/
public function getCommitStatsBySearchQueryAndDate(SearchRequest $request): JsonResponse
{
$searchTermSql = '%' . $request->getSearchTerm() . '%';
$searchDate = $request->getSearchDate();
return response()->json([
'commitsPoints' => $this->getCommitsCountByHourForTermAndDate($searchTermSql, $searchDate),
'pullsPoints' => $this->getPullCountByHourForTermAndDate($searchTermSql, $searchDate),
'commentsPoints' => $this->getIssueCommentCountByHourForTermAndDate($searchTermSql, $searchDate),
]);
}

private function getSuggestedDate(): string
{
$firstCommit = Commit::orderBy('created_at', 'DESC')->first();
Expand Down
50 changes: 50 additions & 0 deletions app/Http/Requests/SearchRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Carbon\Carbon;

final class SearchRequest extends FormRequest
{

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}

public function getSearchTerm(): string
{
return $this->query->get('searchTerm', '');
}

public function getSearchDate(): Carbon
{
return Carbon::createFromFormat('!Y-m-d', $this->query->get('searchDate'));
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string,array>
*/
public function rules(): array
{
return [
'searchTerm' => [
'string',
'sometimes',
],
'searchDate' => [
'string',
'date:Y-m-d',
'required',
]
];
}
}
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<?php

use App\Http\Controllers\ApiDataController;
use Illuminate\Support\Facades\Route;

Route::get('/search/stats', [ApiDataController::class, 'getCommitStatsBySearchQueryAndDate']);

0 comments on commit e5e2749

Please sign in to comment.