Kavita/API/Data/Repositories/DeviceRepository.cs
Joe Milazzo 9f29fa593d
Progress Overhaul + Profile Page and a LOT more! (#4262)
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com>
Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
2025-12-09 10:00:11 -07:00

53 lines
1.4 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.DTOs.Device;
using API.DTOs.Device.EmailDevice;
using API.Entities;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
namespace API.Data.Repositories;
#nullable enable
public interface IDeviceRepository
{
void Update(Device device);
Task<IEnumerable<EmailDeviceDto>> GetDevicesForUserAsync(int userId);
Task<Device?> GetDeviceById(int deviceId);
}
public class DeviceRepository : IDeviceRepository
{
private readonly DataContext _context;
private readonly IMapper _mapper;
public DeviceRepository(DataContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public void Update(Device device)
{
_context.Entry(device).State = EntityState.Modified;
}
public async Task<IEnumerable<EmailDeviceDto>> GetDevicesForUserAsync(int userId)
{
return await _context.Device
.Where(d => d.AppUserId == userId)
.OrderBy(d => d.LastUsed)
.ProjectTo<EmailDeviceDto>(_mapper.ConfigurationProvider)
.ToListAsync();
}
public async Task<Device?> GetDeviceById(int deviceId)
{
return await _context.Device
.Where(d => d.Id == deviceId)
.SingleOrDefaultAsync();
}
}