2017-04-03 12:27:02 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Bit.Core.Exceptions;
|
2017-04-08 10:44:13 -04:00
|
|
|
|
using Bit.Core.Models.Table;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
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 IOrganizationRepository _organizationRepository;
|
|
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
2017-04-27 09:19:30 -04:00
|
|
|
|
private readonly ICollectionRepository _collectionRepository;
|
|
|
|
|
|
private readonly ICollectionUserRepository _collectionUserRepository;
|
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(
|
2017-04-03 12:27:02 -04:00
|
|
|
|
IOrganizationRepository organizationRepository,
|
|
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
2017-04-27 09:19:30 -04:00
|
|
|
|
ICollectionRepository collectionRepository,
|
|
|
|
|
|
ICollectionUserRepository collectionUserRepository,
|
2017-04-03 12:27:02 -04:00
|
|
|
|
IUserRepository userRepository,
|
|
|
|
|
|
IMailService mailService)
|
|
|
|
|
|
{
|
|
|
|
|
|
_organizationRepository = organizationRepository;
|
|
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
2017-04-27 09:19:30 -04:00
|
|
|
|
_collectionRepository = collectionRepository;
|
|
|
|
|
|
_collectionUserRepository = collectionUserRepository;
|
2017-04-03 12:27:02 -04:00
|
|
|
|
_userRepository = userRepository;
|
|
|
|
|
|
_mailService = mailService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public async Task SaveAsync(Collection collection)
|
2017-04-08 10:44:13 -04:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
if(collection.Id == default(Guid))
|
2017-04-08 10:44:13 -04:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
var org = await _organizationRepository.GetByIdAsync(collection.OrganizationId);
|
2017-04-08 10:44:13 -04:00
|
|
|
|
if(org == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Org not found");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
if(org.MaxCollections.HasValue)
|
2017-04-08 10:44:13 -04:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
var collectionCount = await _collectionRepository.GetCountByOrganizationIdAsync(org.Id);
|
|
|
|
|
|
if(org.MaxCollections.Value <= collectionCount)
|
2017-04-08 10:44:13 -04:00
|
|
|
|
{
|
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-08 10:44:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-04-03 12:27:02 -04:00
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
await _collectionRepository.CreateAsync(collection);
|
2017-04-08 10:44:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
await _collectionRepository.ReplaceAsync(collection);
|
2017-04-08 10:44:13 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-04-03 12:27:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|