Добавьте файлы проекта.
This commit is contained in:
19
Yuna.Website/Yuna.Website.Server/Storage/AppDbContext.cs
Normal file
19
Yuna.Website/Yuna.Website.Server/Storage/AppDbContext.cs
Normal file
@ -0,0 +1,19 @@
|
||||
//using Microsoft.EntityFrameworkCore;
|
||||
//using Yuna.Website.Server.Model;
|
||||
//using Yuna.Website.Server.Storage.StorageModel;
|
||||
//using Yuna.Website.Server.Storage.StorageModel.Skill;
|
||||
|
||||
//namespace Yuna.Website.Server.Storage
|
||||
//{
|
||||
// public class AppDbContext : DbContext
|
||||
// {
|
||||
// public AppDbContext()
|
||||
// {
|
||||
// }
|
||||
|
||||
// //public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
|
||||
// //public virtual DbSet<UserInStorage> Users => Set<UserInStorage>();
|
||||
// //public virtual DbSet<SkillInStorage> Skills => Set<SkillInStorage>();
|
||||
// //public virtual DbSet<DeviceInStorage> Devices => Set<DeviceInStorage>();
|
||||
// }
|
||||
//}
|
34
Yuna.Website/Yuna.Website.Server/Storage/DapperContext.cs
Normal file
34
Yuna.Website/Yuna.Website.Server/Storage/DapperContext.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Npgsql;
|
||||
using System.Data;
|
||||
using Yuna.Website.Server.Infrastructure;
|
||||
|
||||
namespace Yuna.Website.Server.Storage
|
||||
{
|
||||
public class DapperContext
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
public DapperContext(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
public DapperContext(bool isTest)
|
||||
{
|
||||
_configuration = null!;
|
||||
}
|
||||
|
||||
private IDbConnection? _dbConnection;
|
||||
public IDbConnection Connection
|
||||
{
|
||||
get
|
||||
{
|
||||
|
||||
if (_dbConnection is null) _dbConnection = new NpgsqlConnection(Settings.DbConnectionStr);
|
||||
return _dbConnection;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
using Dapper;
|
||||
using System.Data;
|
||||
|
||||
namespace Yuna.Website.Server.Storage.Repositories.Device
|
||||
{
|
||||
public class DeviceRepository : IDeviceRepository
|
||||
{
|
||||
private readonly DapperContext _context;
|
||||
|
||||
public DeviceRepository(DapperContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task AddProps(IReadOnlyList<Model.Prop> props, long deviceId)
|
||||
{
|
||||
var query =
|
||||
$@"INSERT INTO ""Yuna_Props_In_Devices""
|
||||
(""PropId"", ""DeviceId"")
|
||||
VALUES
|
||||
{string.Join(", ", props.Select(prop => $"({prop.Id}, {deviceId})"))}";
|
||||
|
||||
await _context.Connection.ExecuteAsync(query);
|
||||
}
|
||||
|
||||
public async Task<Model.Device?> Create(Model.Device device)
|
||||
{
|
||||
var query =
|
||||
$@"INSERT INTO ""Yuna_Devices""
|
||||
(""Name"", ""Description"", ""DeviceUrl"")
|
||||
VALUES
|
||||
('{device.Name}', '{device.Description}', '{device.DeviceUrl}')
|
||||
RETURNING *";
|
||||
|
||||
var result = await _context.Connection.QuerySingleAsync<Model.Device?>(query, device);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<Model.Device?> Delete(long id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<Model.Device> 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";
|
||||
|
||||
var result = await _context.Connection.QuerySingleAsync<Model.Device>(query);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<Model.Device>> GetList()
|
||||
{
|
||||
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""";
|
||||
|
||||
var result = await _context.Connection.QueryAsync<Model.Device>(query);
|
||||
return result.ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using Yuna.Website.Server.Model;
|
||||
|
||||
namespace Yuna.Website.Server.Storage.Repositories.Device
|
||||
{
|
||||
public interface IDeviceRepository
|
||||
{
|
||||
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);
|
||||
public Task<Model.Device?> Delete(long id);
|
||||
public Task AddProps(IReadOnlyList<Model.Prop> skills, long deviceId);
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
namespace Yuna.Website.Server.Storage.Repositories.Prop
|
||||
{
|
||||
public interface IPropRepository
|
||||
{
|
||||
public Task<Model.Prop?> GetById(long id);
|
||||
public Task<Model.Prop?> GetByPropName(string value);
|
||||
public Task<IReadOnlyList<Model.Prop>> GetList();
|
||||
public Task<Model.Prop?> Create(Model.Prop value);
|
||||
//public Task<User?> Update(User user);
|
||||
public Task<Model.Prop?> Delete(long id);
|
||||
public Task<IReadOnlyList<Model.Prop>?> GetByIds(long[] ids);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
using Dapper;
|
||||
|
||||
namespace Yuna.Website.Server.Storage.Repositories.Prop
|
||||
{
|
||||
public class PropRepository : IPropRepository
|
||||
{
|
||||
private readonly DapperContext _context;
|
||||
|
||||
public PropRepository(DapperContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<Model.Prop?> Create(Model.Prop value)
|
||||
{
|
||||
var query =
|
||||
$@"INSERT INTO ""Yuna_Props""
|
||||
(""Name"", ""MeasureName"", ""JsonValueName"")
|
||||
VALUES
|
||||
(@{nameof(Model.Prop.Name)}, @{nameof(Model.Prop.MeasureName)}, @{nameof(Model.Prop.JsonValueName)})
|
||||
RETURNING *";
|
||||
|
||||
var result = await _context.Connection.QuerySingleAsync<Model.Prop?>(query, value);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<Model.Prop?> Delete(long id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<Model.Prop?> GetById(long id)
|
||||
{
|
||||
var query =
|
||||
$@"SELECT
|
||||
p.""Id"" as {nameof(Model.Prop.Id)},
|
||||
p.""Name"" as {nameof(Model.Prop.Name)},
|
||||
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
|
||||
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}
|
||||
FROM ""Yuna_Props"" p
|
||||
WHERE p.""Id"" = {id}
|
||||
LIMIT 1";
|
||||
|
||||
var result = await _context.Connection.QuerySingleOrDefaultAsync<Model.Prop>(query);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<Model.Prop>?> GetByIds(long[] ids)
|
||||
{
|
||||
var idList = string.Join(", ", ids);
|
||||
var query =
|
||||
$@"SELECT
|
||||
p.""Id"" as {nameof(Model.Prop.Id)},
|
||||
p.""Name"" as {nameof(Model.Prop.Name)},
|
||||
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
|
||||
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}
|
||||
FROM ""Yuna_Props"" p
|
||||
WHERE p.""Id"" IN ({idList})";
|
||||
|
||||
var result = await _context.Connection.QueryAsync<Model.Prop>(query);
|
||||
if (result.Count() != ids.Length) return null;
|
||||
return result.ToList();
|
||||
}
|
||||
|
||||
public async Task<Model.Prop?> GetByPropName(string value)
|
||||
{
|
||||
var query =
|
||||
$@"SELECT
|
||||
p.""Id"" as {nameof(Model.Prop.Id)},
|
||||
p.""Name"" as {nameof(Model.Prop.Name)},
|
||||
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
|
||||
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}
|
||||
FROM ""Yuna_Props"" p
|
||||
WHERE LOWER(p.""Name"") = '{value.ToLower()}'
|
||||
LIMIT 1";
|
||||
|
||||
var result = await _context.Connection.QuerySingleOrDefaultAsync<Model.Prop>(query);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<Model.Prop>> GetList()
|
||||
{
|
||||
var query =
|
||||
$@"SELECT
|
||||
p.""Id"" as {nameof(Model.Prop.Id)},
|
||||
p.""Name"" as {nameof(Model.Prop.Name)},
|
||||
p.""MeasureName"" as {nameof(Model.Prop.MeasureName)},
|
||||
p.""JsonValueName"" as {nameof(Model.Prop.JsonValueName)}
|
||||
FROM ""Yuna_Props"" p";
|
||||
|
||||
var result = await _context.Connection.QueryAsync<Model.Prop>(query);
|
||||
return result.ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
namespace Yuna.Website.Server.Storage.Repositories.User
|
||||
{
|
||||
public interface IUserRepository
|
||||
{
|
||||
public Task<Model.User?> GetById(long id);
|
||||
public Task<Model.User?> GetByUsername(string username);
|
||||
public Task<IReadOnlyList<Model.User>> GetList();
|
||||
public Task<Model.User?> Create(Model.User? user);
|
||||
public Task<Model.User?> Update(Model.User? user);
|
||||
public Task<Model.User?> Delete(long id);
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
|
||||
using Dapper;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Yuna.Website.Server.Model;
|
||||
|
||||
namespace Yuna.Website.Server.Storage.Repositories.User
|
||||
{
|
||||
public class UserRepository : IUserRepository
|
||||
{
|
||||
private readonly DapperContext _context;
|
||||
|
||||
public UserRepository(DapperContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
public async Task<Model.User?> Create(Model.User? user)
|
||||
{
|
||||
var query =
|
||||
$@"INSERT INTO ""Yuna_Users""
|
||||
(""Username"", ""HashedPassword"", ""IsAdmin"")
|
||||
VALUES
|
||||
(@{nameof(Model.User.UserName)}, @{nameof(Model.User.HashedPassword)}, @{nameof(Model.User.IsAdmin)})
|
||||
RETURNING *
|
||||
";
|
||||
|
||||
var result = await _context.Connection.QuerySingleAsync<Model.User?>(query, user);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Task<Model.User?> Delete(long id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<Model.User?> GetById(long id)
|
||||
{
|
||||
var query =
|
||||
$@"SELECT
|
||||
u.""Id"" as {nameof(Model.User.Id)},
|
||||
u.""Username"" as {nameof(Model.User.UserName)},
|
||||
u.""HashedPassword"" as {nameof(Model.User.HashedPassword)},
|
||||
u.""IsAdmin"" as {nameof(Model.User.IsAdmin)}
|
||||
FROM ""Yuna_Users"" u
|
||||
WHERE u.""Id"" = {id}
|
||||
LIMIT 1";
|
||||
|
||||
var result = await _context.Connection.QuerySingleAsync<Model.User>(query);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Model.User?> GetByUsername(string username)
|
||||
{
|
||||
var query =
|
||||
$@"SELECT
|
||||
u.""Id"" as {nameof(Model.User.Id)},
|
||||
u.""Username"" as {nameof(Model.User.UserName)},
|
||||
u.""HashedPassword"" as {nameof(Model.User.HashedPassword)},
|
||||
u.""IsAdmin"" as {nameof(Model.User.IsAdmin)}
|
||||
FROM ""Yuna_Users"" u
|
||||
WHERE LOWER(u.""Username"") = '{username.ToLower()}'
|
||||
LIMIT 1";
|
||||
|
||||
var result = await _context.Connection.QuerySingleOrDefaultAsync<Model.User>(query);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<IReadOnlyList<Model.User>> GetList()
|
||||
{
|
||||
var query =
|
||||
$@"SELECT
|
||||
u.""Id"" as {nameof(Model.User.Id)},
|
||||
u.""Username"" as {nameof(Model.User.UserName)},
|
||||
u.""HashedPassword"" as {nameof(Model.User.HashedPassword)},
|
||||
u.""IsAdmin"" as {nameof(Model.User.IsAdmin)}
|
||||
FROM ""Yuna_Users"" u";
|
||||
|
||||
var result = await _context.Connection.QueryAsync<Model.User>(query);
|
||||
return result.ToList();
|
||||
}
|
||||
|
||||
public async Task<Model.User?> Update(Model.User? user)
|
||||
{
|
||||
var query = $@"
|
||||
UPDATE ""Yuna_Users""
|
||||
SET
|
||||
""Username"" = @{nameof(Model.User.UserName)},
|
||||
""HashedPassword"" = @{nameof(Model.User.HashedPassword)},
|
||||
""IsAdmin"" = {nameof(Model.User.IsAdmin)}
|
||||
""WHERE Id"" = @{nameof(Model.User.Id)}
|
||||
RETURNING *
|
||||
";
|
||||
|
||||
var result = await _context.Connection.QuerySingleOrDefaultAsync<Model.User>(query, user);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user