Skip to content

Avoid reviewing non purchased product(s) #1114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SimplCommerce.Infrastructure.Data;
using SimplCommerce.Module.Core.Extensions;
using SimplCommerce.Module.Orders.Models;
using SimplCommerce.Module.Reviews.Areas.Reviews.ViewModels;
using SimplCommerce.Module.Reviews.Data;
using SimplCommerce.Module.Reviews.Models;
Expand All @@ -16,11 +18,16 @@ public class ReviewController : Controller
private const int DefaultPageSize = 25;

private readonly IReviewRepository _reviewRepository;
private readonly IRepository<Order> _orderRepository;
private readonly IWorkContext _workContext;

public ReviewController(IReviewRepository reviewRepository, IWorkContext workContext)
public ReviewController(
IReviewRepository reviewRepository,
IRepository<Order> orderRepository,
IWorkContext workContext)
{
_reviewRepository = reviewRepository;
_orderRepository = orderRepository;
_workContext = workContext;
}

Expand All @@ -31,6 +38,14 @@ public async Task<IActionResult> AddReview(ReviewForm model)
{
var user = await _workContext.GetCurrentUser();
model.ReviewerName = user.FullName; // Otherwise ReviewerName is null

if (!await _orderRepository.Query().AnyAsync(o => o.CustomerId == user.Id && o.OrderItems.Any(i => i.ProductId == model.EntityId)))
{
ModelState.AddModelError("*", "You can only review products you have purchased.");

return PartialView("_ReviewForm", model);
}

var review = new Review
{
Rating = model.Rating,
Expand All @@ -47,6 +62,7 @@ public async Task<IActionResult> AddReview(ReviewForm model)

return PartialView("_ReviewFormSuccess", model);
}

return PartialView("_ReviewForm", model);
}

Expand Down
Loading