示例#1
0
 /**
  * Api function for get currency and listing by 7 days data.
  *
  * @param number $currencyId
  * @return boolean|Response
  */
 public function getCurrenciesById($currencyId = 0)
 {
     $data = get();
     $default = array('currency_id' => $currencyId);
     // Validator input params.
     $valids = Validator::make($default, ['currency_id' => 'required|numeric|min:1'], EXRHelper::getMessage());
     // Check fails.
     if ($valids->fails()) {
         return Reponse::json(array('status' => 'error', 'data' => $valids->messages()), 500);
     }
     $cacheId = 'currency_detail_' . $currencyId;
     if (!Cache::has($cacheId)) {
         $records = CurrencyDaily::whereRaw('currency_id = ? AND created_at >= NOW() - INTERVAL ? WEEK', array($currencyId, DLN_LIMIT_WEEK))->orderBy('updated_at', 'ASC')->with('currency')->get();
         $newRecords = array();
         if (count($records)) {
             foreach ($records as $record) {
                 switch ($record->type) {
                     case 'gold':
                         $multiple = 100000;
                         break;
                     default:
                         $multiple = 1;
                         break;
                 }
                 $record->buy = intval($record->buy * $multiple);
                 $record->transfer = intval($record->transfer * $multiple);
                 $record->sell = intval($record->sell * $multiple);
                 $record->min_buy = intval($record->min_buy * $multiple);
                 $record->max_buy = intval($record->max_buy * $multiple);
                 $record->min_sell = intval($record->min_sell * $multiple);
                 $record->max_sell = intval($record->max_sell * $multiple);
                 $record->buy_change = intval($record->buy_change * $multiple);
                 $record->sell_change = intval($record->sell_change * $multiple);
                 $newRecords[] = $record;
             }
         }
         Cache::put($cacheId, json_encode($newRecords), EXR_CACHE_MINUTE);
     } else {
         $records = json_decode(Cache::get($cacheId, null));
     }
     return Response::json(array('status' => 'success', 'data' => $records));
 }