// src/Webshop.Application/Services/Customers/ReviewService.cs using System; using System.Linq; using System.Threading.Tasks; using Webshop.Application.DTOs.Reviews; using Webshop.Domain.Entities; using Webshop.Domain.Interfaces; using Webshop.Infrastructure.Data; namespace Webshop.Application.Services.Customers { public class CustomerReviewService : ICustomerReviewService { private readonly IReviewRepository _reviewRepository; private readonly ICustomerRepository _customerRepository; private readonly ApplicationDbContext _context; public CustomerReviewService(IReviewRepository reviewRepository, ICustomerRepository customerRepository, ApplicationDbContext context) { _reviewRepository = reviewRepository; _customerRepository = customerRepository; _context = context; } public async Task> CreateReviewAsync(CreateReviewDto reviewDto, string userId) { var customer = await _customerRepository.GetByUserIdAsync(userId); if (customer == null) return ServiceResult.Fail(ServiceResultType.Unauthorized, "Kunde nicht gefunden."); // Business-Regel: Hat der Kunde dieses Produkt gekauft? // bool hasPurchased = await _context.OrderItems // .AnyAsync(oi => oi.ProductId == reviewDto.ProductId && oi.Order.CustomerId == customer.Id); // if (!hasPurchased) return ServiceResult.Fail(ServiceResultType.Forbidden, "Sie können nur Produkte bewerten, die Sie gekauft haben."); var newReview = new Review { ProductId = reviewDto.ProductId, CustomerId = customer.Id, Rating = reviewDto.Rating, Title = reviewDto.Title, Comment = reviewDto.Comment, ReviewDate = DateTimeOffset.UtcNow, IsApproved = false // Bewertungen müssen vom Admin freigegeben werden }; await _reviewRepository.AddAsync(newReview); var resultDto = new ReviewDto { /* Mapping */ }; return ServiceResult.Ok(resultDto); } } }