Skip to content

Commit

Permalink
feat(api): allow toggling timers and clearing intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisrijsdijk committed Feb 8, 2024
1 parent 524974e commit f2048be
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
66 changes: 54 additions & 12 deletions src/server/api/v1/controllers/timersApiController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import timersManager from "../../../../backend/timers/timer-manager";
import { Request, Response } from "express";
import { Timer } from "../../../../types/timers";

function findTimer(req: Request, res: Response): Timer | undefined {
const timerId: string = req.params.timerId;

if (!(timerId.length > 0)) {
res.status(400).send({
status: "error",
message: "No timerId provided"
});
return undefined;
}

const timer = timersManager.getItem(timerId);

if (timer == null) {
res.status(404).send({
status: "error",
message: `Timer '${timerId}' not found`
});
return undefined;
}

return timer;
}

export async function getTimers(req: Request, res: Response): Promise<Response> {
const timers = timersManager.getAllItems()
Expand All @@ -15,23 +40,40 @@ export async function getTimers(req: Request, res: Response): Promise<Response>
}

export async function getTimerById(req: Request, res: Response): Promise<Response> {
const timerId: string = req.params.timerId;
const timer = findTimer(req, res);
if (!timer) {
return;
}

if (!(timerId.length > 0)) {
return res.status(400).send({
status: "error",
message: "No timerId provided"
});
return res.json(timer);
}

export async function updateTimerById(req: Request, res: Response): Promise<Response> {
const timer = findTimer(req, res);
if (!timer) {
return;
}

const timer = timersManager.getItem(timerId);
const action: string = req.params.action;

if (timer == null) {
return res.status(404).send({
if (
action !== "enable" &&
action !== "disable" &&
action !== "toggle" &&
action !== "clear"
) {
return res.status(400).send({
status: "error",
message: `Timer '${timerId}' not found`
message: "invalid action provided"
});
}

return res.json(timer);
}
if (action === "clear") {
timersManager.updateIntervalForTimer(timer);
return res.status(200).send();
}

const isActive = action === "toggle" ? !timer.active : action === "enable";
timersManager.updateTimerActiveStatus(timer.id, isActive);
return res.status(200).send();
}
5 changes: 5 additions & 0 deletions src/server/api/v1/v1Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ router
.route("/timers/:timerId")
.get(timers.getTimerById);

// Action can be "toggle", "enable", "disable" or "clear"
router
.route("/timers/:timerId/:action")
.get(timers.updateTimerById);

const queues = require("./controllers/effectQueuesApiController");

router
Expand Down

0 comments on commit f2048be

Please sign in to comment.