Skip to content

Commit

Permalink
added rate system
Browse files Browse the repository at this point in the history
  • Loading branch information
A1Gard committed Sep 23, 2024
1 parent 44b499d commit c49bfe7
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 8 deletions.
57 changes: 53 additions & 4 deletions app/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Models\Post;
use App\Models\Product;
use App\Models\Quantity;
use App\Models\Rate;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -520,19 +521,19 @@ public function sendSms(Request $request)
$customer = Customer::where('mobile', $request->input('tel'));
$code = rand(11111, 99999);

if (config('app.sms.driver') == 'Kavenegar'){
if (config('app.sms.driver') == 'Kavenegar') {
$args = [
'receptor' => $request->input('tel'),
'template' => trim(getSetting('sign')),
'token' => $code
];
}else{
} else {
$args = [
'code' => $code,
];
}

sendingSMS(getSetting('sign'),$request->input('tel'),$args);
sendingSMS(getSetting('sign'), $request->input('tel'), $args);

Log::info('auth code: ' . $code);
if ($customer->count() == 0) {
Expand Down Expand Up @@ -619,7 +620,7 @@ public function lang(Request $request)
break;
}
}
if (count( explode('@', $r)) == 1){
if (count(explode('@', $r)) == 1) {
return abort(404);
}
$method = explode('@', $r)[1];
Expand Down Expand Up @@ -678,4 +679,52 @@ public function pay($hash)
return redirect()->back()->withErrors($message);
}
}

public function rate(Request $request)
{
$request->validate([
'rate.*' => ['required', 'integer'],
'rateable_id' => ['required', 'integer'],
'rateable_type' => ['required', 'string'],
]);

// return $request->all();

$changed = false;
foreach ($request->rate as $k => $rt) {


$r = Rate::where('rateable_type', $request->rateable_type)
->where('rateable_id', $request->rateable_id)
->where('rater_type', Customer::class)
->where('rater_id', auth('customer')->id())
->where('evaluation_id', $k);
if ($r->count() != 0) {
$rate = $r->first();
$changed = true;
} else {
$rate = new Rate();
}
if ($rt != 0) {
$rate->rater_type = Customer::class;
$rate->rater_id = auth('customer')->id();
$rate->rateable_type = $request->rateable_type;
$rate->rateable_id = $request->rateable_id;
$rate->evaluation_id = $k;
$rate->rate = $rt;
$rate->save();
}

}
if ($changed) {
return [
'OK' => true,
'message' => __('Your rate updated'),
];
}
return [
'OK' => true,
'message' => __('Your rate registered'),
];
}
}
11 changes: 11 additions & 0 deletions app/Models/Rate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Rate extends Model
{
use HasFactory;
}
35 changes: 35 additions & 0 deletions database/migrations/2024_09_23_080515_create_rates_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('rates', function (Blueprint $table) {
$table->id();
$table->morphs('rateable');
$table->morphs('rater');
$table->tinyInteger('rate');
$table->unsignedBigInteger('evaluation_id');
$table->timestamps();


$table->foreign('evaluation_id')
->references('id')->on('evaluations');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('rates');
}
};
24 changes: 24 additions & 0 deletions resources/js/client-custom/customerActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,28 @@ window.addEventListener('load', function () {
}
});
});

document.querySelector('#rating-form')?.addEventListener('submit', function (e) {
e.preventDefault();

// Create a new FormData object from the form
const formData = new FormData(this);

// Make the Axios call with the FormData object
axios.post(this.getAttribute('data-url'), formData)
.then(response => {
if (response.data.OK){

$toast.success(response.data.message);
}else {
$toast.error(response.data.error);
}
// Handle success (e.g., show a success message)
})
.catch(error => {
$toast.error(error.message);
// Handle error (e.g., show an error message)
});
});

});
1 change: 1 addition & 0 deletions resources/js/client-custom/safeForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
window.addEventListener('load',function () {

setTimeout(()=>{
document.querySelectorAll('.safe-form')?.forEach(function (el) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<section id='ProductKaren' class="content">

<div class="{{gfx()['container']}}">
@include('components.err')
<div class="row">
<div class="col-lg-5">
<div id="preview">
Expand Down Expand Up @@ -142,6 +143,11 @@ class="btn btn-outline-primary add-to-card btn-lg">
<div class="navtab" data-target="info">
{{__("Information")}}
</div>
@if(auth('customer')->check())
<div class="navtab" data-target="rate">
{{__("Rate")}}
</div>
@endif
<div class="underline"></div>
</div>
<div id="desc" class="tab-content active">
Expand All @@ -150,6 +156,20 @@ class="btn btn-outline-primary add-to-card btn-lg">
<div id="table" class="tab-content">
{!! $product->table !!}
</div>
<div id="rate" class="tab-content">
<form id="rating-form" method="post" data-url="{{route('client.rate')}}">
@csrf
<input type="hidden" name="rateable_id" value="{{$product->id}}">
<input type="hidden" name="rateable_type" value="{{\App\Models\Product::class}}">
@foreach($product->evaluations() as $e)
<rate-input xtitle="{{$e->title}}" xname="rate[{{ $e->id }}]"></rate-input>
<hr>
@endforeach
<button class="btn btn-primary w-100">
<i class="ri-send-plane-line"></i>
</button>
</form>
</div>
<div id="info" class="tab-content">
<table class="table table-striped table-bordered table-striped">
<tr>
Expand Down
7 changes: 3 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ function () {
Route::get('/customer/send/auth-code', [ClientController::class, 'sendSms'])->name('send-sms');
Route::get('/customer/check/auth-code', [ClientController::class, 'checkAuth'])->name('check-auth');
Route::get('/customer/profile', [ClientController::class, 'profile'])->name('profile');
Route::post('/customer/rate', [ClientController::class, 'rate'])->name('rate');
Route::get('/compare', [ClientController::class, 'compare'])->name('compare');
Route::get('/contact-us', [ClientController::class, 'contact'])->name('contact');
Route::post('/contact-us/submit', [ClientController::class, 'sendContact'])->name('send-contact');
Expand Down Expand Up @@ -457,10 +458,8 @@ function () {
})->name('login.as');

Route::get('test', function () {
// return \Resources\Views\Segments\PreloaderCircle::onAdd();
// return $product->getAllMeta();
// return \App\Models\Quantity::whereId(1)->first()->meta;
return [json_decode(\Cookie::get('card')), json_decode(\Cookie::get('q'))];
$p = \App\Models\Product::first();
return $p->evaluations();
})->name('test');


Expand Down

0 comments on commit c49bfe7

Please sign in to comment.