using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZeroFramework.DeviceCenter.Application.Models.Devices;
using ZeroFramework.DeviceCenter.Application.PermissionProviders;
using ZeroFramework.DeviceCenter.Application.Services.Devices;
using ZeroFramework.DeviceCenter.Application.Services.Generics;
namespace ZeroFramework.DeviceCenter.API.Controllers
{
///
/// For more information on enabling Web API for empty projects
///
[Route("api/[controller]")]
[ApiController]
public class DeviceGroupsController(IDeviceGroupApplicationService deviceGroupService) : ControllerBase
{
private readonly IDeviceGroupApplicationService _deviceGroupService = deviceGroupService;
// GET: api/
[HttpGet]
[Authorize(DeviceGroupPermissions.DeviceGroups.Default)]
public async Task> GetDeviceGroups([FromQuery] DeviceGroupPagedRequestModel model)
{
return await _deviceGroupService.GetListAsync(model);
}
[HttpPut("Devices")]
[Authorize(DeviceGroupPermissions.DeviceGroups.Edit)]
public async Task PutDevicesToGroup(int deviceGroupId, [FromBody] long[] deviceIds)
{
await _deviceGroupService.AddDevicesToGroup(deviceGroupId, deviceIds);
return Ok();
}
[HttpDelete("Devices")]
[Authorize(DeviceGroupPermissions.DeviceGroups.Edit)]
public async Task DeleteDevicesFromGroup(int deviceGroupId, [FromBody] long[] deviceIds)
{
await _deviceGroupService.RemoveDevicesFromGroup(deviceGroupId, deviceIds);
return Ok();
}
// GET api//5
[HttpGet("{id}")]
[Authorize(DeviceGroupPermissions.DeviceGroups.Default)]
public async Task GetDeviceGroup(int id)
{
return await _deviceGroupService.GetAsync(id);
}
// POST api/
[HttpPost]
[Authorize(DeviceGroupPermissions.DeviceGroups.Create)]
public async Task PostDeviceGroup([FromBody] DeviceGroupCreateRequestModel value)
{
return await _deviceGroupService.CreateAsync(value);
}
// PUT api//5
[HttpPut("{id}")]
[Authorize(DeviceGroupPermissions.DeviceGroups.Edit)]
public async Task PutDeviceGroup(int id, [FromBody] DeviceGroupUpdateRequestModel value)
{
value.Id = id;
return await _deviceGroupService.UpdateAsync(id, value);
}
// DELETE api//5
[HttpDelete("{id}")]
[Authorize(DeviceGroupPermissions.DeviceGroups.Delete)]
public async Task DeleteDeviceGroup(int id)
{
await _deviceGroupService.DeleteAsync(id);
}
}
}