first commit
This commit is contained in:
37
src/GreenHome.Infrastructure/DeviceService.cs
Normal file
37
src/GreenHome.Infrastructure/DeviceService.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using AutoMapper;
|
||||
using GreenHome.Application;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace GreenHome.Infrastructure;
|
||||
|
||||
public sealed class DeviceService : IDeviceService
|
||||
{
|
||||
private readonly GreenHomeDbContext dbContext;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public DeviceService(GreenHomeDbContext dbContext, IMapper mapper)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<int> AddDeviceAsync(DeviceDto dto, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = mapper.Map<Domain.Device>(dto);
|
||||
dbContext.Devices.Add(entity);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
return entity.Id;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<DeviceDto>> ListAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var items = await dbContext.Devices.AsNoTracking().OrderBy(d => d.DeviceName).ToListAsync(cancellationToken);
|
||||
return mapper.Map<IReadOnlyList<DeviceDto>>(items);
|
||||
}
|
||||
|
||||
public async Task<DeviceDto> GetDeviceId(string deviceName,CancellationToken cancellationToken)
|
||||
{
|
||||
var item = await dbContext.Devices.AsNoTracking().Where(d=>d.DeviceName==deviceName).FirstOrDefaultAsync(cancellationToken);
|
||||
return mapper.Map<DeviceDto>(item);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user