first commit

This commit is contained in:
motahhari
2025-10-31 20:21:22 +03:30
commit 60d20a2734
186 changed files with 15497 additions and 0 deletions

View 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);
}
}