public void AddCollection_PublicCollection_CollectionAddedToDb(string collectionName)
        {
            //Arrange
            var collectionData = new CollectionCreationData {
                IsPublic = true, Name = collectionName
            };
            var userId = NewGuid;

            AddUser(userId);

            //Act
            InTransaction(context =>
            {
                var controller = new CollectionsController(context, GetUserProviderMock(userId), getCollectionConfigurationProviderMock());
                controller.AddCollection(collectionData);
            });

            //Assert
            InTransaction(context =>
            {
                var collection = context.Collection.SingleOrDefault(c => c.Name == collectionName);
                collection.Should().NotBeNull();
                collection.IsPublic.Should().BeTrue();
            });
        }
        public void AddCollection_PublicCollection_CollectionAssignedToUser(string collectionName)
        {
            //Arrange
            var collectionData = new CollectionCreationData {
                IsPublic = true, Name = collectionName
            };
            var userId = NewGuid;

            AddUser(userId);

            //Act
            InTransaction(context =>
            {
                var controller = new CollectionsController(context, GetUserProviderMock(userId), getCollectionConfigurationProviderMock());
                controller.AddCollection(collectionData);
            });

            //Assert
            InTransaction(context =>
            {
                var collectionFromContext = context.Collection.SingleOrDefault(c => c.Name == collectionName);
                var collectionFromUser    = context.ApplicationUser
                                            .Include(u => u.Collections)
                                            .Single(u => u.Id == userId).Collections
                                            .Single(c => c.Name == collectionName);
                collectionFromContext.Should().BeEquivalentTo(collectionFromUser);
                collectionFromContext.OwnerId.Should().Be(userId);
            });
        }
        public IActionResult AddCollection([FromBody] CollectionCreationData data)
        {
            var maxCollectionPerUser = _configurationProvider.MaxCollectionsPerUser;
            var userId = _userProvider.GetCurrentUserId();
            var userCollectionCount = _dbContext.Users
                                      .Include(u => u.Collections)
                                      .Single(u => u.Id == userId)
                                      .Collections.Count;

            if (userCollectionCount >= maxCollectionPerUser)
            {
                return(BadRequest("Could not add the collection: Reached maximum number of collections"));
            }

            var collection = new Collection
            {
                Name        = data.Name,
                IsPublic    = data.IsPublic,
                OwnerId     = userId,
                Description = data.Description
            };

            _dbContext.Add(collection);
            _dbContext.SaveChanges();
            return(Ok());
        }