Skip to content

Commit

Permalink
Merge pull request #117 from dataswap/feat/add-api-of-datasetchallenge
Browse files Browse the repository at this point in the history
feat: 🎸 add api of datasetchallenge
  • Loading branch information
siriusyim authored Jan 18, 2024
2 parents ccb0328 + 157bc8c commit a203bed
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import { MatchingTargetService } from "./matchingTarget/matchingTarget.service"
import { MatchingBidsController } from "./matchingBids/matchingBids.controller"
import { MatchingBidsService } from "./matchingBids/matchingBids.service"
import { BackgroundTaskService } from "./backgroundTask/provider/backgroundTask.service"
import { DatasetChallengeController } from "./datasetChallenge/datasetChallenge.controller"
import { DatasetChallengeService } from "./datasetChallenge/datasetChallenge.service"

/**
* Root module for the application.
Expand All @@ -69,6 +71,7 @@ import { BackgroundTaskService } from "./backgroundTask/provider/backgroundTask.
MatchingMetadataController,
MatchingTargetController,
MatchingBidsController,
DatasetChallengeController,
],
providers: [
VersionService,
Expand All @@ -86,6 +89,7 @@ import { BackgroundTaskService } from "./backgroundTask/provider/backgroundTask.
MatchingMetadataService,
MatchingTargetService,
MatchingBidsService,
DatasetChallengeService,
],
})
export class AppModule {}
72 changes: 72 additions & 0 deletions src/datasetChallenge/datasetChallenge.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*******************************************************************************
* (c) 2023 dataswap
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

import { Controller, Post, Body, UseInterceptors } from "@nestjs/common"
import { DatasetChallengeService } from "./datasetChallenge.service"
import { DatasetChallenge } from "@dataswapjs/dataswapjs"
import { ValueFields, Result } from "@unipackage/utils"
import { BigIntToStringInterceptor } from "../shared/bigIntToStringInterceptor"
import { QueryParam } from "src/shared/queryParams"

/**
* Controller responsible for handling root-level requests.
*/
@UseInterceptors(BigIntToStringInterceptor)
@Controller("datasetchallenge")
export class DatasetChallengeController {
/**
* Creates an instance of RootController.
* @param rootService - The RootService instance.
*/
constructor(
private readonly datasetChallengeService: DatasetChallengeService
) {}

/**
* Handles GET requests for root-level resources with an identifier.
* @param queryFilter - Request parameters.
* @returns A string representing the response.
* @example
* {
* "conditions": [
* {
* "datasetId": { "$gt": 0, "$lt": 3}
* }
* ]
* }
*/
@Post("query")
async find(
@Body() queryParam: QueryParam<DatasetChallenge>
): Promise<Result<ValueFields<DatasetChallenge>[]>> {
return await this.datasetChallengeService.find(queryParam)
}

/**
* Gets a total count.
* @returns A number representing a total count by query param.
*/
@Post("total")
async total(
@Body() queryParam: QueryParam<DatasetChallenge>
): Promise<Result<number>> {
return await this.datasetChallengeService.total(queryParam)
}
}
68 changes: 68 additions & 0 deletions src/datasetChallenge/datasetChallenge.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* (c) 2023 dataswap
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

import { Injectable } from "@nestjs/common"
import { DatasetChallenge } from "@dataswapjs/dataswapjs"
import { ValueFields, Result } from "@unipackage/utils"
import { calibrationBgTask, mainBgTask } from "../config/backgroundTask"
import { BackgroundTask } from "src/backgroundTask"
import { QueryParam } from "src/shared/queryParams"

/**
* Service responsible for providing root-level functionality.
*/
@Injectable()
export class DatasetChallengeService {
/**
* Gets a greeting message.
* @returns A string representing a greeting message.
*/
async find(
queryParam: QueryParam<DatasetChallenge>
): Promise<Result<ValueFields<DatasetChallenge>[]>> {
let bgTask: BackgroundTask
if (queryParam.network === "calibration") {
bgTask = calibrationBgTask
} else {
bgTask = mainBgTask
}
return await bgTask.context.datastore.datasetChallenge.find(
queryParam.queryFilter
)
}

/**
* Gets a total count.
* @returns A number representing a total count by query param.
*/
async total(
queryParam: QueryParam<DatasetChallenge>
): Promise<Result<number>> {
let bgTask: BackgroundTask
if (queryParam.network === "calibration") {
bgTask = calibrationBgTask
} else {
bgTask = mainBgTask
}
return await bgTask.context.datastore.datasetChallenge.total(
queryParam.queryFilter
)
}
}
20 changes: 20 additions & 0 deletions test/app.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import { MatchingBidsController } from "../src/matchingBids/matchingBids.control
import { MatchingBidsService } from "../src/matchingBids/matchingBids.service"
import { SyncController } from "../src/sync/sync.controller"
import { SyncService } from "../src/sync/sync.service"
import { DatasetChallengeController } from "../src/datasetChallenge/datasetChallenge.controller"
import { DatasetChallengeService } from "../src/datasetChallenge/datasetChallenge.service"

describe("AppController Test", () => {
let tipsetController: TipsetController
Expand All @@ -62,6 +64,7 @@ describe("AppController Test", () => {
let matchingTargetController: MatchingTargetController
let matchingBidsController: MatchingBidsController
let syncController: SyncController
let datasetChallengeController: DatasetChallengeController

beforeAll(async () => {
calibrationBgTask.start()
Expand All @@ -88,6 +91,7 @@ describe("AppController Test", () => {
MatchingTargetController,
MatchingBidsController,
SyncController,
DatasetChallengeController,
],
providers: [
TipsetService,
Expand All @@ -103,6 +107,7 @@ describe("AppController Test", () => {
MatchingTargetService,
MatchingBidsService,
SyncService,
DatasetChallengeService,
],
}).compile()

Expand Down Expand Up @@ -135,6 +140,9 @@ describe("AppController Test", () => {
MatchingBidsController
)
syncController = root.get<SyncController>(SyncController)
datasetChallengeController = root.get<DatasetChallengeController>(
DatasetChallengeController
)
})

describe("tipset query", () => {
Expand Down Expand Up @@ -283,4 +291,16 @@ describe("AppController Test", () => {
expect(res.ok).toBe(true)
}, 300000)
})

describe("datasetchallenge query", () => {
it("should ok", async () => {
const res = await datasetChallengeController.find({
network: "calibration",
queryFilter: {
conditions: [{ datasetId: { $gt: 0, $lt: 3 } }],
},
})
expect(res.ok).toBe(true)
}, 300000)
})
})
12 changes: 12 additions & 0 deletions test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ describe("AppController (e2e)", () => {
.expect(201)
}, 30000)

it("/datasetchallenge/query (POST)", async () => {
return request(app.getHttpServer())
.post("/datasetchallenge/query")
.send({
network: "calibration",
queryFilter: {
conditions: [{ datasetId: { $gt: 0, $lt: 3 } }],
},
})
.expect(201)
}, 30000)

it("/version (GET)", async () => {
return request(app.getHttpServer()).get("/version").expect(200)
}, 30000)
Expand Down

0 comments on commit a203bed

Please sign in to comment.