review
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// src/Webshop.Application/DTOs/Reviews/CreateReviewDto.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Webshop.Application.DTOs.Reviews
|
||||
{
|
||||
public class CreateReviewDto
|
||||
{
|
||||
[Required]
|
||||
public Guid ProductId { get; set; }
|
||||
|
||||
[Required]
|
||||
[Range(1, 5)]
|
||||
public int Rating { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string Comment { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[MaxLength(255)]
|
||||
public string Title { get; set; } = string.Empty;
|
||||
|
||||
[MaxLength(2000)]
|
||||
public string? Comment { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// src/Webshop.Application/DTOs/Reviews/ReviewDto.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Webshop.Application.DTOs.Reviews
|
||||
{
|
||||
@@ -10,11 +7,12 @@ namespace Webshop.Application.DTOs.Reviews
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid ProductId { get; set; }
|
||||
public Guid? CustomerId { get; set; }
|
||||
public string? ProductName { get; set; } // F<>r Admin-Ansicht
|
||||
public string CustomerName { get; set; } = string.Empty;
|
||||
public int Rating { get; set; }
|
||||
public string? Title { get; set; }
|
||||
public string Comment { get; set; } = string.Empty;
|
||||
public string Title { get; set; } = string.Empty;
|
||||
public string? Comment { get; set; }
|
||||
public DateTimeOffset ReviewDate { get; set; }
|
||||
public bool IsApproved { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Webshop.Application/Services/Admin/AdminReviewService.cs
Normal file
56
Webshop.Application/Services/Admin/AdminReviewService.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
// src/Webshop.Application/Services/Admin/AdminReviewService.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Reviews;
|
||||
using Webshop.Domain.Interfaces;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
public class AdminReviewService : IAdminReviewService
|
||||
{
|
||||
private readonly IReviewRepository _reviewRepository;
|
||||
|
||||
public AdminReviewService(IReviewRepository reviewRepository)
|
||||
{
|
||||
_reviewRepository = reviewRepository;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ReviewDto>> GetAllReviewsAsync()
|
||||
{
|
||||
var reviews = await _reviewRepository.GetAllAsync();
|
||||
return reviews.Select(r => new ReviewDto
|
||||
{
|
||||
Id = r.Id,
|
||||
ProductId = r.ProductId,
|
||||
ProductName = r.Product?.Name,
|
||||
CustomerName = $"{r.Customer?.FirstName} {r.Customer?.LastName}",
|
||||
Rating = r.Rating,
|
||||
Title = r.Title,
|
||||
Comment = r.Comment,
|
||||
ReviewDate = r.ReviewDate,
|
||||
IsApproved = r.IsApproved
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<ServiceResult> ApproveReviewAsync(Guid reviewId)
|
||||
{
|
||||
var review = await _reviewRepository.GetByIdAsync(reviewId);
|
||||
if (review == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Bewertung nicht gefunden.");
|
||||
|
||||
review.IsApproved = true;
|
||||
await _reviewRepository.UpdateAsync(review);
|
||||
return ServiceResult.Ok();
|
||||
}
|
||||
|
||||
public async Task<ServiceResult> DeleteReviewAsync(Guid reviewId)
|
||||
{
|
||||
var review = await _reviewRepository.GetByIdAsync(reviewId);
|
||||
if (review == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Bewertung nicht gefunden.");
|
||||
|
||||
await _reviewRepository.DeleteAsync(reviewId);
|
||||
return ServiceResult.Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// src/Webshop.Application/Services/Admin/IAdminReviewService.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application;
|
||||
using Webshop.Application.DTOs.Reviews;
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
public interface IAdminReviewService
|
||||
{
|
||||
Task<IEnumerable<ReviewDto>> GetAllReviewsAsync();
|
||||
Task<ServiceResult> ApproveReviewAsync(Guid reviewId);
|
||||
Task<ServiceResult> DeleteReviewAsync(Guid reviewId);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,12 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
// src/Webshop.Application/Services/Customers/Interfaces/IReviewService.cs
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs;
|
||||
using Webshop.Application.DTOs.Auth;
|
||||
using Webshop.Application.DTOs.Users;
|
||||
using Webshop.Application;
|
||||
using Webshop.Application.DTOs.Reviews;
|
||||
|
||||
|
||||
namespace Webshop.Application.Services.Customers.Interfaces
|
||||
namespace Webshop.Application.Services.Customers
|
||||
{
|
||||
public interface IReviewService
|
||||
public interface ICustomerReviewService
|
||||
{
|
||||
// Fügen Sie hier Methodensignaturen hinzu
|
||||
Task<ServiceResult<ReviewDto>> CreateReviewAsync(CreateReviewDto reviewDto, string userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,52 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// src/Webshop.Application/Services/Customers/ReviewService.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.Services.Customers.Interfaces;
|
||||
|
||||
using Webshop.Application.DTOs.Reviews;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Interfaces;
|
||||
using Webshop.Infrastructure.Data;
|
||||
|
||||
namespace Webshop.Application.Services.Customers
|
||||
{
|
||||
public class ReviewService : IReviewService
|
||||
public class CustomerReviewService : ICustomerReviewService
|
||||
{
|
||||
// Fügen Sie hier Abhängigkeiten per Dependency Injection hinzu (z.B. Repositories)
|
||||
private readonly IReviewRepository _reviewRepository;
|
||||
private readonly ICustomerRepository _customerRepository;
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
// public ReviewService(IYourRepository repository) { }
|
||||
public CustomerReviewService(IReviewRepository reviewRepository, ICustomerRepository customerRepository, ApplicationDbContext context)
|
||||
{
|
||||
_reviewRepository = reviewRepository;
|
||||
_customerRepository = customerRepository;
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// Fügen Sie hier Service-Methoden hinzu
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// src/Webshop.Application/Services/Public/Interfaces/IReviewService.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Reviews;
|
||||
|
||||
namespace Webshop.Application.Services.Public
|
||||
{
|
||||
public interface IReviewService
|
||||
{
|
||||
Task<IEnumerable<ReviewDto>> GetApprovedReviewsByProductIdAsync(Guid productId);
|
||||
}
|
||||
}
|
||||
34
Webshop.Application/Services/Public/ReviewService.cs
Normal file
34
Webshop.Application/Services/Public/ReviewService.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
// src/Webshop.Application/Services/Public/ReviewService.cs
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Webshop.Application.DTOs.Reviews;
|
||||
using Webshop.Domain.Interfaces;
|
||||
|
||||
namespace Webshop.Application.Services.Public
|
||||
{
|
||||
public class ReviewService : IReviewService
|
||||
{
|
||||
private readonly IReviewRepository _reviewRepository;
|
||||
|
||||
public ReviewService(IReviewRepository reviewRepository)
|
||||
{
|
||||
_reviewRepository = reviewRepository;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ReviewDto>> GetApprovedReviewsByProductIdAsync(Guid productId)
|
||||
{
|
||||
var reviews = await _reviewRepository.GetApprovedByProductIdAsync(productId);
|
||||
return reviews.Select(r => new ReviewDto
|
||||
{
|
||||
// Mapping zu DTO, CustomerName wird für Anonymität oft gekürzt
|
||||
CustomerName = $"{r.Customer?.FirstName} {r.Customer?.LastName?.FirstOrDefault()}.",
|
||||
Rating = r.Rating,
|
||||
Title = r.Title,
|
||||
Comment = r.Comment,
|
||||
ReviewDate = r.ReviewDate
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user