SPMS_API/SPMS.Infrastructure/Persistence/Repositories/ServiceRepository.cs

51 lines
2.0 KiB
C#

using Microsoft.EntityFrameworkCore;
using SPMS.Domain.Entities;
using SPMS.Domain.Enums;
using SPMS.Domain.Interfaces;
namespace SPMS.Infrastructure.Persistence.Repositories;
public class ServiceRepository : Repository<Service>, IServiceRepository
{
public ServiceRepository(AppDbContext context) : base(context) { }
public async Task<Service?> GetByServiceCodeAsync(string serviceCode)
=> await _dbSet.FirstOrDefaultAsync(s => s.ServiceCode == serviceCode);
public async Task<Service?> GetByApiKeyAsync(string apiKey)
=> await _dbSet.FirstOrDefaultAsync(s => s.ApiKey == apiKey);
public async Task<Service?> GetByIdWithIpsAsync(long id)
=> await _dbSet
.Include(s => s.ServiceIps)
.FirstOrDefaultAsync(s => s.Id == id);
public async Task<Service?> GetByServiceCodeWithIpsAsync(string serviceCode)
=> await _dbSet
.Include(s => s.ServiceIps)
.FirstOrDefaultAsync(s => s.ServiceCode == serviceCode);
public async Task<IReadOnlyList<Service>> GetByStatusAsync(ServiceStatus status)
=> await _dbSet.Where(s => s.Status == status).ToListAsync();
public async Task<bool> ServiceNameExistsAsync(string serviceName)
=> await _dbSet.AnyAsync(s => s.ServiceName == serviceName);
public async Task<bool> ServiceCodeExistsAsync(string serviceCode)
=> await _dbSet.AnyAsync(s => s.ServiceCode == serviceCode);
// ServiceIp methods
public async Task<ServiceIp?> GetServiceIpByIdAsync(long ipId)
=> await _context.Set<ServiceIp>().FirstOrDefaultAsync(ip => ip.Id == ipId);
public async Task<bool> ServiceIpExistsAsync(long serviceId, string ipAddress)
=> await _context.Set<ServiceIp>()
.AnyAsync(ip => ip.ServiceId == serviceId && ip.IpAddress == ipAddress);
public async Task AddServiceIpAsync(ServiceIp serviceIp)
=> await _context.Set<ServiceIp>().AddAsync(serviceIp);
public void DeleteServiceIp(ServiceIp serviceIp)
=> _context.Set<ServiceIp>().Remove(serviceIp);
}