add voice service call service and more
This commit is contained in:
@@ -25,13 +25,102 @@ public sealed class DeviceService : IDeviceService
|
||||
|
||||
public async Task<IReadOnlyList<DeviceDto>> ListAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var items = await dbContext.Devices.AsNoTracking().OrderBy(d => d.DeviceName).ToListAsync(cancellationToken);
|
||||
var items = await dbContext.Devices
|
||||
.AsNoTracking()
|
||||
.Include(d => d.User)
|
||||
.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);
|
||||
var item = await dbContext.Devices
|
||||
.AsNoTracking()
|
||||
.Include(d => d.User)
|
||||
.Where(d=>d.DeviceName==deviceName)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
return mapper.Map<DeviceDto>(item);
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<DeviceDto>> GetUserDevicesAsync(int userId, CancellationToken cancellationToken)
|
||||
{
|
||||
var items = await dbContext.Devices
|
||||
.AsNoTracking()
|
||||
.Include(d => d.User)
|
||||
.Where(d => d.UserId == userId)
|
||||
.OrderBy(d => d.DeviceName)
|
||||
.ToListAsync(cancellationToken);
|
||||
return mapper.Map<IReadOnlyList<DeviceDto>>(items);
|
||||
}
|
||||
|
||||
public async Task<PagedResult<DeviceDto>> GetDevicesAsync(DeviceFilter filter, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!filter.UserId.HasValue)
|
||||
{
|
||||
throw new ArgumentException("UserId is required", nameof(filter));
|
||||
}
|
||||
|
||||
// Get user and role
|
||||
var user = await dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(u => u.Id == filter.UserId.Value, cancellationToken);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
return new PagedResult<DeviceDto>
|
||||
{
|
||||
Items = Array.Empty<DeviceDto>(),
|
||||
TotalCount = 0,
|
||||
Page = filter.Page,
|
||||
PageSize = filter.PageSize
|
||||
};
|
||||
}
|
||||
|
||||
// Build query based on role
|
||||
IQueryable<Domain.Device> query = dbContext.Devices
|
||||
.AsNoTracking()
|
||||
.Include(d => d.User);
|
||||
|
||||
if (user.Role == Domain.UserRole.Normal)
|
||||
{
|
||||
// Normal user: only own devices
|
||||
query = query.Where(d => d.UserId == user.Id);
|
||||
}
|
||||
else if (user.Role == Domain.UserRole.Supervisor)
|
||||
{
|
||||
// Supervisor: devices assigned to them
|
||||
query = query.Where(d => d.DeviceUsers.Any(du => du.UserId == user.Id));
|
||||
}
|
||||
// Admin: all devices (no filter)
|
||||
|
||||
// Apply search filter
|
||||
if (!string.IsNullOrWhiteSpace(filter.Search))
|
||||
{
|
||||
var searchTerm = filter.Search.Trim().ToLower();
|
||||
query = query.Where(d =>
|
||||
d.DeviceName.ToLower().Contains(searchTerm) ||
|
||||
d.User.Name.ToLower().Contains(searchTerm) ||
|
||||
d.User.Family.ToLower().Contains(searchTerm) ||
|
||||
d.Location.ToLower().Contains(searchTerm));
|
||||
}
|
||||
|
||||
// Get total count
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
|
||||
// Apply pagination
|
||||
var items = await query
|
||||
.OrderBy(d => d.DeviceName)
|
||||
.Skip((filter.Page - 1) * filter.PageSize)
|
||||
.Take(filter.PageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return new PagedResult<DeviceDto>
|
||||
{
|
||||
Items = mapper.Map<IReadOnlyList<DeviceDto>>(items),
|
||||
TotalCount = totalCount,
|
||||
Page = filter.Page,
|
||||
PageSize = filter.PageSize
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user