Skip to content

Commit

Permalink
feat: Add optional paddockId query parameter to filter microcontrolle…
Browse files Browse the repository at this point in the history
…rs in getAllMicrocontrollers endpoint
  • Loading branch information
tuke307 committed Nov 13, 2024
1 parent 99d9e63 commit 5d8dd47
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ export default class MicrocontrollerController extends Api {
next: NextFunction
) => {
try {
const microcontrollers =
await this.microcontrollerService.getAllMicrocontrollers();
const paddockId = req.query.paddockId ? parseInt(req.query.paddockId as string, 10) : null;
let microcontrollers: Microcontroller[];

if (paddockId) {
microcontrollers = await this.microcontrollerService.getMicrocontrollersByPaddock(paddockId);
} else {
microcontrollers = await this.microcontrollerService.getAllMicrocontrollers();
}

this.send(
res,
microcontrollers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ microcontrollers.post(
* GET /microcontrollers
* @summary Get all microcontrollers
* @tags microcontrollers
* @param {number} [paddockId.query] - Optional paddock ID to filter microcontrollers
* @return {Array.<Microcontroller>} 200 - microcontrollers
*/
microcontrollers.get('/', verifyAuthToken, controller.getAllMicrocontrollers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ export default class MicrocontrollerService {
const microcontrollers = await prisma.microcontroller.findMany();
return microcontrollers;
}

public async getMicrocontrollersByPaddock(paddockId: number): Promise<Microcontroller[]> {
const microcontrollers = await prisma.microcontroller.findMany({
where: {
paddockId,
},
});
return microcontrollers;
}
}

0 comments on commit 5d8dd47

Please sign in to comment.