Files
ShopSolution-backend/Webshop.Infrastructure/Repositories/ReviewRepository.cs
Tizian.Breuch 5b84ccc575 review
2025-09-08 11:24:10 +02:00

67 lines
1.9 KiB
C#

// src/Webshop.Infrastructure/Repositories/ReviewRepository.cs
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data;
namespace Webshop.Infrastructure.Repositories
{
public class ReviewRepository : IReviewRepository
{
private readonly ApplicationDbContext _context;
public ReviewRepository(ApplicationDbContext context)
{
_context = context;
}
public async Task<IEnumerable<Review>> GetAllAsync()
{
return await _context.Reviews
.Include(r => r.Customer)
.Include(r => r.Product)
.OrderByDescending(r => r.ReviewDate)
.ToListAsync();
}
public async Task<IEnumerable<Review>> GetApprovedByProductIdAsync(Guid productId)
{
return await _context.Reviews
.Include(r => r.Customer)
.Where(r => r.ProductId == productId && r.IsApproved)
.OrderByDescending(r => r.ReviewDate)
.ToListAsync();
}
public async Task<Review?> GetByIdAsync(Guid id)
{
return await _context.Reviews.FindAsync(id);
}
public async Task AddAsync(Review review)
{
await _context.Reviews.AddAsync(review);
await _context.SaveChangesAsync();
}
public async Task UpdateAsync(Review review)
{
_context.Reviews.Update(review);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(Guid id)
{
var review = await _context.Reviews.FindAsync(id);
if (review != null)
{
_context.Reviews.Remove(review);
await _context.SaveChangesAsync();
}
}
}
}