Skip to content

Commit

Permalink
Implemented adding reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
mspeake161 committed Feb 18, 2025
1 parent 6e50d7f commit b3e3c6b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 23 deletions.
29 changes: 27 additions & 2 deletions app/Http/Controllers/ReviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Http\Controllers;

use App\Models\Product;
use App\Models\Review;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

// manages the review system
class ReviewController extends Controller
Expand All @@ -14,14 +16,37 @@ function show($productId) {
$product = Product::all()->where('id', $productId)->first();
// TODO: find out if they have bought the product
$purchased = true;
// return page if purchased
// return page if purchased and product exists
if ($purchased && $product) {
return view('pages.review', ['product' => $product]);
} else {
return redirect()->back()->with('error', 'You must purchase the product to review it');
}
}
function add(Request $request) {

// validate request
$request->validate([
'rating' => 'required|integer|between:1,5',
'message' => 'required|string',
'product_id' => 'required|integer',
'headline' => 'required|string',
]);
// check if logged in
if (!Auth::check()) {
return redirect()->back()->with('error', 'You must be logged in to review a product');
}
$user = Auth::user();
// create review
$review = new Review([
'user_id' => $user['id'],
'product_id' => $request['product_id'],
'rating' => $request['rating'],
'review' => $request['message'],
'title' => $request['headline']
]);
// save it
$review->save();
// return to product page with message
return redirect()->route('product.show', ['id' => $request['product_id']])->with('message', 'Review added successfully');
}
}
3 changes: 2 additions & 1 deletion app/Models/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Review extends Model
'user_id',
'product_id',
'rating',
'review'
'review',
'title'
];

// returns user for given review
Expand Down
27 changes: 27 additions & 0 deletions database/migrations/2025_02_18_175420_update_reviews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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
{
// Add the review title column
Schema::table('reviews', function ($table) {
$table->string('title');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
// will be caught by creation migration
}
};
38 changes: 21 additions & 17 deletions public/js/rating.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
const ratingContainer = document.querySelector('.rating');
const stars = ratingContainer.querySelectorAll('.star');
document.addEventListener('DOMContentLoaded', () => {
const ratingContainer = document.querySelector('.rating');
const stars = ratingContainer.querySelectorAll('.star');
const ratingHolder = document.querySelector('#rating-holder');
stars.forEach(star => {
star.addEventListener('click', () => {
const rating = parseInt(star.dataset.value, 10);
ratingContainer.dataset.rating = rating;

stars.forEach(star => {
star.addEventListener('click', () => {
const rating = parseInt(star.dataset.value, 10);
ratingContainer.dataset.rating = rating;
// Update star appearance
stars.forEach(s => {
if (s.dataset.value <= rating) {
s.classList.add('active');
} else {
s.classList.remove('active');
}
});

// Update star appearance
stars.forEach(s => {
if (s.dataset.value <= rating) {
s.classList.add('active');
} else {
s.classList.remove('active');
}
});
// You can now submit the rating to your server or perform other actions
console.log("Rating submitted:", rating);

// You can now submit the rating to your server or perform other actions
console.log("Rating submitted:", rating);
// Update the rating holder
ratingHolder.value = rating;
});
});
});

9 changes: 6 additions & 3 deletions resources/views/pages/review.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<div class="review">
<h1>Create Review</h1>
<!-- Need a form submission -->
<form method="POST" action="/review">
<form method="POST" action="{{ route('review.add') }}">
@csrf
<!-- Image + Name of Product -->
<img src="{{asset($product->getMainImage())}}" alt="Product Image">
<p>{{$product->name}}</p>
Expand All @@ -22,7 +23,7 @@
<div class="line-break"><br></div>
<h2>Add a headline</h2>
<!-- Input for headline -->
<input type="text" placeholder="What's most important to know?"></input>
<input type="text" placeholder="What's most important to know?" name="headline"></input>
<div class="line-break"><br></div>
<h2>Add a written review</h2>
<!-- Input for review -->
Expand All @@ -31,8 +32,10 @@
style="resize: none"></textarea>
<div class="line-break"><br></div>
<p>We will notify you via email as soon as your review is processed.</p>
<input type="hidden" name="product_id" value="{{$product->id}}">
<input type="hidden" id="rating-holder" name="rating" value="0">
<!-- Submit button -->
<button>Submit</button>
<button type="submit">Submit</button>
</form>
</div>
@endsection
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,5 @@

// Show the review page
Route::get('/review/{productId}', [ReviewController::class, 'show'])->name('review.show');

Route::post('/review/add', [ReviewController::class, 'add'])->name('review.add');

0 comments on commit b3e3c6b

Please sign in to comment.