-
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 #134 from IQSS/feature/131-create-getcollection-us…
…e-case Add getCollection use case and tests
- Loading branch information
Showing
26 changed files
with
482 additions
and
70 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 |
---|---|---|
|
@@ -8,4 +8,5 @@ node_modules | |
coverage | ||
|
||
# ignore npm lock | ||
package-json.lock | ||
package-json.lock | ||
.npmrc |
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,11 @@ | ||
import { DvObjectOwnerNode } from '../../../core' | ||
export interface Collection { | ||
id: number | ||
alias: string | ||
name: string | ||
affiliation?: string | ||
description?: string | ||
isPartOf: DvObjectOwnerNode | ||
} | ||
|
||
export const ROOT_COLLECTION_ALIAS = 'root' |
4 changes: 4 additions & 0 deletions
4
src/collections/domain/repositories/ICollectionsRepository.ts
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,4 @@ | ||
import { Collection } from '../models/Collection' | ||
export interface ICollectionsRepository { | ||
getCollection(collectionIdOrAlias: number | string): Promise<Collection> | ||
} |
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,22 @@ | ||
import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
import { ICollectionsRepository } from '../repositories/ICollectionsRepository' | ||
import { Collection, ROOT_COLLECTION_ALIAS } from '../models/Collection' | ||
|
||
export class GetCollection implements UseCase<Collection> { | ||
private collectionsRepository: ICollectionsRepository | ||
|
||
constructor(collectionsRepository: ICollectionsRepository) { | ||
this.collectionsRepository = collectionsRepository | ||
} | ||
|
||
/** | ||
* Returns a Collection instance, given the search parameters to identify it. | ||
* | ||
* @param {number | string} [collectionIdOrAlias = 'root'] - A generic collection identifier, which can be either a string (for queries by CollectionAlias), or a number (for queries by CollectionId) | ||
* If this parameter is not set, the default value is: 'root' | ||
* @returns {Promise<Collection>} | ||
*/ | ||
async execute(collectionIdOrAlias: number | string = ROOT_COLLECTION_ALIAS): Promise<Collection> { | ||
return await this.collectionsRepository.getCollection(collectionIdOrAlias) | ||
} | ||
} |
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,10 @@ | ||
import { GetCollection } from './domain/useCases/GetCollection' | ||
|
||
import { CollectionsRepository } from './infra/repositories/CollectionsRepository' | ||
|
||
const collectionsRepository = new CollectionsRepository() | ||
|
||
const getCollection = new GetCollection(collectionsRepository) | ||
|
||
export { getCollection } | ||
export { Collection } from './domain/models/Collection' |
26 changes: 26 additions & 0 deletions
26
src/collections/infra/repositories/CollectionsRepository.ts
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 { ApiRepository } from '../../../core/infra/repositories/ApiRepository' | ||
import { ICollectionsRepository } from '../../domain/repositories/ICollectionsRepository' | ||
import { transformCollectionResponseToCollection } from './transformers/collectionTransformers' | ||
import { Collection, ROOT_COLLECTION_ALIAS } from '../../domain/models/Collection' | ||
export class CollectionsRepository extends ApiRepository implements ICollectionsRepository { | ||
private readonly collectionsResourceName: string = 'dataverses' | ||
private readonly collectionsDefaultOperationType: string = 'get' | ||
|
||
public async getCollection( | ||
collectionIdOrAlias: number | string = ROOT_COLLECTION_ALIAS | ||
): Promise<Collection> { | ||
return this.doGet( | ||
this.buildApiEndpoint( | ||
this.collectionsResourceName, | ||
this.collectionsDefaultOperationType, | ||
collectionIdOrAlias | ||
), | ||
true, | ||
{ returnOwners: true } | ||
) | ||
.then((response) => transformCollectionResponseToCollection(response)) | ||
.catch((error) => { | ||
throw error | ||
}) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/collections/infra/repositories/transformers/CollectionPayload.ts
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,9 @@ | ||
import { OwnerNodePayload } from '../../../../core/infra/repositories/transformers/OwnerNodePayload' | ||
export interface CollectionPayload { | ||
id: number | ||
alias: string | ||
name: string | ||
affiliation?: string | ||
description?: string | ||
isPartOf: OwnerNodePayload | ||
} |
23 changes: 23 additions & 0 deletions
23
src/collections/infra/repositories/transformers/collectionTransformers.ts
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,23 @@ | ||
import { Collection } from '../../../domain/models/Collection' | ||
import { AxiosResponse } from 'axios' | ||
import { CollectionPayload } from './CollectionPayload' | ||
import { transformPayloadToOwnerNode } from '../../../../core/infra/repositories/transformers/dvObjectOwnerNodeTransformer' | ||
|
||
export const transformCollectionResponseToCollection = (response: AxiosResponse): Collection => { | ||
const collectionPayload = response.data.data | ||
return transformPayloadToCollection(collectionPayload) | ||
} | ||
|
||
const transformPayloadToCollection = (collectionPayload: CollectionPayload): Collection => { | ||
const collectionModel: Collection = { | ||
id: collectionPayload.id, | ||
alias: collectionPayload.alias, | ||
name: collectionPayload.name, | ||
affiliation: collectionPayload.affiliation, | ||
description: collectionPayload.description, | ||
...(collectionPayload.isPartOf && { | ||
isPartOf: transformPayloadToOwnerNode(collectionPayload.isPartOf) | ||
}) | ||
} | ||
return collectionModel | ||
} |
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
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.