This commit is contained in:
Vasya Ryzhkoff 2024-07-11 19:52:47 +07:00
parent e3c69229ad
commit 56484b54c8
4 changed files with 62 additions and 19 deletions

View File

@ -17,7 +17,7 @@ namespace Yuna.Website.Server.API
app.MapDelete("/api/device/{id:long}", () => { }) app.MapDelete("/api/device/{id:long}", () => { })
.WithTags("device"); .WithTags("device");
app.MapGet("/api/device/{id:long}", () => { }) app.MapGet("/api/device/{deviceId:long}", GetById)
.WithTags("device"); .WithTags("device");
app.MapGet("/api/device", GetAll) app.MapGet("/api/device", GetAll)
@ -58,6 +58,16 @@ namespace Yuna.Website.Server.API
return Results.Ok(result); 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] [Authorize]
public async Task<IResult> GetAll(IDeviceService deviceService) public async Task<IResult> GetAll(IDeviceService deviceService)
{ {

View File

@ -8,6 +8,7 @@ using Yuna.Website.Server.Services.UserService;
using Yuna.Website.Server.Storage; using Yuna.Website.Server.Storage;
using Yuna.Website.Server.Storage.Repositories.Device; using Yuna.Website.Server.Storage.Repositories.Device;
using Yuna.Website.Server.Storage.Repositories.Prop; using Yuna.Website.Server.Storage.Repositories.Prop;
using Yuna.Website.Server.Storage.Repositories.User;
namespace Yuna.Website.Server namespace Yuna.Website.Server
{ {
@ -131,6 +132,7 @@ namespace Yuna.Website.Server
builder.Services.AddScoped<IDeviceRepository, DeviceRepository>(); builder.Services.AddScoped<IDeviceRepository, DeviceRepository>();
builder.Services.AddScoped<IPropRepository, PropRepository>(); builder.Services.AddScoped<IPropRepository, PropRepository>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
} }
public static void DefineAuth(WebApplicationBuilder builder) public static void DefineAuth(WebApplicationBuilder builder)

View File

@ -41,25 +41,40 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
throw new NotImplementedException(); throw new NotImplementedException();
} }
public async Task<Model.Device> GetById(long id) public async Task<Model.Device?> GetById(long id)
{ {
var query = var query =
$@"SELECT $@"SELECT
d.""Id"" as {nameof(Model.Device.Id)}, d.""Id"" as {nameof(Model.Device.Id)},
d.""Name"" as {nameof(Model.Device.Name)}, d.""Name"" as {nameof(Model.Device.Name)},
d.""Description"" as {nameof(Model.Device.Description)}, d.""Description"" as {nameof(Model.Device.Description)},
p.""Id"" as {nameof(Model.Prop.Id)}, p.""Id"" as {nameof(Model.Prop.Id)},
p.""Name"" as {nameof(Model.Prop.Name)}, p.""Name"" as {nameof(Model.Prop.Name)},
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}, p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)},
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)} p.""MeasureName"" as {nameof(Model.Prop.MeasureName)}
FROM ""Yuna_Devices"" d FROM ""Yuna_Devices"" d
LEFT JOIN ""Yuna_Props_In_Devices"" pd ON d.""Id"" = pd.""DeviceId"" LEFT JOIN ""Yuna_Props_In_Devices"" pd ON d.""Id"" = pd.""DeviceId""
LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id"" LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id""
WHERE d.""Id"" = {id} WHERE d.""Id"" = {id}";
LIMIT 1";
var result = await _context.Connection.QuerySingleAsync<Model.Device>(query); var deviceDict = new Dictionary<long, Model.Device>();
return result; var result = await _context.Connection.QueryAsync<Model.Device, Model.Prop, Model.Device>(
query,
(device, prop) =>
{
if (!deviceDict.TryGetValue(device.Id, out var currentDevice))
{
currentDevice = device;
currentDevice.Props = new List<Model.Prop>();
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<IReadOnlyList<Model.Device>> GetList() public async Task<IReadOnlyList<Model.Device>> 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_In_Devices"" pd ON d.""Id"" = pd.""DeviceId""
LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id"""; LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id""";
var result = await _context.Connection.QueryAsync<Model.Device>(query); var deviceDict = new Dictionary<long, Model.Device>();
return result.ToList(); var result = await _context.Connection.QueryAsync<Model.Device, Model.Prop, Model.Device>(
query,
(device, prop) =>
{
if (!deviceDict.TryGetValue(device.Id, out var currentDevice))
{
currentDevice = device;
currentDevice.Props = new List<Model.Prop>();
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();
} }
} }
} }

View File

@ -4,7 +4,7 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
{ {
public interface IDeviceRepository public interface IDeviceRepository
{ {
public Task<Model.Device> GetById(long id); public Task<Model.Device?> GetById(long id);
public Task<IReadOnlyList<Model.Device>> GetList(); public Task<IReadOnlyList<Model.Device>> GetList();
public Task<Model.Device?> Create(Model.Device device); public Task<Model.Device?> Create(Model.Device device);
//public Task<User?> Update(User user); //public Task<User?> Update(User user);