// src/Webshop.Application/Services/Public/ReviewService.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Webshop.Application; using Webshop.Application.DTOs.Reviews; using Webshop.Domain.Interfaces; namespace Webshop.Application.Services.Public { public class ReviewService : IReviewService { private readonly IReviewRepository _reviewRepository; private readonly IProductRepository _productRepository; public ReviewService(IReviewRepository reviewRepository, IProductRepository productRepository) { _reviewRepository = reviewRepository; _productRepository = productRepository; } public async Task>> GetApprovedReviewsByProductIdAsync(Guid productId) { // Prüfung: Existiert das Produkt und ist es für die Öffentlichkeit sichtbar? var product = await _productRepository.GetProductByIdAsync(productId); if (product == null || !product.IsActive) { return ServiceResult.Fail>(ServiceResultType.NotFound, $"Produkt mit ID '{productId}' wurde nicht gefunden oder ist nicht aktiv."); } var reviews = await _reviewRepository.GetApprovedByProductIdAsync(productId); var dtos = reviews.Select(r => new ReviewDto { // Anonymisiertes Mapping für die öffentliche Ansicht CustomerName = (r.Customer != null) ? $"{r.Customer.FirstName} {r.Customer.LastName?.FirstOrDefault()}." : "Anonym", Rating = r.Rating, Title = r.Title, Comment = r.Comment, ReviewDate = r.ReviewDate }).ToList(); return ServiceResult.Ok>(dtos); } } }