This commit is contained in:
2024-07-18 03:13:32 +07:00
parent d2eee41ed6
commit d9c3bfa35e
49 changed files with 3037 additions and 232 deletions

View File

@ -19,16 +19,11 @@ namespace Yuna.Website.Server.Storage
_configuration = null!;
}
private IDbConnection? _dbConnection;
public IDbConnection Connection
{
get
{
public IDbConnection Connection => new NpgsqlConnection(Settings.DbConnectionStr);
if (_dbConnection is null) _dbConnection = new NpgsqlConnection(Settings.DbConnectionStr);
return _dbConnection;
}
}
}
}

View File

@ -27,9 +27,9 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
{
var query =
$@"INSERT INTO ""Yuna_Devices""
(""Name"", ""Description"", ""DeviceUrl"")
(""Name"", ""Description"", ""DeviceUrl"", ""UserId"")
VALUES
('{device.Name}', '{device.Description}', '{device.DeviceUrl}')
('{device.Name}', '{device.Description}', '{device.DeviceUrl}', {device.UserId})
RETURNING *";
var result = await _context.Connection.QuerySingleAsync<Model.Device?>(query, device);
@ -49,6 +49,7 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
d.""Name"" as {nameof(Model.Device.Name)},
d.""Description"" as {nameof(Model.Device.Description)},
d.""DeviceUrl"" as {nameof(Model.Device.DeviceUrl)},
d.""UserId"" as {nameof(Model.Device.UserId)},
p.""Id"" as {nameof(Model.Prop.Id)},
p.""Name"" as {nameof(Model.Prop.Name)},
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)},
@ -87,6 +88,7 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
d.""Name"" as {nameof(Model.Device.Name)},
d.""Description"" as {nameof(Model.Device.Description)},
d.""DeviceUrl"" as {nameof(Model.Device.DeviceUrl)},
d.""UserId"" as {nameof(Model.Device.UserId)},
p.""Id"" as {nameof(Model.Prop.Id)},
p.""Name"" as {nameof(Model.Prop.Name)},
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)},
@ -107,7 +109,46 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
currentDevice.Props = new List<Model.Prop>();
deviceDict.Add(currentDevice.Id, currentDevice);
}
currentDevice.Props.Add(prop);
if(prop is not null) currentDevice.Props.Add(prop);
return currentDevice;
},
splitOn: $"{nameof(Model.Prop.Id)}" // Specify the column to split the results on
);
return result.Distinct().ToList();
}
public async Task<IReadOnlyList<Model.Device>> GetList(long userId)
{
var query =
$@"SELECT
d.""Id"" as {nameof(Model.Device.Id)},
d.""Name"" as {nameof(Model.Device.Name)},
d.""Description"" as {nameof(Model.Device.Description)},
d.""DeviceUrl"" as {nameof(Model.Device.DeviceUrl)},
d.""UserId"" as {nameof(Model.Device.UserId)},
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)},
p.""Type"" as {nameof(Model.Prop.Type)}
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.""UserId"" = {userId};";
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);
}
if (prop is not null) currentDevice.Props.Add(prop);
return currentDevice;
},
splitOn: $"{nameof(Model.Prop.Id)}" // Specify the column to split the results on

View File

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