Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(agora): migrating mongoose api and openapi yaml files (AG-1552) #2869

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions apps/agora/api/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import express from 'express';
import mongoose from 'mongoose';
import { dataVersionRoute } from './components/dataversion';
import { teamMemberImageRoute, teamsRoute } from './components/teams';
import {
allBiodomainsRoute,
biodomainsRoute,
comparisonGenesRoute,
dataVersionRoute,
distributionRoute,
geneRoute,
genesRoute,
nominatedGenesRoute,
searchGeneRoute,
teamMemberImageRoute,
teamsRoute,
} from './components';

const mongoUri = process.env.MONGODB_URI;

Expand All @@ -19,7 +30,15 @@ mongoose.connection.on('error', console.error.bind(console, 'MongoDB connection

const router = express.Router();
mongoose.connection.once('open', async () => {
router.get('/biodomains', allBiodomainsRoute);
router.get('/biodomains/:id', biodomainsRoute);
router.get('/dataversion', dataVersionRoute);
router.get('/distribution', distributionRoute);
router.get('/genes/search', searchGeneRoute);
router.get('/genes/comparison', comparisonGenesRoute);
router.get('/genes/nominated', nominatedGenesRoute);
router.get('/genes/:id', geneRoute);
router.get('/genes', genesRoute);
router.get('/teams', teamsRoute);
router.get('/teamMembers/:name/image', teamMemberImageRoute);
});
Expand Down
84 changes: 84 additions & 0 deletions apps/agora/api/src/components/biodomains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// -------------------------------------------------------------------------- //
// Internal
// -------------------------------------------------------------------------- //
import { Request, Response, NextFunction } from 'express';
import { cache, setHeaders } from '../helpers';
import { AllBioDomainsCollection, BioDomains, BioDomainsCollection } from '../models';
import { BioDomainInfo } from '../../app/models';

// -------------------------------------------------------------------------- //
// Functions
// -------------------------------------------------------------------------- //
export async function getAllBioDomains() {
const cacheKey = 'all-biodomains';
let result: BioDomainInfo[] | null | undefined = cache.get(cacheKey);

if (result) {
return result;
}

result = await AllBioDomainsCollection.find().lean().sort().exec();

cache.set(cacheKey, result);
return result || undefined;
}

export async function getAllGeneBioDomains() {
const cacheKey = 'all-genebiodomains';
let result: BioDomains[] | null | undefined = cache.get(cacheKey);

if (result) {
return result;
}

result = await BioDomainsCollection.find().lean().sort().exec();

cache.set(cacheKey, result);
return result || undefined;
}

export async function getBioDomains(ensg: string) {
const cacheKey = ensg + '-biodomains';
let result: BioDomains | null | undefined = cache.get(cacheKey);

if (result) {
return result;
}

result = await BioDomainsCollection.findOne({
ensembl_gene_id: ensg,
})
.lean()
.exec();

cache.set(cacheKey, result);
return result || undefined;
}

// -------------------------------------------------------------------------- //
// Routes
// -------------------------------------------------------------------------- //
export async function allBiodomainsRoute(req: Request, res: Response, next: NextFunction) {
try {
const result = await getAllBioDomains();
setHeaders(res);
res.json(result);
} catch (err) {
next(err);
}
}

export async function biodomainsRoute(req: Request, res: Response, next: NextFunction) {
if (!req.params || !req.params.id) {
res.status(404).send('Not found');
return;
}

try {
const result = await getBioDomains(<string>req.params.id);
setHeaders(res);
res.json(result?.gene_biodomains);
} catch (err) {
next(err);
}
}
Loading
Loading