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

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

147 lines
5.7 KiB
C#
Raw Normal View History

2017-05-08 14:08:44 -04:00
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text.Json;
2017-05-08 22:14:01 -04:00
using System.Threading.Tasks;
using Bit.Core.Entities;
2017-05-09 19:04:01 -04:00
using Bit.Core.Models.Data;
using Bit.Core.Repositories;
using Bit.Core.Settings;
using Bit.Core.Utilities;
2017-05-08 14:08:44 -04:00
using Dapper;
namespace Bit.Infrastructure.Dapper.Repositories
2017-05-08 14:08:44 -04:00
{
public class GroupRepository : Repository<Group, Guid>, IGroupRepository
{
public GroupRepository(GlobalSettings globalSettings)
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
2017-05-08 14:08:44 -04:00
{ }
public GroupRepository(string connectionString, string readOnlyConnectionString)
: base(connectionString, readOnlyConnectionString)
2017-05-08 14:08:44 -04:00
{ }
public async Task<Tuple<Group, ICollection<SelectionReadOnly>>> GetByIdWithCollectionsAsync(Guid id)
2017-05-08 22:14:01 -04:00
{
using (var connection = new SqlConnection(ConnectionString))
2017-05-08 22:14:01 -04:00
{
var results = await connection.QueryMultipleAsync(
$"[{Schema}].[Group_ReadWithCollectionsById]",
new { Id = id },
commandType: CommandType.StoredProcedure);
var group = await results.ReadFirstOrDefaultAsync<Group>();
var colletions = (await results.ReadAsync<SelectionReadOnly>()).ToList();
2017-05-08 22:14:01 -04:00
return new Tuple<Group, ICollection<SelectionReadOnly>>(group, colletions);
2017-05-08 22:14:01 -04:00
}
}
2017-05-08 14:08:44 -04:00
public async Task<ICollection<Group>> GetManyByOrganizationIdAsync(Guid organizationId)
{
using (var connection = new SqlConnection(ConnectionString))
2017-05-08 14:08:44 -04:00
{
var results = await connection.QueryAsync<Group>(
$"[{Schema}].[Group_ReadByOrganizationId]",
new { OrganizationId = organizationId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
2017-05-08 22:14:01 -04:00
2018-10-18 08:38:22 -04:00
public async Task<ICollection<Guid>> GetManyIdsByUserIdAsync(Guid organizationUserId)
2017-05-09 19:04:01 -04:00
{
using (var connection = new SqlConnection(ConnectionString))
2017-05-09 19:04:01 -04:00
{
2018-10-18 08:38:22 -04:00
var results = await connection.QueryAsync<Guid>(
$"[{Schema}].[GroupUser_ReadGroupIdsByOrganizationUserId]",
new { OrganizationUserId = organizationUserId },
2017-05-09 19:04:01 -04:00
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
2018-10-18 08:38:22 -04:00
public async Task<ICollection<Guid>> GetManyUserIdsByIdAsync(Guid id)
2017-05-09 19:04:01 -04:00
{
using (var connection = new SqlConnection(ConnectionString))
2017-05-09 19:04:01 -04:00
{
var results = await connection.QueryAsync<Guid>(
2018-10-18 08:38:22 -04:00
$"[{Schema}].[GroupUser_ReadOrganizationUserIdsByGroupId]",
new { GroupId = id },
2017-05-09 19:04:01 -04:00
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
public async Task<ICollection<GroupUser>> GetManyGroupUsersByOrganizationIdAsync(Guid organizationId)
{
using (var connection = new SqlConnection(ConnectionString))
{
var results = await connection.QueryAsync<GroupUser>(
$"[{Schema}].[GroupUser_ReadByOrganizationId]",
new { OrganizationId = organizationId },
commandType: CommandType.StoredProcedure);
return results.ToList();
}
}
public async Task CreateAsync(Group obj, IEnumerable<SelectionReadOnly> collections)
2017-05-08 22:14:01 -04:00
{
obj.SetNewId();
var objWithCollections = JsonSerializer.Deserialize<GroupWithCollections>(JsonSerializer.Serialize(obj));
objWithCollections.Collections = collections.ToArrayTVP();
2017-05-08 22:14:01 -04:00
using (var connection = new SqlConnection(ConnectionString))
2017-05-08 22:14:01 -04:00
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[Group_CreateWithCollections]",
objWithCollections,
commandType: CommandType.StoredProcedure);
}
}
public async Task ReplaceAsync(Group obj, IEnumerable<SelectionReadOnly> collections)
2017-05-08 22:14:01 -04:00
{
var objWithCollections = JsonSerializer.Deserialize<GroupWithCollections>(JsonSerializer.Serialize(obj));
objWithCollections.Collections = collections.ToArrayTVP();
2017-05-08 22:14:01 -04:00
using (var connection = new SqlConnection(ConnectionString))
2017-05-08 22:14:01 -04:00
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[Group_UpdateWithCollections]",
objWithCollections,
commandType: CommandType.StoredProcedure);
}
}
2017-05-09 19:24:03 -04:00
public async Task DeleteUserAsync(Guid groupId, Guid organizationUserId)
{
using (var connection = new SqlConnection(ConnectionString))
2017-05-09 19:24:03 -04:00
{
var results = await connection.ExecuteAsync(
$"[{Schema}].[GroupUser_Delete]",
new { GroupId = groupId, OrganizationUserId = organizationUserId },
commandType: CommandType.StoredProcedure);
}
}
2017-05-15 14:41:20 -04:00
public async Task UpdateUsersAsync(Guid groupId, IEnumerable<Guid> organizationUserIds)
{
using (var connection = new SqlConnection(ConnectionString))
2017-05-15 14:41:20 -04:00
{
var results = await connection.ExecuteAsync(
"[dbo].[GroupUser_UpdateUsers]",
new { GroupId = groupId, OrganizationUserIds = organizationUserIds.ToGuidIdArrayTVP() },
commandType: CommandType.StoredProcedure);
}
}
2017-05-08 14:08:44 -04:00
}
}