detect/detect.gui/Api/System/DeviceApi.cs

50 lines
1.9 KiB
C#

using detect.gui.Models.Entities;
using detect.gui.Services;
using detect.gui.Services.Detect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Splat;
namespace detect.gui.Api.System;
public class DeviceApi
{
public DeviceApi(IEndpointRouteBuilder? webApp)
{
// all
webApp?.MapGet("/v1/data/device/all", () => Locator.Current.GetService<DetectDeviceService>()!.ListAll());
// search
webApp?.MapGet("/v1/data/device/search", ([FromQuery] string? name, [FromQuery] string? ip, [FromQuery]int pageNum = 1, [FromQuery]int pageSize = 10) =>
Locator.Current.GetService<DetectDeviceService>()!.Search(name, ip, pageNum, pageSize));
// id
webApp?.MapGet("/v1/data/device/{id:long}", ([FromRoute] long id) =>
Locator.Current.GetService<DetectDeviceService>()!.ListById(id));
// add
webApp?.MapPost("/v1/data/device/", ([FromBody] DeviceEntity entity) =>
{
var result = Locator.Current.GetService<DetectDeviceService>()!.AddData(entity);
Locator.Current.GetService<DeviceClientService>()!.ConnectAllDevices();
return result;
});
// update
webApp?.MapPut("/v1/data/device/", ([FromBody] DeviceEntity entity) =>
{
var result = Locator.Current.GetService<DetectDeviceService>()!.UpdateData(entity);
Locator.Current.GetService<DeviceClientService>()!.RefreshDeviceClient(entity);
return result;
});
// delete
webApp?.MapDelete("/v1/data/device/{id:long}", ([FromRoute] long id) =>
{
var result = Locator.Current.GetService<DetectDeviceService>()!.DeleteById(id);
Locator.Current.GetService<DeviceClientService>()!.DeleteDeviceClientById(id);
return result;
});
}
}