Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 20 additions & 1 deletion app/lib/server/controllers/run.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
const { runService } = require('../../services/runs/RunService');
const { stdDataRequestDTO } = require('../../domain/dtos');
const { validateDtoOrRepondOnFailure } = require('../utilities');
const Joi = require('joi');

/**
* List All runs in db
Expand All @@ -22,7 +23,6 @@ const { validateDtoOrRepondOnFailure } = require('../utilities');
* @param {Object} next express next handler
* @returns {undefined}
*/

const listRunsHandler = async (req, res, next) => {
const validatedDTO = await validateDtoOrRepondOnFailure(stdDataRequestDTO, req, res);
if (validatedDTO) {
Expand All @@ -33,6 +33,25 @@ const listRunsHandler = async (req, res, next) => {
}
};

/**
* List runs belonging to period which id is provided
* @param {Object} req express HTTP request object
* @param {Object} res express HTTP response object
* @param {Object} next express next handler
* @returns {undefined}
*/
const listRunsPerPeriodHandler = async (req, res, next) => {
const customDTO = stdDataRequestDTO.keys({ params: { id: Joi.number() } });
const validatedDTO = await validateDtoOrRepondOnFailure(customDTO, req, res);
if (validatedDTO) {
const runs = await runService.getRunsPerPeriod(validatedDTO.params.id, validatedDTO.query);
res.json({
data: runs,
});
}
};

module.exports = {
listRunsHandler,
listRunsPerPeriodHandler,
};
8 changes: 7 additions & 1 deletion app/lib/server/routers/period.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* or submit itself to any jurisdiction.
*/

const { PeriodController } = require('../controllers');
const { PeriodController, RunController } = require('../controllers');

module.exports = {
path: '/periods',
Expand All @@ -22,5 +22,11 @@ module.exports = {
controller: PeriodController.listPeriodsHandler,
description: 'List all periods which are present in DB with avg energy of run\'s beams associated with them',
},
{
method: 'get',
path: '/:id/runs',
controller: RunController.listRunsPerPeriodHandler,
description: 'List all runs associated with period which id is provided',
},
],
};
18 changes: 17 additions & 1 deletion app/lib/services/runs/RunService.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,28 @@ class RunService {
* @param {Object} query - Filtering query definiton from http request,... #TODO
* @returns {Promise<Run[]>} Promise object represents the result of this use case.
*/
async getAll({ filter }) { // TODO args
async getAll({ filter }) {
const runs = await RunRepository.findAll({
where: filterToSequelizeWhereClause(filter),
});
return runs.map((run) => runAdapter.toEntity(run));
}

/**
* Return all runs
* @param {Number} periodId - id of period which for runs should be returnd
* @param {Object} query - Filtering query definiton from http request,... #TODO
* @returns {Promise<Run[]>} Promise object represents the result of this use case.
*/
async getRunsPerPeriod(periodId, { filter }) {
const runs = await RunRepository.findAll({
where: {
period_id: periodId,
...filterToSequelizeWhereClause(filter),
},
});
return runs.map((run) => runAdapter.toEntity(run));
}
}

module.exports = {
Expand Down