creae update devices

This commit is contained in:
2024-07-23 01:43:13 +07:00
parent d9c3bfa35e
commit 65b924c174
23 changed files with 359 additions and 32 deletions

View File

@ -1,5 +1,6 @@
using Dapper;
using System.Data;
using Yuna.Website.Server.Model;
namespace Yuna.Website.Server.Storage.Repositories.Device
{
@ -36,9 +37,18 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
return result;
}
public Task<Model.Device?> Delete(long id)
public async Task<Model.Device?> Delete(long id)
{
throw new NotImplementedException();
var query =
$@"
UPDATE ""Yuna_Devices""
SET ""IsDeleted"" = TRUE
WHERE ""Id"" = {id}
AND NOT ""IsDeleted""
returning *
";
return await _context.Connection.QuerySingleOrDefaultAsync<Model.Device>(query);
}
public async Task<Model.Device?> GetById(long id)
@ -58,7 +68,8 @@ 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}";
WHERE d.""Id"" = {id}
AND NOT d.""IsDeleted""";
var deviceDict = new Dictionary<long, Model.Device>();
var result = await _context.Connection.QueryAsync<Model.Device, Model.Prop, Model.Device>(
@ -96,7 +107,9 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
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""";
LEFT JOIN ""Yuna_Props"" p ON pd.""PropId"" = p.""Id""
WHERE NOT d.""IsDeleted""
ORDER BY d.""Id""";
var deviceDict = new Dictionary<long, Model.Device>();
var result = await _context.Connection.QueryAsync<Model.Device, Model.Prop, Model.Device>(
@ -135,7 +148,9 @@ 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.""UserId"" = {userId};";
WHERE d.""UserId"" = {userId}
AND NOT d.""IsDeleted""
ORDER BY d.""Id""";
var deviceDict = new Dictionary<long, Model.Device>();
var result = await _context.Connection.QueryAsync<Model.Device, Model.Prop, Model.Device>(
@ -156,5 +171,22 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
return result.Distinct().ToList();
}
public async Task<Model.Device?> Update(Model.Device device)
{
var query =
$@"
UPDATE public.""Yuna_Devices""
SET ""Name"" = @Name,
""Description"" = @Description,
""DeviceUrl"" = @DeviceUrl,
""UserId"" = @UserId
WHERE ""Id"" = @Id
AND NOT ""IsDeleted""
returning *
";
return await _context.Connection.QuerySingleOrDefaultAsync<Model.Device>(query, device);
}
}
}

View File

@ -8,7 +8,7 @@ namespace Yuna.Website.Server.Storage.Repositories.Device
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?> Update(Model.Device device);
public Task<Model.Device?> Delete(long id);
public Task AddProps(IReadOnlyList<Model.Prop> skills, long deviceId);
}