SPMS_API/SPMS.Infrastructure/Persistence/Repositories/ServiceRepository.cs
seonkyu.kim c8a3d616c3 feat: IP 화이트리스트 관리 API 구현 (#50)
- IP 목록 조회, 추가, 삭제 API 구현
- IPv4 형식 검증 추가
- 중복 IP 체크 로직 추가

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-10 00:52:41 +09:00

45 lines
1.7 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();
// 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);
}