Files
ShopSolution-backend/Webshop.Application/Services/Customers/ReviewService.cs
Tizian.Breuch 5b84ccc575 review
2025-09-08 11:24:10 +02:00

52 lines
2.2 KiB
C#

// 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<ServiceResult<ReviewDto>> CreateReviewAsync(CreateReviewDto reviewDto, string userId)
{
var customer = await _customerRepository.GetByUserIdAsync(userId);
if (customer == null) return ServiceResult.Fail<ReviewDto>(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<ReviewDto>(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);
}
}
}