diff --git a/Yuna.Website/Yuna.Website.Server/API/DeviceEndpoints.cs b/Yuna.Website/Yuna.Website.Server/API/DeviceEndpoints.cs index 96ad0f2..f34d5e1 100644 --- a/Yuna.Website/Yuna.Website.Server/API/DeviceEndpoints.cs +++ b/Yuna.Website/Yuna.Website.Server/API/DeviceEndpoints.cs @@ -17,7 +17,7 @@ namespace Yuna.Website.Server.API app.MapDelete("/api/device/{id:long}", () => { }) .WithTags("device"); - app.MapGet("/api/device/{id:long}", () => { }) + app.MapGet("/api/device/{deviceId:long}", GetById) .WithTags("device"); app.MapGet("/api/device", GetAll) @@ -58,6 +58,16 @@ namespace Yuna.Website.Server.API return Results.Ok(result); } + [Authorize] + public async Task 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 GetAll(IDeviceService deviceService) { diff --git a/Yuna.Website/Yuna.Website.Server/Starter.cs b/Yuna.Website/Yuna.Website.Server/Starter.cs index 4ae3d45..39c69e6 100644 --- a/Yuna.Website/Yuna.Website.Server/Starter.cs +++ b/Yuna.Website/Yuna.Website.Server/Starter.cs @@ -8,6 +8,7 @@ using Yuna.Website.Server.Services.UserService; using Yuna.Website.Server.Storage; using Yuna.Website.Server.Storage.Repositories.Device; using Yuna.Website.Server.Storage.Repositories.Prop; +using Yuna.Website.Server.Storage.Repositories.User; namespace Yuna.Website.Server { @@ -131,6 +132,7 @@ namespace Yuna.Website.Server builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); } public static void DefineAuth(WebApplicationBuilder builder) diff --git a/Yuna.Website/Yuna.Website.Server/Storage/Repositories/Device/DeviceRepository.cs b/Yuna.Website/Yuna.Website.Server/Storage/Repositories/Device/DeviceRepository.cs index 0183e45..bd3d412 100644 --- a/Yuna.Website/Yuna.Website.Server/Storage/Repositories/Device/DeviceRepository.cs +++ b/Yuna.Website/Yuna.Website.Server/Storage/Repositories/Device/DeviceRepository.cs @@ -41,25 +41,40 @@ namespace Yuna.Website.Server.Storage.Repositories.Device throw new NotImplementedException(); } - public async Task GetById(long id) + public async Task GetById(long id) { var query = $@"SELECT - d.""Id"" as {nameof(Model.Device.Id)}, - d.""Name"" as {nameof(Model.Device.Name)}, - d.""Description"" as {nameof(Model.Device.Description)}, - p.""Id"" as {nameof(Model.Prop.Id)}, - p.""Name"" as {nameof(Model.Prop.Name)}, - p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}, - p.""MeasureName"" as {nameof(Model.Prop.MeasureName)} - FROM ""Yuna_Devices"" d - LEFT JOIN ""Yuna_Props_In_Devices"" pd ON d.""Id"" = pd.""DeviceId"" - LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id"" - WHERE d.""Id"" = {id} - LIMIT 1"; + d.""Id"" as {nameof(Model.Device.Id)}, + d.""Name"" as {nameof(Model.Device.Name)}, + d.""Description"" as {nameof(Model.Device.Description)}, + p.""Id"" as {nameof(Model.Prop.Id)}, + p.""Name"" as {nameof(Model.Prop.Name)}, + p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}, + p.""MeasureName"" as {nameof(Model.Prop.MeasureName)} + FROM ""Yuna_Devices"" d + LEFT JOIN ""Yuna_Props_In_Devices"" pd ON d.""Id"" = pd.""DeviceId"" + LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id"" + WHERE d.""Id"" = {id}"; - var result = await _context.Connection.QuerySingleAsync(query); - return result; + var deviceDict = new Dictionary(); + var result = await _context.Connection.QueryAsync( + query, + (device, prop) => + { + if (!deviceDict.TryGetValue(device.Id, out var currentDevice)) + { + currentDevice = device; + currentDevice.Props = new List(); + deviceDict.Add(currentDevice.Id, currentDevice); + } + currentDevice.Props.Add(prop); + return currentDevice; + }, + splitOn: $"{nameof(Model.Prop.Id)}" // Specify the column to split the results on + ); + + return result.FirstOrDefault(); } public async Task> GetList() @@ -77,8 +92,24 @@ namespace Yuna.Website.Server.Storage.Repositories.Device LEFT JOIN ""Yuna_Props_In_Devices"" pd ON d.""Id"" = pd.""DeviceId"" LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id"""; - var result = await _context.Connection.QueryAsync(query); - return result.ToList(); + var deviceDict = new Dictionary(); + var result = await _context.Connection.QueryAsync( + query, + (device, prop) => + { + if (!deviceDict.TryGetValue(device.Id, out var currentDevice)) + { + currentDevice = device; + currentDevice.Props = new List(); + deviceDict.Add(currentDevice.Id, currentDevice); + } + currentDevice.Props.Add(prop); + return currentDevice; + }, + splitOn: $"{nameof(Model.Prop.Id)}" // Specify the column to split the results on + ); + + return result.Distinct().ToList(); } } } \ No newline at end of file diff --git a/Yuna.Website/Yuna.Website.Server/Storage/Repositories/Device/IDeviceRepository.cs b/Yuna.Website/Yuna.Website.Server/Storage/Repositories/Device/IDeviceRepository.cs index be4d62f..7508590 100644 --- a/Yuna.Website/Yuna.Website.Server/Storage/Repositories/Device/IDeviceRepository.cs +++ b/Yuna.Website/Yuna.Website.Server/Storage/Repositories/Device/IDeviceRepository.cs @@ -4,7 +4,7 @@ namespace Yuna.Website.Server.Storage.Repositories.Device { public interface IDeviceRepository { - public Task GetById(long id); + public Task GetById(long id); public Task> GetList(); public Task Create(Model.Device device); //public Task Update(User user);