mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-12-22 12:57:22 -05:00
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com> Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
53 lines
1.4 KiB
C#
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();
|
|
}
|
|
}
|