Commit af981060 by Phạm Văn Đoan

tạo api lấy thông tin top 100 bài hát NctController.php

parent 962718e7
<?php
namespace App\Http\Controllers\Api;
use App\Repositories\TrackRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class NctController extends Controller
{
protected $trackRepository;
public function __construct(TrackRepository $trackRepository)
{
$this->trackRepository = $trackRepository;
}
/**
* API: http://localhost:8000/api/get-nct-top100
*
* @return \Illuminate\Http\JsonResponse
*/
public function getNctTop100(Request $request)
{
$url = config('api.nct.top100');
$url_playlist_top100 = config('api.nct.playlist_top100');
$tracks = $this->trackRepository->processNctGetTrackInfoFromTop100($url);
$tracks_playlist = $this->trackRepository->processNctGetTrackInfoFromTop100Playlist($url_playlist_top100);
$response = null;
foreach ($tracks as $key => $track) {
$link = null;
$duration = 0;
$check = $this->privateGeTrackLink($tracks_playlist, $track['id_nct']);
if ($check) {
$link = $check['link'];
$duration = $check['duration'];
}
$response[] = $track;
$response[$key]['link'] = $link;
$response[$key]['duration'] = $duration;
}
return response()->json([
'code' => 200,
'message' => 'Lấy thông tin top 100 bài hát NCT thành công.',
'data' => $response
]);
}
private function privateGeTrackLink($tracks_playlist, $track_id)
{
if (is_array($tracks_playlist) && count($tracks_playlist) > 0) {
foreach ($tracks_playlist as $key => $val) {
if ($val['id_nct'] == $track_id) {
return $val;
}
}
}
return null;
}
}
......@@ -16,3 +16,5 @@ use Illuminate\Http\Request;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::get('/get-nct-top100', 'Api\NctController@getNctTop100');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment