Created
January 18, 2019 23:22
-
-
Save Jhhames/7a3ecf8803d8de1be63dbc182edc40d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Middleware; | |
use Closure; | |
use App\Reward; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\Session; | |
class RewardMiddleware | |
{ | |
/** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
$userId = Auth::user()->id; | |
$reward = Reward::where('user_id',$userId); | |
if($reward->exists()){ | |
$reward = $reward->first(); | |
$reward_id = $reward->id; | |
$findReward = Reward::find($reward_id)->return(); | |
if($findReward){ | |
$reward->lastReward = Time(); | |
$reward->increment('points',50); | |
Session::flash('rewardAvailable','You\'ve earned 50pricks on your daily reward '); | |
$reward->save(); | |
} | |
}else{ | |
$reward = new Reward; | |
$reward->user_id = Auth::user()->id; | |
$reward->lastReward = Time(); | |
$reward->save(); | |
} | |
return $next($request); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Facades\Schema; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class CreateRewardsTable extends Migration | |
{ | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('rewards', function (Blueprint $table) { | |
$table->increments('id'); | |
$table->string('user_id'); | |
$table->string('lastReward'); | |
$table->integer('points')->default(0); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::dropIfExists('rewards'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Support\Facades\Session; | |
class Reward extends Model | |
{ | |
// | |
public function user(){ | |
$this->belongsTo('App\User'); | |
} | |
public function return(){ | |
$now = Time(); | |
$getTime = $this->getTimeLeft($this->lastReward, $now); | |
if($getTime->min >= 5){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
public function getTimeLeft($start, $stop){ | |
$timeStampLeft = $stop - $start; | |
$wk = $day = $min = $sec = $hr = 0; | |
if($timeStampLeft >= 60){ | |
$min = floor($timeStampLeft / 60); | |
$sec = $timeStampLeft % 60; | |
if($min >= 60 ){ | |
$hr = floor($min / 60); | |
$min = $min % 60; | |
if($hr >= 24 ){ | |
$day = floor($hr / 24); | |
$hr = $hr % 24; | |
if($day >= 7){ | |
$wk = floor($day / 7); | |
$day = $day % 7; | |
} | |
} | |
} | |
}else{ | |
$sec = $timeStampLeft; | |
} | |
$timeLeft = (object)[ | |
'week' => (int)$wk, | |
'day' => (int)$day, | |
'hour' =>(int)$hr, | |
'min' => $min, | |
'sec' => $sec, | |
'stamp' => $timeStampLeft | |
]; | |
return $timeLeft; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::group(['middleware'=>['reward','auth']], function(){ | |
Route::get('/james', function(){ | |
$userId = Auth::user()->id; | |
$reward = App\Reward::where('user_id',$userId)->first(); | |
return view('new.test')->with(['reward'=>$reward]); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<section> | |
<div class="container"> | |
<div class="row"> | |
<ul class="list-group"> | |
<div class="list-group-item"> | |
<strong> | |
Rewards Earned : | |
</strong> | |
<span class="text-success"> | |
{{ $reward->points }} points | |
</span> | |
</div> | |
<div class="list-group-item"> | |
<strong> | |
Time to next reward : | |
</strong> | |
<span> | |
@php | |
$time = Time() - $reward->lastReward | |
@endphp | |
{{ gmdate("i",$time) }} | |
</span> | |
</div> | |
</ul> | |
</div> | |
</div> | |
</section> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<!-- CSRF Token --> | |
<meta name="csrf-token" content="{{ csrf_token() }}"> | |
<link rel="shortcut icon" href="/img/coffee.png" type="image/x-icon"> | |
<title>{{ config('app.name', 'Laravel') }}</title> | |
<!-- Scripts --> | |
<script src="{{ asset('js/app.js') }}" defer></script> | |
<!-- Fonts --> | |
<link rel="dns-prefetch" href="//fonts.gstatic.com"> | |
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css"> | |
<!-- Styles --> | |
<link href="{{ asset('css/app.css') }}" rel="stylesheet"> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css"> | |
<link rel="stylesheet" href="{{ asset('css/font-awesome.min.css') }}"> | |
<style> | |
a{ | |
text-decoration: none; | |
color: white; | |
} | |
#last a { | |
color: #3490dc !important; | |
} | |
a:hover{ | |
text-decoration: none; | |
} | |
</style> | |
</head> | |
<body style="" class="text"> | |
<div id="app"> | |
<nav class="navbar navbar-expand-md navbar-light navbar-laravel" style="background-color:#E1AD48"> | |
<div class="container"> | |
<a class="navbar-brand" href="{{ url('/') }}"> | |
{{ config('app.name', 'Laravel') }} | |
</a> | |
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}"> | |
<span class="navbar-toggler-icon"></span> | |
</button> | |
<div class="collapse navbar-collapse" id="navbarSupportedContent"> | |
<!-- Left Side Of Navbar --> | |
<ul class="navbar-nav mr-auto"> | |
</ul> | |
<!-- Right Side Of Navbar --> | |
<ul class="navbar-nav ml-auto"> | |
<!-- Authentication Links --> | |
@guest | |
<li class="nav-item"> | |
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a> | |
</li> | |
{{-- @if (Route::has('register')) | |
<li class="nav-item"> | |
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a> | |
</li> | |
@endif --}} | |
@else | |
<li class="nav-item dropdown"> | |
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre> | |
{{ Auth::user()->name }} <span class="caret"></span> | |
</a> | |
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown"> | |
<a class="dropdown-item" href="/home" data-toggle="modal"> | |
<span class="fa fa-home"></span> Admin Home | |
</a> | |
<a class="dropdown-item" href="/admin/orders" data-toggle="modal"> | |
<span class="fa fa-shopping-cart"></span> Orders | |
</a> | |
<a class="dropdown-item" href="/admin/contacts" data-toggle="modal"> | |
<span class="fa fa-envelope-o"></span> Contact Messages | |
</a> | |
<a class="dropdown-item" href="#changePassword" data-toggle="modal"> | |
<span class="fa fa-key"></span> Change Password | |
</a> | |
<div class="dropdown-divider"></div> | |
<a class="dropdown-item" href="{{ route('logout') }}" | |
onclick="event.preventDefault(); | |
document.getElementById('logout-form').submit();"> | |
<span class="fa fa-sign-out"></span> {{ __('Logout') }} | |
</a> | |
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;"> | |
@csrf | |
</form> | |
</div> | |
</li> | |
@endguest | |
</ul> | |
</div> | |
</div> | |
</nav> | |
<main class="py-4"> | |
@yield('content') | |
</main> | |
{{-- <script src="js/app.js"></script> --}} | |
<script src="/js/jquery-3.3.1.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script> | |
<script> | |
toastr.options.closeButton = true; | |
@if(Session::has('rewardAvailable')) | |
toastr.success("{{ Session::get('rewardAvailable') }}") | |
@endif | |
</script> | |
@php | |
Session::forget('rewardAvailable'); | |
@endphp | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment