Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface IProviderRepository : IRepository<Provider, Guid>
Task<Provider?> GetByOrganizationIdAsync(Guid organizationId);
Task<ICollection<Provider>> SearchAsync(string name, string userEmail, int skip, int take);
Task<ICollection<ProviderAbility>> GetManyAbilitiesAsync();
Task<ProviderAbility?> GetAbilityAsync(Guid id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,16 @@ public async Task<ICollection<ProviderAbility>> GetManyAbilitiesAsync()
return results.ToList();
}
}

public async Task<ProviderAbility?> GetAbilityAsync(Guid id)
{
await using var connection = new SqlConnection(ReadOnlyConnectionString);

var results = await connection.QueryAsync<ProviderAbility>(
"[dbo].[Provider_ReadAbilityById]",
new { Id = id },
commandType: CommandType.StoredProcedure);

return results.FirstOrDefault();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,21 @@ public async Task<ICollection<ProviderAbility>> GetManyAbilitiesAsync()
}).ToListAsync();
}
}

#nullable enable
public async Task<ProviderAbility?> GetAbilityAsync(Guid id)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
return await GetDbSet(dbContext)
.Where(e => e.Id == id)
.Select(e => new ProviderAbility
{
Id = e.Id,
UseEvents = e.UseEvents,
Enabled = e.Enabled,
}).FirstOrDefaultAsync();
}
}
}
15 changes: 15 additions & 0 deletions src/Sql/dbo/Stored Procedures/Provider_ReadAbilityById.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE PROCEDURE [dbo].[Provider_ReadAbilityById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON

SELECT
[Id],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine but you might want to change it to SELECT * to match the contributing docs.

[UseEvents],
[Enabled]
FROM
[dbo].[ProviderAbilityView]
WHERE
[Id] = @Id
END
8 changes: 8 additions & 0 deletions src/Sql/dbo/Views/ProviderAbilityView.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE VIEW [dbo].[ProviderAbilityView]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Is this view necessary? We already have a view called ProviderView that contains all of the columns in the Provider table. Can you just select the columns you need from ProviderView in your stored procedure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done according to our contributing docs.

AS
SELECT
[Id],
[UseEvents],
[Enabled]
FROM
[dbo].[Provider]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
ο»Ώusing Bit.Core.AdminConsole.Models.Data.Provider;
using Xunit;

using ProviderEntity = Bit.Core.AdminConsole.Entities.Provider.Provider;

namespace Bit.Core.Test.AdminConsole.Models.Data.Provider;

public class ProviderAbilityTests
{
[Fact]
public void Constructor_MapsAllProperties()
{
var provider = new ProviderEntity
{
Id = Guid.NewGuid(),
UseEvents = true,
Enabled = true,
};

var ability = new ProviderAbility(provider);

Assert.Equal(provider.Id, ability.Id);
Assert.Equal(provider.UseEvents, ability.UseEvents);
Assert.Equal(provider.Enabled, ability.Enabled);
}

[Fact]
public void Constructor_DefaultValues()
{
var provider = new ProviderEntity
{
Id = Guid.NewGuid(),
UseEvents = false,
Enabled = false,
};

var ability = new ProviderAbility(provider);

Assert.Equal(provider.Id, ability.Id);
Assert.False(ability.UseEvents);
Assert.False(ability.Enabled);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CREATE OR ALTER VIEW [dbo].[ProviderAbilityView]
AS
SELECT
[Id],
[UseEvents],
[Enabled]
FROM
[dbo].[Provider]
GO

CREATE OR ALTER PROCEDURE [dbo].[Provider_ReadAbilityById]
@Id UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON

SELECT
[Id],
[UseEvents],
[Enabled]
FROM
[dbo].[ProviderAbilityView]
WHERE
[Id] = @Id
END
GO
Loading