-
Notifications
You must be signed in to change notification settings - Fork 21
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 #330 from hms-dbmi-cellenics/sample-create-delete-v2
Sample create delete api v2
- Loading branch information
Showing
38 changed files
with
1,201 additions
and
493 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
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,55 @@ | ||
const Sample = require('../model/Sample'); | ||
const Experiment = require('../model/Experiment'); | ||
const MetadataTrack = require('../model/MetadataTrack'); | ||
|
||
const getLogger = require('../../utils/getLogger'); | ||
const { OK } = require('../../utils/responses'); | ||
|
||
const sqlClient = require('../../sql/sqlClient'); | ||
|
||
const logger = getLogger('[SampleController] - '); | ||
|
||
const createSample = async (req, res) => { | ||
const { | ||
params: { experimentId, sampleId }, | ||
body: { name, sampleTechnology }, | ||
} = req; | ||
|
||
logger.log('Creating sample'); | ||
|
||
await sqlClient.get().transaction(async (trx) => { | ||
await new Sample(trx).create({ | ||
id: sampleId, | ||
experiment_id: experimentId, | ||
name, | ||
sample_technology: sampleTechnology, | ||
}); | ||
|
||
await new Experiment(trx).addSample(experimentId, sampleId); | ||
|
||
await new MetadataTrack(trx) | ||
.createNewSampleValues(experimentId, sampleId); | ||
}); | ||
|
||
logger.log(`Finished creating sample ${sampleId} for experiment ${experimentId}`); | ||
|
||
res.json(OK()); | ||
}; | ||
|
||
const deleteSample = async (req, res) => { | ||
const { params: { experimentId, sampleId } } = req; | ||
|
||
await sqlClient.get().transaction(async (trx) => { | ||
await new Sample(trx).destroy(sampleId); | ||
await new Experiment(trx).deleteSample(experimentId, sampleId); | ||
}); | ||
|
||
logger.log(`Finished deleting sample ${sampleId} from experiment ${experimentId}`); | ||
|
||
res.json(OK()); | ||
}; | ||
|
||
module.exports = { | ||
createSample, | ||
deleteSample, | ||
}; |
This file was deleted.
Oops, something went wrong.
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 @@ | ||
const tableNames = { | ||
EXPERIMENT: 'experiment', | ||
EXPERIMENT_EXECUTION: 'experiment_execution', | ||
SAMPLE: 'sample', | ||
USER_ACCESS: 'user_access', | ||
INVITE_ACCESS: 'invite_access', | ||
METADATA_TRACK: 'metadata_track', | ||
SAMPLE_IN_METADATA_TRACK_MAP: 'sample_in_metadata_track_map', | ||
}; | ||
|
||
module.exports = tableNames; |
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,71 @@ | ||
// The basic functions of a model that uses Knexjs to store and retrieve data from a | ||
// database using the provided `knex` instance. Custom functionality can be | ||
// composed on top of this set of common functions. | ||
// | ||
// The idea is that these are the most-used types of functions that most/all | ||
// "models" will want to have. | ||
|
||
class BasicModel { | ||
constructor(sql, tableName, selectableProps = [], timeout = 4000) { | ||
this.sql = sql; | ||
this.tableName = tableName; | ||
this.selectableProps = selectableProps; | ||
this.timeout = timeout; | ||
} | ||
|
||
create(props) { | ||
return this.sql.insert(props) | ||
.returning(this.selectableProps) | ||
.into(this.tableName) | ||
.timeout(this.timeout); | ||
} | ||
|
||
findAll() { | ||
return this.sql | ||
.select(this.selectableProps) | ||
.from(this.tableName) | ||
.timeout(this.timeout); | ||
} | ||
|
||
find(filters) { | ||
return this.sql.select(this.selectableProps) | ||
.from(this.tableName) | ||
.where(filters) | ||
.timeout(this.timeout); | ||
} | ||
|
||
// Same as `find` but only returns the first match if >1 are found. | ||
findOne(filters) { | ||
return this.find(filters) | ||
// @ts-ignore | ||
.then((results) => { | ||
if (!Array.isArray(results)) return results; | ||
|
||
return results[0]; | ||
}); | ||
} | ||
|
||
findById(id) { | ||
return this.sql.select(this.selectableProps) | ||
.from(this.tableName) | ||
.where({ id }) | ||
.timeout(this.timeout); | ||
} | ||
|
||
update(id, props) { | ||
return this.sql.update(props) | ||
.from(this.tableName) | ||
.where({ id }) | ||
.returning(this.selectableProps) | ||
.timeout(this.timeout); | ||
} | ||
|
||
destroy(id) { | ||
return this.sql.del() | ||
.from(this.tableName) | ||
.where({ id }) | ||
.timeout(this.timeout); | ||
} | ||
} | ||
|
||
module.exports = BasicModel; |
Oops, something went wrong.