public void Delete(CollectionDetailsViewModel collectionDetails) { _context.Word.RemoveRange(collectionDetails.Words); _context.SaveChanges(); _context.ProjectCollection.RemoveRange(collectionDetails.ProjectCollections); _context.SaveChanges(); // For now, because nothing else exists, a simple delete works: _context.Collection.Remove(collectionDetails.Collection); _context.SaveChanges(); }
public void User_Can_Delete_Collection() { // Get an object that's in the database var collectionToAdd = new Collection() { UserId = 1, CategorizationId = 1, Name = "New Collection to Axe", Description = "Blah", Pinned = false, CreationDate = DateTime.Now - TimeSpan.FromDays(15) }; // Add collectionToAdd to collectionDetailsVm var collectionDetails = new CollectionDetailsViewModel { Collection = collectionToAdd, ProjectCollections = new List <ProjectCollection>(), Words = new List <Word>() }; // Instantiate CollectionRepo var repo = new CollectionRepository(_context); // Get a count of Collections for UserId 1 var count = repo.Get(1).Count; // Add new collection repo.Add(collectionToAdd); // Get a new count var countAfterAdd = repo.Get(1).Count; // Attempt to delete the collection repo.Delete(collectionDetails); // New count after deleting var countAfterDeletion = repo.Get(1).Count; // We successfully added one collection Assert.True(count < countAfterAdd); // We successfully deleted one collection Assert.True(count == countAfterDeletion); }
public IActionResult Add(CollectionFormViewModel collectionForm) { // For the Add, do not need to check for if the projectCollections are in the db // because this Collection is unique, there can be no duplicates. var firebaseUser = _utils.GetCurrentUser(User); // Check to ensure an unauthorized user (anonymous account) can not add a collection if (firebaseUser == null) { return(NotFound()); } // Ensure the userId on the incoming collection matches the person making the request if (collectionForm.Collection.UserId != firebaseUser.Id) { return(BadRequest()); } // Get all of this user's collections var allCollections = _collectionRepo.Get(firebaseUser.Id); // see if the name of the incoming collection is in the db var collectionWithThatName = allCollections.Find(c => c.Name == collectionForm.Collection.Name); // if there is a returned collection, we can't add because name isn't unique for this user if (collectionWithThatName != null) { return(NotFound()); } // Need to add the default requirements for the collection here collectionForm.Collection.CategorizationId = 1; collectionForm.Collection.CreationDate = DateTime.Now; try { _collectionRepo.Add(collectionForm.Collection); try { // After we add the collection, assign the collection id to each projectCollection foreach (var projectCollection in collectionForm.ProjectCollections) { projectCollection.CollectionId = collectionForm.Collection.Id; } } // The user attempted to enter Null for their ProjectCollecitons catch (NullReferenceException e) { // Make a CollectionDetailsViewModel to pass the created collection into for deletion var collectionDetailsVm = new CollectionDetailsViewModel { Collection = collectionForm.Collection, ProjectCollections = new List <ProjectCollection>(), Words = new List <Word>() }; // Remove the just entered collection from db _collectionRepo.Delete(collectionDetailsVm); // Return a BadRequest return(BadRequest()); } // Add ProjectCollections _projColRepo.Add(collectionForm.ProjectCollections); return(Ok(collectionForm)); } catch (DbUpdateException e) { return(NotFound()); } }