2016-06-18 14:02:25 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Data;
|
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2016-06-18 14:02:25 -04:00
|
|
|
|
using Dapper;
|
|
|
|
|
|
|
2022-01-11 10:40:51 +01:00
|
|
|
|
namespace Bit.Infrastructure.Dapper.Repositories
|
2016-06-18 14:02:25 -04:00
|
|
|
|
{
|
|
|
|
|
|
public class DeviceRepository : Repository<Device, Guid>, IDeviceRepository
|
|
|
|
|
|
{
|
|
|
|
|
|
public DeviceRepository(GlobalSettings globalSettings)
|
2018-10-09 17:21:12 -04:00
|
|
|
|
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
|
2016-06-18 14:02:25 -04:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2018-10-09 17:21:12 -04:00
|
|
|
|
public DeviceRepository(string connectionString, string readOnlyConnectionString)
|
|
|
|
|
|
: base(connectionString, readOnlyConnectionString)
|
2016-06-18 14:02:25 -04:00
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<Device> GetByIdAsync(Guid id, Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var device = await GetByIdAsync(id);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (device == null || device.UserId != userId)
|
2016-06-18 14:02:25 -04:00
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return device;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-02 16:52:54 -04:00
|
|
|
|
public async Task<Device> GetByIdentifierAsync(string identifier)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-06-02 16:52:54 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Device>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadByIdentifier]",
|
|
|
|
|
|
new
|
|
|
|
|
|
{
|
|
|
|
|
|
Identifier = identifier
|
|
|
|
|
|
},
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.FirstOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-21 00:08:22 -04:00
|
|
|
|
public async Task<Device> GetByIdentifierAsync(string identifier, Guid userId)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2016-06-21 00:08:22 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Device>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadByIdentifierUserId]",
|
|
|
|
|
|
new
|
|
|
|
|
|
{
|
|
|
|
|
|
UserId = userId,
|
|
|
|
|
|
Identifier = identifier
|
|
|
|
|
|
},
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.FirstOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-18 14:02:25 -04:00
|
|
|
|
public async Task<ICollection<Device>> GetManyByUserIdAsync(Guid userId)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2016-06-18 14:02:25 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<Device>(
|
|
|
|
|
|
$"[{Schema}].[{Table}_ReadByUserId]",
|
|
|
|
|
|
new { UserId = userId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-08-06 15:15:11 -04:00
|
|
|
|
|
2017-06-02 16:52:54 -04:00
|
|
|
|
public async Task ClearPushTokenAsync(Guid id)
|
2016-08-06 15:15:11 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2016-08-06 15:15:11 -04:00
|
|
|
|
{
|
|
|
|
|
|
await connection.ExecuteAsync(
|
2017-06-02 16:52:54 -04:00
|
|
|
|
$"[{Schema}].[{Table}_ClearPushTokenById]",
|
|
|
|
|
|
new { Id = id },
|
2016-08-06 15:15:11 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-06-18 14:02:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|