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}", () => { })
.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<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)
{

View File

@ -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<IDeviceRepository, DeviceRepository>();
builder.Services.AddScoped<IPropRepository, PropRepository>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
}
public static void DefineAuth(WebApplicationBuilder builder)

View File

@ -41,7 +41,7 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
throw new NotImplementedException();
}
public async Task<Model.Device> GetById(long id)
public async Task<Model.Device?> GetById(long id)
{
var query =
$@"SELECT
@ -55,11 +55,26 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
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";
WHERE d.""Id"" = {id}";
var result = await _context.Connection.QuerySingleAsync<Model.Device>(query);
return result;
var deviceDict = new Dictionary<long, Model.Device>();
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()
@ -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<Model.Device>(query);
return result.ToList();
var deviceDict = new Dictionary<long, Model.Device>();
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 Task<Model.Device> GetById(long id);
public Task<Model.Device?> GetById(long id);
public Task<IReadOnlyList<Model.Device>> GetList();
public Task<Model.Device?> Create(Model.Device device);
//public Task<User?> Update(User user);