Files
server/src/Core/Services/Implementations/CollectionService.cs

89 lines
3.3 KiB
C#
Raw Normal View History

2017-04-03 12:27:02 -04:00
using System;
using System.Threading.Tasks;
using Bit.Core.Exceptions;
using Bit.Core.Models.Table;
using Bit.Core.Repositories;
using System.Collections.Generic;
2017-05-11 12:22:14 -04:00
using Bit.Core.Models.Data;
2017-04-03 12:27:02 -04:00
namespace Bit.Core.Services
{
2017-04-27 09:19:30 -04:00
public class CollectionService : ICollectionService
2017-04-03 12:27:02 -04:00
{
private readonly IEventService _eventService;
2017-04-03 12:27:02 -04:00
private readonly IOrganizationRepository _organizationRepository;
private readonly IOrganizationUserRepository _organizationUserRepository;
2017-04-27 09:19:30 -04:00
private readonly ICollectionRepository _collectionRepository;
2017-04-03 12:27:02 -04:00
private readonly IUserRepository _userRepository;
private readonly IMailService _mailService;
2017-04-27 09:19:30 -04:00
public CollectionService(
IEventService eventService,
2017-04-03 12:27:02 -04:00
IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository,
2017-04-27 09:19:30 -04:00
ICollectionRepository collectionRepository,
2017-04-03 12:27:02 -04:00
IUserRepository userRepository,
IMailService mailService)
{
_eventService = eventService;
2017-04-03 12:27:02 -04:00
_organizationRepository = organizationRepository;
_organizationUserRepository = organizationUserRepository;
2017-04-27 09:19:30 -04:00
_collectionRepository = collectionRepository;
2017-04-03 12:27:02 -04:00
_userRepository = userRepository;
_mailService = mailService;
}
2017-05-11 12:22:14 -04:00
public async Task SaveAsync(Collection collection, IEnumerable<SelectionReadOnly> groups = null)
{
var org = await _organizationRepository.GetByIdAsync(collection.OrganizationId);
if(org == null)
{
throw new BadRequestException("Organization not found");
}
if(collection.Id == default(Guid))
{
2017-04-27 09:19:30 -04:00
if(org.MaxCollections.HasValue)
{
2017-04-27 09:19:30 -04:00
var collectionCount = await _collectionRepository.GetCountByOrganizationIdAsync(org.Id);
if(org.MaxCollections.Value <= collectionCount)
{
2017-04-27 09:19:30 -04:00
throw new BadRequestException("You have reached the maximum number of collections " +
$"({org.MaxCollections.Value}) for this organization.");
}
}
2017-04-03 12:27:02 -04:00
2017-05-11 12:22:14 -04:00
if(groups == null || !org.UseGroups)
{
await _collectionRepository.CreateAsync(collection);
}
else
{
2017-05-11 12:22:14 -04:00
await _collectionRepository.CreateAsync(collection, groups);
}
await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Created);
}
else
{
if(!org.UseGroups)
{
await _collectionRepository.ReplaceAsync(collection);
}
else
{
await _collectionRepository.ReplaceAsync(collection, groups ?? new List<SelectionReadOnly>());
}
await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Updated);
}
}
public async Task DeleteAsync(Collection collection)
{
await _collectionRepository.DeleteAsync(collection);
await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Deleted);
}
2017-04-03 12:27:02 -04:00
}
}