60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
// src/Webshop.Infrastructure/Repositories/DiscountRepository.cs
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Webshop.Domain.Entities;
|
|
using Webshop.Domain.Interfaces;
|
|
using Webshop.Infrastructure.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Webshop.Infrastructure.Repositories
|
|
{
|
|
public class DiscountRepository : IDiscountRepository
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public DiscountRepository(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<IEnumerable<Discount>> GetAllAsync()
|
|
{
|
|
return await _context.Discounts
|
|
.Include(d => d.ProductDiscounts)
|
|
.Include(d => d.categorieDiscounts)
|
|
.OrderBy(d => d.Name)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<Discount?> GetByIdAsync(Guid id)
|
|
{
|
|
return await _context.Discounts
|
|
.Include(d => d.ProductDiscounts)
|
|
.Include(d => d.categorieDiscounts)
|
|
.FirstOrDefaultAsync(d => d.Id == id);
|
|
}
|
|
|
|
public async Task AddAsync(Discount discount)
|
|
{
|
|
await _context.Discounts.AddAsync(discount);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateAsync(Discount discount)
|
|
{
|
|
_context.Discounts.Update(discount);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteAsync(Guid id)
|
|
{
|
|
var discount = await _context.Discounts.FindAsync(id);
|
|
if (discount != null)
|
|
{
|
|
_context.Discounts.Remove(discount);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
} |