Skip to content

Commit

Permalink
Add ability to get parent feature from features endpoint (GMOD#456)
Browse files Browse the repository at this point in the history
* Add topLevel flag to GET features endpoint

* Use boolean pipe

---------

Co-authored-by: Garrett Stevens <[email protected]>
  • Loading branch information
shashankbrgowda and garrettjstevens authored Oct 8, 2024
1 parent e8db129 commit c3c59b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Controller, Get, Logger, Param, Query } from '@nestjs/common'
import {
Controller,
Get,
Logger,
Param,
ParseBoolPipe,
Query,
} from '@nestjs/common'

import { FeatureRangeSearchDto } from '../entity/gff3Object.dto'
import { Role } from '../utils/role/role.enum'
Expand Down Expand Up @@ -54,9 +61,13 @@ export class FeaturesController {
* or if search data was not found or in case of error throw exception
*/
@Get(':featureid')
getFeature(@Param('featureid') featureid: string) {
getFeature(
@Param('featureid') featureid: string,
@Query('topLevel', new ParseBoolPipe({ optional: true }))
topLevel: boolean | undefined,
) {
this.logger.debug(`Get feature by featureId: ${featureid}`)
return this.featuresService.findById(featureid)
return this.featuresService.findById(featureid, topLevel)
}

@Get('check/:featureid')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ export class FeaturesService {
/**
* Get feature by featureId. When retrieving features by id, the features and any of its children are returned, but not any of its parent or sibling features.
* @param featureId - featureId
* @param topLevel - If true, return the top level feature and its children. If false, return the requested feature and its children.
* @returns Return the feature(s) if search was successful. Otherwise throw exception
*/
async findById(featureId: string) {
async findById(featureId: string, topLevel?: boolean) {
// Search correct feature
const topLevelFeature = await this.featureModel
.findOne({ allIds: featureId })
Expand All @@ -87,7 +88,11 @@ export class FeaturesService {
}

// Now we need to find correct top level feature or sub-feature inside the feature
const foundFeature = this.getFeatureFromId(topLevelFeature, featureId)
const foundFeature = this.getFeatureFromId(
topLevelFeature,
featureId,
topLevel,
)
if (!foundFeature) {
const errMsg = 'ERROR when searching feature by featureId'
this.logger.error(errMsg)
Expand All @@ -103,13 +108,21 @@ export class FeaturesService {
* @param featureId -
* @returns
*/
getFeatureFromId(feature: Feature, featureId: string): Feature | null {
getFeatureFromId(
feature: Feature,
featureId: string,
topLevel?: boolean,
parent?: Feature | null,
): Feature | null {
this.logger.verbose(`Entry=${JSON.stringify(feature)}`)

if (feature._id.equals(featureId)) {
this.logger.debug(
`Top level featureId matches in object ${JSON.stringify(feature)}`,
)
if (topLevel && parent) {
return parent
}
return feature
}
// Check if there is also childFeatures in parent feature and it's not empty
Expand All @@ -118,8 +131,16 @@ export class FeaturesService {
'FeatureId was not found on top level so lets make recursive call...',
)
for (const [, childFeature] of feature.children ?? new Map()) {
const subFeature = this.getFeatureFromId(childFeature, featureId)
const subFeature = this.getFeatureFromId(
childFeature,
featureId,
topLevel,
feature,
)
if (subFeature) {
if (topLevel) {
return feature
}
return subFeature
}
}
Expand Down

0 comments on commit c3c59b7

Please sign in to comment.