2017-03-07 23:06:14 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Data;
|
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
|
using System.Linq;
|
2017-05-09 12:41:36 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
2017-05-11 12:22:14 -04:00
|
|
|
|
using Bit.Core.Models.Data;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Repositories;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
|
|
|
|
|
using Bit.Core.Utilities;
|
2021-12-16 15:35:09 +01:00
|
|
|
|
using Dapper;
|
2017-05-09 12:41:36 -04:00
|
|
|
|
using Newtonsoft.Json;
|
2017-03-07 23:06:14 -05:00
|
|
|
|
|
2022-01-11 10:40:51 +01:00
|
|
|
|
namespace Bit.Infrastructure.Dapper.Repositories
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public class CollectionRepository : Repository<Collection, Guid>, ICollectionRepository
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public CollectionRepository(GlobalSettings globalSettings)
|
2018-10-09 17:21:12 -04:00
|
|
|
|
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2018-10-09 17:21:12 -04:00
|
|
|
|
public CollectionRepository(string connectionString, string readOnlyConnectionString)
|
|
|
|
|
|
: base(connectionString, readOnlyConnectionString)
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2017-04-07 16:41:04 -04:00
|
|
|
|
public async Task<int> GetCountByOrganizationIdAsync(Guid organizationId)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-04-07 16:41:04 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.ExecuteScalarAsync<int>(
|
2017-04-27 09:19:30 -04:00
|
|
|
|
"[dbo].[Collection_ReadCountByOrganizationId]",
|
2017-04-07 16:41:04 -04:00
|
|
|
|
new { OrganizationId = organizationId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-11 12:22:14 -04:00
|
|
|
|
public async Task<Tuple<Collection, ICollection<SelectionReadOnly>>> GetByIdWithGroupsAsync(Guid id)
|
2017-05-09 12:41:36 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-05-09 12:41:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryMultipleAsync(
|
|
|
|
|
|
$"[{Schema}].[Collection_ReadWithGroupsById]",
|
|
|
|
|
|
new { Id = id },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
var collection = await results.ReadFirstOrDefaultAsync<Collection>();
|
2017-05-11 12:22:14 -04:00
|
|
|
|
var groups = (await results.ReadAsync<SelectionReadOnly>()).ToList();
|
2017-05-09 12:41:36 -04:00
|
|
|
|
|
2017-05-11 12:22:14 -04:00
|
|
|
|
return new Tuple<Collection, ICollection<SelectionReadOnly>>(collection, groups);
|
2017-05-09 12:41:36 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-17 14:58:45 -04:00
|
|
|
|
public async Task<Tuple<CollectionDetails, ICollection<SelectionReadOnly>>> GetByIdWithGroupsAsync(
|
|
|
|
|
|
Guid id, Guid userId)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2018-10-17 14:58:45 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryMultipleAsync(
|
|
|
|
|
|
$"[{Schema}].[Collection_ReadWithGroupsByIdUserId]",
|
|
|
|
|
|
new { Id = id, UserId = userId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
var collection = await results.ReadFirstOrDefaultAsync<CollectionDetails>();
|
|
|
|
|
|
var groups = (await results.ReadAsync<SelectionReadOnly>()).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
return new Tuple<CollectionDetails, ICollection<SelectionReadOnly>>(collection, groups);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public async Task<ICollection<Collection>> GetManyByOrganizationIdAsync(Guid organizationId)
|
2017-03-09 23:58:43 -05:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-03-09 23:58:43 -05:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
var results = await connection.QueryAsync<Collection>(
|
2017-03-09 23:58:43 -05:00
|
|
|
|
$"[{Schema}].[{Table}_ReadByOrganizationId]",
|
|
|
|
|
|
new { OrganizationId = organizationId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-17 14:58:45 -04:00
|
|
|
|
public async Task<CollectionDetails> GetByIdAsync(Guid id, Guid userId)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2018-10-17 14:58:45 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<CollectionDetails>(
|
|
|
|
|
|
$"[{Schema}].[Collection_ReadByIdUserId]",
|
|
|
|
|
|
new { Id = id, UserId = userId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.FirstOrDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-06-11 14:25:53 -04:00
|
|
|
|
public async Task<ICollection<CollectionDetails>> GetManyByUserIdAsync(Guid userId)
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-03-07 23:06:14 -05:00
|
|
|
|
{
|
2018-06-11 14:25:53 -04:00
|
|
|
|
var results = await connection.QueryAsync<CollectionDetails>(
|
2017-05-11 10:32:25 -04:00
|
|
|
|
$"[{Schema}].[Collection_ReadByUserId]",
|
2018-06-11 14:25:53 -04:00
|
|
|
|
new { UserId = userId },
|
2017-03-07 23:06:14 -05:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
2021-08-02 11:49:27 +10:00
|
|
|
|
return results.ToList();
|
2017-03-07 23:06:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-05-09 12:41:36 -04:00
|
|
|
|
|
2017-05-11 12:22:14 -04:00
|
|
|
|
public async Task CreateAsync(Collection obj, IEnumerable<SelectionReadOnly> groups)
|
2017-05-09 12:41:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
obj.SetNewId();
|
|
|
|
|
|
var objWithGroups = JsonConvert.DeserializeObject<CollectionWithGroups>(JsonConvert.SerializeObject(obj));
|
2017-05-11 12:22:14 -04:00
|
|
|
|
objWithGroups.Groups = groups.ToArrayTVP();
|
2017-05-09 12:41:36 -04:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-05-09 12:41:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.ExecuteAsync(
|
2018-10-25 07:46:58 -04:00
|
|
|
|
$"[{Schema}].[Collection_CreateWithGroups]",
|
2017-05-09 12:41:36 -04:00
|
|
|
|
objWithGroups,
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-11 12:22:14 -04:00
|
|
|
|
public async Task ReplaceAsync(Collection obj, IEnumerable<SelectionReadOnly> groups)
|
2017-05-09 12:41:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
var objWithGroups = JsonConvert.DeserializeObject<CollectionWithGroups>(JsonConvert.SerializeObject(obj));
|
2017-05-11 12:22:14 -04:00
|
|
|
|
objWithGroups.Groups = groups.ToArrayTVP();
|
2017-05-09 12:41:36 -04:00
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-05-09 12:41:36 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.ExecuteAsync(
|
|
|
|
|
|
$"[{Schema}].[Collection_UpdateWithGroups]",
|
|
|
|
|
|
objWithGroups,
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-17 14:58:45 -04:00
|
|
|
|
public async Task CreateUserAsync(Guid collectionId, Guid organizationUserId)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2018-10-17 14:58:45 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.ExecuteAsync(
|
|
|
|
|
|
$"[{Schema}].[CollectionUser_Create]",
|
|
|
|
|
|
new { CollectionId = collectionId, OrganizationUserId = organizationUserId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-11 14:52:35 -04:00
|
|
|
|
public async Task DeleteUserAsync(Guid collectionId, Guid organizationUserId)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-05-11 14:52:35 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.ExecuteAsync(
|
|
|
|
|
|
$"[{Schema}].[CollectionUser_Delete]",
|
|
|
|
|
|
new { CollectionId = collectionId, OrganizationUserId = organizationUserId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-17 14:58:45 -04:00
|
|
|
|
public async Task UpdateUsersAsync(Guid id, IEnumerable<SelectionReadOnly> users)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2018-10-17 14:58:45 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.ExecuteAsync(
|
2018-10-17 22:18:03 -04:00
|
|
|
|
$"[{Schema}].[CollectionUser_UpdateUsers]",
|
2018-10-18 08:38:22 -04:00
|
|
|
|
new { CollectionId = id, Users = users.ToArrayTVP() },
|
2018-10-17 14:58:45 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-17 22:18:03 -04:00
|
|
|
|
public async Task<ICollection<SelectionReadOnly>> GetManyUsersByIdAsync(Guid id)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2018-10-17 22:18:03 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.QueryAsync<SelectionReadOnly>(
|
|
|
|
|
|
$"[{Schema}].[CollectionUser_ReadByCollectionId]",
|
|
|
|
|
|
new { CollectionId = id },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-09 12:41:36 -04:00
|
|
|
|
public class CollectionWithGroups : Collection
|
|
|
|
|
|
{
|
2017-05-11 12:22:14 -04:00
|
|
|
|
public DataTable Groups { get; set; }
|
2017-05-09 12:41:36 -04:00
|
|
|
|
}
|
2017-03-07 23:06:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|