review
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
// src/Webshop.Api/Controllers/Public/ReviewsController.cs
|
// src/Webshop.Api/Controllers/Public/ReviewsController.cs
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Webshop.Application.DTOs.Products; // Für ProductDto
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Reviews;
|
using Webshop.Application.DTOs.Reviews;
|
||||||
using Webshop.Application.Services.Public;
|
using Webshop.Application.Services.Public;
|
||||||
|
|
||||||
@@ -23,10 +24,18 @@ namespace Webshop.Api.Controllers.Public
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<ReviewDto>>> GetApprovedReviews(Guid productId)
|
[ProducesResponseType(typeof(IEnumerable<ReviewDto>), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<IActionResult> GetApprovedReviews(Guid productId)
|
||||||
{
|
{
|
||||||
var reviews = await _reviewService.GetApprovedReviewsByProductIdAsync(productId);
|
var result = await _reviewService.GetApprovedReviewsByProductIdAsync(productId);
|
||||||
return Ok(reviews);
|
|
||||||
|
return result.Type switch
|
||||||
|
{
|
||||||
|
ServiceResultType.Success => Ok(result.Value),
|
||||||
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||||
|
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,12 +2,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Reviews;
|
using Webshop.Application.DTOs.Reviews;
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Public
|
namespace Webshop.Application.Services.Public
|
||||||
{
|
{
|
||||||
public interface IReviewService
|
public interface IReviewService
|
||||||
{
|
{
|
||||||
Task<IEnumerable<ReviewDto>> GetApprovedReviewsByProductIdAsync(Guid productId);
|
Task<ServiceResult<IEnumerable<ReviewDto>>> GetApprovedReviewsByProductIdAsync(Guid productId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Reviews;
|
using Webshop.Application.DTOs.Reviews;
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Domain.Interfaces;
|
||||||
|
|
||||||
@@ -11,24 +12,36 @@ namespace Webshop.Application.Services.Public
|
|||||||
public class ReviewService : IReviewService
|
public class ReviewService : IReviewService
|
||||||
{
|
{
|
||||||
private readonly IReviewRepository _reviewRepository;
|
private readonly IReviewRepository _reviewRepository;
|
||||||
|
private readonly IProductRepository _productRepository;
|
||||||
|
|
||||||
public ReviewService(IReviewRepository reviewRepository)
|
public ReviewService(IReviewRepository reviewRepository, IProductRepository productRepository)
|
||||||
{
|
{
|
||||||
_reviewRepository = reviewRepository;
|
_reviewRepository = reviewRepository;
|
||||||
|
_productRepository = productRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<ReviewDto>> GetApprovedReviewsByProductIdAsync(Guid productId)
|
public async Task<ServiceResult<IEnumerable<ReviewDto>>> 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<IEnumerable<ReviewDto>>(ServiceResultType.NotFound, $"Produkt mit ID '{productId}' wurde nicht gefunden oder ist nicht aktiv.");
|
||||||
|
}
|
||||||
|
|
||||||
var reviews = await _reviewRepository.GetApprovedByProductIdAsync(productId);
|
var reviews = await _reviewRepository.GetApprovedByProductIdAsync(productId);
|
||||||
return reviews.Select(r => new ReviewDto
|
|
||||||
|
var dtos = reviews.Select(r => new ReviewDto
|
||||||
{
|
{
|
||||||
// Mapping zu DTO, CustomerName wird für Anonymität oft gekürzt
|
// Anonymisiertes Mapping für die öffentliche Ansicht
|
||||||
CustomerName = $"{r.Customer?.FirstName} {r.Customer?.LastName?.FirstOrDefault()}.",
|
CustomerName = (r.Customer != null) ? $"{r.Customer.FirstName} {r.Customer.LastName?.FirstOrDefault()}." : "Anonym",
|
||||||
Rating = r.Rating,
|
Rating = r.Rating,
|
||||||
Title = r.Title,
|
Title = r.Title,
|
||||||
Comment = r.Comment,
|
Comment = r.Comment,
|
||||||
ReviewDate = r.ReviewDate
|
ReviewDate = r.ReviewDate
|
||||||
});
|
}).ToList();
|
||||||
|
|
||||||
|
return ServiceResult.Ok<IEnumerable<ReviewDto>>(dtos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user