Files
GreenHomeBack/src/GreenHome.Api/Controllers/DevicesController.cs

66 lines
2.0 KiB
C#

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<ActionResult<IReadOnlyList<DeviceDto>>> GetAll(CancellationToken cancellationToken)
{
var result = await deviceService.ListAsync(cancellationToken);
return Ok(result);
}
[HttpGet("CheckDevice")]
public async Task<ActionResult<IReadOnlyList<DeviceDto>>> CheckDevice(string deviceName,CancellationToken cancellationToken)
{
var result = await deviceService.GetDeviceId(deviceName,cancellationToken);
return Ok(result);
}
[HttpPost]
public async Task<ActionResult<int>> Create(DeviceDto dto, CancellationToken cancellationToken)
{
var id = await deviceService.AddDeviceAsync(dto, cancellationToken);
return Ok(id);
}
[HttpGet("user/{userId}")]
public async Task<ActionResult<IReadOnlyList<DeviceDto>>> GetUserDevices(int userId, CancellationToken cancellationToken)
{
var result = await deviceService.GetUserDevicesAsync(userId, cancellationToken);
return Ok(result);
}
[HttpGet("filtered")]
public async Task<ActionResult<PagedResult<DeviceDto>>> 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);
}
}