using Microsoft.AspNetCore.Mvc; using GreenHome.Application; namespace GreenHome.Api.Controllers; [ApiController] [Route("api/[controller]")] public class DevicesController : ControllerBase { private readonly IDeviceService deviceService; public DevicesController(IDeviceService deviceService) { this.deviceService = deviceService; } [HttpGet] public async Task>> GetAll(CancellationToken cancellationToken) { var result = await deviceService.ListAsync(cancellationToken); return Ok(result); } [HttpGet("CheckDevice")] public async Task>> CheckDevice(string deviceName,CancellationToken cancellationToken) { var result = await deviceService.GetDeviceId(deviceName,cancellationToken); return Ok(result); } [HttpPost] public async Task> Create(DeviceDto dto, CancellationToken cancellationToken) { var id = await deviceService.AddDeviceAsync(dto, cancellationToken); return Ok(id); } [HttpGet("user/{userId}")] public async Task>> GetUserDevices(int userId, CancellationToken cancellationToken) { var result = await deviceService.GetUserDevicesAsync(userId, cancellationToken); return Ok(result); } [HttpGet("filtered")] public async Task>> GetDevices( [FromQuery] int userId, [FromQuery] string? search = null, [FromQuery] int page = 1, [FromQuery] int pageSize = 10, CancellationToken cancellationToken = default) { var filter = new DeviceFilter { UserId = userId, Search = search, Page = page, PageSize = pageSize }; var result = await deviceService.GetDevicesAsync(filter, cancellationToken); return Ok(result); } }