116 lines
3.6 KiB
C#
116 lines
3.6 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Text.Json.Serialization;
|
|
using Yuna.Website.Server.Services.DeviceSkillService;
|
|
using Yuna.Website.Server.Services.DeviceService;
|
|
using Yuna.Website.Server.Model;
|
|
|
|
namespace Yuna.Website.Server.API
|
|
{
|
|
public class DeviceEndpoints
|
|
{
|
|
public void Define(WebApplication app)
|
|
{
|
|
app.MapPost("/api/device", CreateDevice)
|
|
.WithTags("device");
|
|
|
|
app.MapDelete("/api/device/{id:long}", () => { })
|
|
.WithTags("device");
|
|
|
|
app.MapGet("/api/device/{deviceId:long}", GetById)
|
|
.WithTags("device");
|
|
|
|
app.MapGet("/api/device", GetAll)
|
|
.WithTags("device");
|
|
|
|
app.MapGet("/api/device/{deviceId:long}/fetch", FetchData)
|
|
.WithTags("device")
|
|
.Produces<string>(404)
|
|
.Produces(200);
|
|
|
|
app.MapPut("/api/device/{deviceId:long}", AddSkillsToDevice)
|
|
.WithTags("device");
|
|
}
|
|
|
|
|
|
public class CreateDeviceResult
|
|
{
|
|
[JsonPropertyName("name")]
|
|
public string Name { get; set; } = null!;
|
|
|
|
[JsonPropertyName("description")]
|
|
public string Description { get; set; } = "";
|
|
|
|
[JsonPropertyName("deviceUrl")]
|
|
public string DeviceUrl { get; set; } = null!;
|
|
|
|
}
|
|
|
|
[Authorize]
|
|
public async Task<IResult> CreateDevice([FromBody] CreateDeviceResult request, IDeviceService deviceService)
|
|
{
|
|
var device = new Device()
|
|
{
|
|
Description = request.Description,
|
|
DeviceUrl = request.DeviceUrl,
|
|
Name = request.Name
|
|
};
|
|
|
|
var result = await deviceService.Create(device);
|
|
if (result is null) return Results.BadRequest();
|
|
|
|
|
|
return Results.Ok(result);
|
|
}
|
|
|
|
[Authorize]
|
|
public async Task<IResult> GetById(long deviceId,IDeviceService deviceService)
|
|
{
|
|
var result = await deviceService.GetById(deviceId);
|
|
|
|
if (result is null) return Results.NotFound(deviceId);
|
|
|
|
return Results.Ok(result);
|
|
}
|
|
|
|
[Authorize]
|
|
public async Task<IResult> GetAll(IDeviceService deviceService)
|
|
{
|
|
var result = await deviceService.GetList();
|
|
return Results.Ok(result);
|
|
}
|
|
|
|
[Authorize]
|
|
public async Task<IResult> Delete(IDeviceService deviceService, long id)
|
|
{
|
|
var result = await deviceService.Delete(id);
|
|
if (result is null) return Results.NotFound();
|
|
return Results.Ok(result);
|
|
}
|
|
|
|
[Authorize]
|
|
public async Task<IResult> AddSkillsToDevice([FromBody] long[] skillsIds, long deviceId, IDeviceService deviceService, IPropService skillService)
|
|
{
|
|
var skills = await skillService.GetByIds(skillsIds);
|
|
if (skills is null) return Results.NotFound("not all skills exist");
|
|
|
|
var result = await deviceService.AddProps(skills, deviceId);
|
|
if (result is null) return Results.NotFound("device");
|
|
return Results.Ok(result);
|
|
}
|
|
|
|
[Authorize]
|
|
public async Task<IResult> FetchData(long deviceId, IDeviceService deviceService)
|
|
{
|
|
var device = await deviceService.GetById(deviceId);
|
|
if (device is null) return Results.NotFound("device");
|
|
|
|
|
|
var result = await deviceService.FetchPropsData(device);
|
|
if (result is null) return Results.NotFound("props");
|
|
|
|
return Results.Ok(result);
|
|
}
|
|
}
|
|
}
|