-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from IQSS/200-update-collection
Adds UpdateCollection use case
- Loading branch information
Showing
10 changed files
with
313 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ The different use cases currently available in the package are classified below, | |
- [List All Collection Items](#list-all-collection-items) | ||
- [Collections write use cases](#collections-write-use-cases) | ||
- [Create a Collection](#create-a-collection) | ||
- [Update a Collection](#update-a-collection) | ||
- [Publish a Collection](#publish-a-collection) | ||
- [Datasets](#Datasets) | ||
- [Datasets read use cases](#datasets-read-use-cases) | ||
|
@@ -232,6 +233,34 @@ The above example creates the new collection in the root collection since no col | |
|
||
The use case returns a number, which is the identifier of the created collection. | ||
|
||
#### Update a Collection | ||
|
||
Updates an existing collection, given a collection identifier and a [CollectionDTO](../src/collections/domain/dtos/CollectionDTO.ts) including the updated collection data. | ||
|
||
##### Example call: | ||
|
||
```typescript | ||
import { updateCollection } from '@iqss/dataverse-client-javascript' | ||
|
||
/* ... */ | ||
|
||
const collectionIdOrAlias = 12345 | ||
const collectionDTO: CollectionDTO = { | ||
alias: alias, | ||
name: 'Updated Collection Name', | ||
contacts: ['[email protected]'], | ||
type: CollectionType.DEPARTMENT | ||
} | ||
|
||
updateCollection.execute(collectionIdOrAlias, collectionDTO) | ||
|
||
/* ... */ | ||
``` | ||
|
||
_See [use case](../src/collections/domain/useCases/UpdateCollection.ts) implementation_. | ||
|
||
The `collectionIdOrAlias` is a generic collection identifier, which can be either a string (for queries by CollectionAlias), or a number (for queries by CollectionId). | ||
|
||
#### Publish a Collection | ||
|
||
Publishes a Collection, given the collection identifier. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
import { CollectionDTO } from '../dtos/CollectionDTO' | ||
import { ICollectionsRepository } from '../repositories/ICollectionsRepository' | ||
|
||
export class UpdateCollection implements UseCase<void> { | ||
private collectionsRepository: ICollectionsRepository | ||
|
||
constructor(collectionsRepository: ICollectionsRepository) { | ||
this.collectionsRepository = collectionsRepository | ||
} | ||
|
||
/** | ||
* Updates an existing collection, given a collection identifier and a CollectionDTO including the updated collection data. | ||
* | ||
* @param {number | string} [collectionIdOrAlias] - A generic collection identifier, which can be either a string (for queries by CollectionAlias), or a number (for queries by CollectionId) | ||
* @param {CollectionDTO} [newCollection] - CollectionDTO object including the updated collection data. | ||
* @returns {Promise<void>} -This method does not return anything upon successful completion. | ||
* @throws {WriteError} - If there are errors while writing data. | ||
*/ | ||
async execute( | ||
collectionIdOrAlias: number | string, | ||
updatedCollection: CollectionDTO | ||
): Promise<void> { | ||
return await this.collectionsRepository.updateCollection(collectionIdOrAlias, updatedCollection) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
POSTGRES_VERSION=13 | ||
DATAVERSE_DB_USER=dataverse | ||
SOLR_VERSION=9.3.0 | ||
DATAVERSE_IMAGE_REGISTRY=docker.io | ||
DATAVERSE_IMAGE_TAG=unstable | ||
DATAVERSE_IMAGE_REGISTRY=ghcr.io | ||
DATAVERSE_IMAGE_TAG=10904-edit-dataverse-collection | ||
DATAVERSE_BOOTSTRAP_TIMEOUT=5m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
ApiConfig, | ||
WriteError, | ||
createCollection, | ||
getCollection, | ||
updateCollection | ||
} from '../../../src' | ||
import { TestConstants } from '../../testHelpers/TestConstants' | ||
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
import { createCollectionDTO } from '../../testHelpers/collections/collectionHelper' | ||
|
||
describe('execute', () => { | ||
beforeEach(async () => { | ||
ApiConfig.init( | ||
TestConstants.TEST_API_URL, | ||
DataverseApiAuthMechanism.API_KEY, | ||
process.env.TEST_API_KEY | ||
) | ||
}) | ||
|
||
test('should successfully update a new collection', async () => { | ||
const testNewCollectionAlias = 'updateCollection-functional-test' | ||
const testNewCollection = createCollectionDTO(testNewCollectionAlias) | ||
await createCollection.execute(testNewCollection) | ||
const testNewName = 'Updated Name' | ||
testNewCollection.name = testNewName | ||
expect.assertions(1) | ||
try { | ||
await updateCollection.execute(testNewCollectionAlias, testNewCollection) | ||
} catch (error) { | ||
throw new Error('Collection should be updated') | ||
} finally { | ||
const updatedCollection = await getCollection.execute(testNewCollectionAlias) | ||
expect(updatedCollection.name).toBe(testNewName) | ||
} | ||
}) | ||
|
||
test('should throw an error when the parent collection does not exist', async () => { | ||
const testNewCollection = createCollectionDTO() | ||
expect.assertions(2) | ||
let writeError: WriteError | ||
try { | ||
await updateCollection.execute(TestConstants.TEST_DUMMY_COLLECTION_ID, testNewCollection) | ||
throw new Error('Use case should throw an error') | ||
} catch (error) { | ||
writeError = error | ||
} finally { | ||
expect(writeError).toBeInstanceOf(WriteError) | ||
expect(writeError.message).toEqual( | ||
`There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'` | ||
) | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.