Files
server/src/Infrastructure.Dapper/Repositories/DeviceRepository.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

88 lines
2.6 KiB
C#
Raw Normal View History

using System.Data;
using System.Data.SqlClient;
using Bit.Core.Entities;
using Bit.Core.Repositories;
using Bit.Core.Settings;
using Dapper;
2022-08-29 16:06:55 -04:00
namespace Bit.Infrastructure.Dapper.Repositories;
public class DeviceRepository : Repository<Device, Guid>, IDeviceRepository
{
2022-08-29 16:06:55 -04:00
public DeviceRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
{ }
2022-08-29 16:06:55 -04:00
public DeviceRepository(string connectionString, string readOnlyConnectionString)
: base(connectionString, readOnlyConnectionString)
{ }
2022-08-29 16:06:55 -04:00
public async Task<Device> GetByIdAsync(Guid id, Guid userId)
{
var device = await GetByIdAsync(id);
if (device == null || device.UserId != userId)
{
2022-08-29 16:06:55 -04:00
return null;
}
2022-08-29 14:53:16 -04:00
2022-08-29 16:06:55 -04:00
return device;
}
public async Task<Device> GetByIdentifierAsync(string identifier)
{
using (var connection = new SqlConnection(ConnectionString))
2017-06-02 16:52:54 -04:00
{
2022-08-29 16:06:55 -04:00
var results = await connection.QueryAsync<Device>(
$"[{Schema}].[{Table}_ReadByIdentifier]",
new
{
Identifier = identifier
},
commandType: CommandType.StoredProcedure);
2017-06-02 16:52:54 -04:00
2022-08-29 16:06:55 -04:00
return results.FirstOrDefault();
2017-06-02 16:52:54 -04:00
}
2022-08-29 16:06:55 -04:00
}
2017-06-02 16:52:54 -04:00
2022-08-29 16:06:55 -04:00
public async Task<Device> GetByIdentifierAsync(string identifier, Guid userId)
{
using (var connection = new SqlConnection(ConnectionString))
{
2022-08-29 16:06:55 -04:00
var results = await connection.QueryAsync<Device>(
$"[{Schema}].[{Table}_ReadByIdentifierUserId]",
new
{
UserId = userId,
Identifier = identifier
},
commandType: CommandType.StoredProcedure);
2022-08-29 16:06:55 -04:00
return results.FirstOrDefault();
}
2022-08-29 16:06:55 -04:00
}
2022-08-29 16:06:55 -04:00
public async Task<ICollection<Device>> GetManyByUserIdAsync(Guid userId)
{
using (var connection = new SqlConnection(ConnectionString))
{
2022-08-29 16:06:55 -04:00
var results = await connection.QueryAsync<Device>(
$"[{Schema}].[{Table}_ReadByUserId]",
new { UserId = userId },
commandType: CommandType.StoredProcedure);
2022-08-29 16:06:55 -04:00
return results.ToList();
}
2022-08-29 16:06:55 -04:00
}
2022-08-29 16:06:55 -04:00
public async Task ClearPushTokenAsync(Guid id)
{
using (var connection = new SqlConnection(ConnectionString))
{
2022-08-29 16:06:55 -04:00
await connection.ExecuteAsync(
$"[{Schema}].[{Table}_ClearPushTokenById]",
new { Id = id },
commandType: CommandType.StoredProcedure);
}
}
}