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/{id:long}", () => { }) .WithTags("device"); app.MapGet("/api/device", GetAll) .WithTags("device"); 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 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 GetAll(IDeviceService deviceService) { var result = await deviceService.GetList(); return Results.Ok(result); } [Authorize] public async Task 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 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); } } }