Kavita/API/Data/Repositories/DeviceRepository.cs
Joseph Milazzo 9d7476a367
Send To Device Support (#1557)
* Tweaked the logging output

* Started implementing some basic idea for devices

* Updated Email Service with new API routes

* Implemented basic DB structure and some APIs to prep for the UI and flows.

* Added an abstract class to make Unit testing easier.

* Removed dependency we don't need

* Updated the UI to be able to show devices and add new devices. Email field will update the platform if the user hasn't interacted with it already.

* Added ability to delete a device as well

* Basic ability to send files to devices works

* Refactored Action code to pass ActionItem back and allow for dynamic children based on an Observable (api).

Hooked in ability to send a chapter to a device. There is no logic in the FE to validate type.

* Fixed a broken unit test

* Implemented the ability to edit a device

* Code cleanup

* Fixed a bad success message

* Fixed broken unit test from updating mock layer
2022-09-23 15:41:29 -07:00

51 lines
1.3 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.DTOs.Device;
using API.Entities;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
namespace API.Data.Repositories;
public interface IDeviceRepository
{
void Update(Device device);
Task<IEnumerable<DeviceDto>> 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<DeviceDto>> GetDevicesForUserAsync(int userId)
{
return await _context.Device
.Where(d => d.AppUserId == userId)
.OrderBy(d => d.LastUsed)
.ProjectTo<DeviceDto>(_mapper.ConfigurationProvider)
.ToListAsync();
}
public async Task<Device> GetDeviceById(int deviceId)
{
return await _context.Device
.Where(d => d.Id == deviceId)
.SingleOrDefaultAsync();
}
}