1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-28 14:46:14 -05:00

Refactor CollectionsController to use command interfaces for collection creation and updates

* Updated CollectionsController to utilize ICreateCollectionCommand and IUpdateCollectionCommand for handling collection creation and updates, replacing calls to ICollectionService.
* Adjusted related unit tests to verify the new command implementations.
This commit is contained in:
Rui Tome 2025-06-12 14:48:23 +01:00
parent c7f3db3047
commit ba9fee9ca5
No known key found for this signature in database
GPG Key ID: 526239D96A8EC066
3 changed files with 19 additions and 11 deletions

View File

@ -20,6 +20,8 @@ public class CollectionsController : Controller
{
private readonly ICollectionRepository _collectionRepository;
private readonly ICollectionService _collectionService;
private readonly ICreateCollectionCommand _createCollectionCommand;
private readonly IUpdateCollectionCommand _updateCollectionCommand;
private readonly IDeleteCollectionCommand _deleteCollectionCommand;
private readonly IUserService _userService;
private readonly IAuthorizationService _authorizationService;
@ -29,6 +31,8 @@ public class CollectionsController : Controller
public CollectionsController(
ICollectionRepository collectionRepository,
ICollectionService collectionService,
ICreateCollectionCommand createCollectionCommand,
IUpdateCollectionCommand updateCollectionCommand,
IDeleteCollectionCommand deleteCollectionCommand,
IUserService userService,
IAuthorizationService authorizationService,
@ -37,6 +41,8 @@ public class CollectionsController : Controller
{
_collectionRepository = collectionRepository;
_collectionService = collectionService;
_createCollectionCommand = createCollectionCommand;
_updateCollectionCommand = updateCollectionCommand;
_deleteCollectionCommand = deleteCollectionCommand;
_userService = userService;
_authorizationService = authorizationService;
@ -153,7 +159,7 @@ public class CollectionsController : Controller
var groups = model.Groups?.Select(g => g.ToSelectionReadOnly());
var users = model.Users?.Select(g => g.ToSelectionReadOnly()).ToList() ?? new List<CollectionAccessSelection>();
await _collectionService.SaveAsync(collection, groups, users);
await _createCollectionCommand.CreateAsync(collection, groups, users);
if (!_currentContext.UserId.HasValue || (_currentContext.GetOrganization(orgId) == null && await _currentContext.ProviderUserForOrgAsync(orgId)))
{
@ -179,7 +185,7 @@ public class CollectionsController : Controller
var groups = model.Groups?.Select(g => g.ToSelectionReadOnly());
var users = model.Users?.Select(g => g.ToSelectionReadOnly());
await _collectionService.SaveAsync(model.ToCollection(collection), groups, users);
await _updateCollectionCommand.UpdateAsync(model.ToCollection(collection), groups, users);
if (!_currentContext.UserId.HasValue || (_currentContext.GetOrganization(collection.OrganizationId) == null && await _currentContext.ProviderUserForOrgAsync(collection.OrganizationId)))
{

View File

@ -2,6 +2,7 @@
using Bit.Api.Models.Public.Request;
using Bit.Api.Models.Public.Response;
using Bit.Core.Context;
using Bit.Core.OrganizationFeatures.OrganizationCollections.Interfaces;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Microsoft.AspNetCore.Authorization;
@ -14,18 +15,18 @@ namespace Bit.Api.Public.Controllers;
public class CollectionsController : Controller
{
private readonly ICollectionRepository _collectionRepository;
private readonly ICollectionService _collectionService;
private readonly IUpdateCollectionCommand _updateCollectionCommand;
private readonly ICurrentContext _currentContext;
private readonly IApplicationCacheService _applicationCacheService;
public CollectionsController(
ICollectionRepository collectionRepository,
ICollectionService collectionService,
IUpdateCollectionCommand updateCollectionCommand,
ICurrentContext currentContext,
IApplicationCacheService applicationCacheService)
{
_collectionRepository = collectionRepository;
_collectionService = collectionService;
_updateCollectionCommand = updateCollectionCommand;
_currentContext = currentContext;
_applicationCacheService = applicationCacheService;
}
@ -93,7 +94,7 @@ public class CollectionsController : Controller
}
var updatedCollection = model.ToCollection(existingCollection);
var associations = model.Groups?.Select(c => c.ToCollectionAccessSelection()).ToList();
await _collectionService.SaveAsync(updatedCollection, associations);
await _updateCollectionCommand.UpdateAsync(updatedCollection, associations, null);
var response = new CollectionResponseModel(updatedCollection, associations);
return new JsonResult(response);
}

View File

@ -9,7 +9,6 @@ using Bit.Core.Exceptions;
using Bit.Core.Models.Data;
using Bit.Core.OrganizationFeatures.OrganizationCollections.Interfaces;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using Microsoft.AspNetCore.Authorization;
@ -38,9 +37,11 @@ public class CollectionsControllerTests
_ = await sutProvider.Sut.Post(organization.Id, collectionRequest);
await sutProvider.GetDependency<ICollectionService>()
await sutProvider.GetDependency<ICreateCollectionCommand>()
.Received(1)
.SaveAsync(Arg.Any<Collection>(), Arg.Any<IEnumerable<CollectionAccessSelection>>(),
.CreateAsync(Arg.Is<Collection>(c =>
c.Name == collectionRequest.Name && c.ExternalId == collectionRequest.ExternalId && c.OrganizationId == organization.Id),
Arg.Any<IEnumerable<CollectionAccessSelection>>(),
Arg.Any<IEnumerable<CollectionAccessSelection>>());
}
@ -64,9 +65,9 @@ public class CollectionsControllerTests
_ = await sutProvider.Sut.Put(collection.OrganizationId, collection.Id, collectionRequest);
await sutProvider.GetDependency<ICollectionService>()
await sutProvider.GetDependency<IUpdateCollectionCommand>()
.Received(1)
.SaveAsync(ExpectedCollection(), Arg.Any<IEnumerable<CollectionAccessSelection>>(),
.UpdateAsync(ExpectedCollection(), Arg.Any<IEnumerable<CollectionAccessSelection>>(),
Arg.Any<IEnumerable<CollectionAccessSelection>>());
}