mirror of
https://github.com/bitwarden/server
synced 2025-12-23 19:53:40 +00:00
[PM-22105] Extract CollectionService.SaveAsync into commands (#5959)
* Add CreateCollectionCommand and associated interface with validation logic * Implement CreateCollectionCommand to handle collection creation with organization checks and access permissions. * Introduce ICreateCollectionCommand interface for defining the collection creation contract. * Add unit tests for CreateCollectionCommand to validate various scenarios including permission checks and error handling. * Add UpdateCollectionCommand and associated interface with validation logic * Implement UpdateCollectionCommand to handle collection updates with organization checks and access permissions. * Introduce IUpdateCollectionCommand interface for defining the collection update contract. * Add unit tests for UpdateCollectionCommand to validate various scenarios including permission checks and error handling. * Add scoped services for collection commands * Register ICreateCollectionCommand and IUpdateCollectionCommand in the service collection for handling collection creation and updates. * 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. * Refactor ICollectionService and CollectionService to remove SaveAsync method * Removed the SaveAsync method from ICollectionService and its implementation in CollectionService. * Updated related tests in CollectionServiceTests to reflect the removal of SaveAsync, ensuring existing functionality remains intact. * Remove unused organization repository dependency from CollectionServiceTests
This commit is contained in:
@@ -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)))
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user