Skip to content

Commit

Permalink
API de calendario recibe rango de fechas (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
francosang authored May 7, 2024
1 parent 14d23d2 commit 7a826a8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
4 changes: 2 additions & 2 deletions server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ router.delete("/titulos/:idTitulo", standardAuth, titulosController.deleteTitulo
router.post("/tontos", sudoAuth, tontosController.createTontoHoy);
// Obtener tonto del dia
router.get("/tontos/hoy", tontosController.getTontoHoy);
// Obtener tontos del mes
router.get("/tontos/:year/:month", tontosController.getTontosMes);
// Obtener calendario de tontos entre dos fechas
router.get("/tontos/calendario", tontosController.getTontosPorRango);
// Obtener tonto por id
router.get("/tontos/:idCowboy", tontosController.getTontoPorId);
// Obtener todos los tontos
Expand Down
24 changes: 19 additions & 5 deletions server/tontos/tontos.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,26 @@ async function getTontoHoy(_, res) {
}
}

async function getTontosMes(req, res) {
async function getTontosPorRango(req, res) {
try {
const year = parseInt(req.params.year);
const month = parseInt(req.params.month);
const inicio = req.query.inicio;
const fin = req.query.fin;

const tontos = await tontosService.getTontoByMes(year, month);
console.log("inicio", inicio, "fin", fin);

if (!inicio) {
return res.status(400).json({
message: `Falta el parámetro de 'inicio' en la URL`,
});
}

if (!fin) {
return res.status(400).json({
message: `Falta el parámetro de 'fin' en la URL`,
});
}

const tontos = await tontosService.getTontosPorRango(inicio, fin);
return res.json(tontos);
} catch (err) {
console.error(err);
Expand Down Expand Up @@ -82,5 +96,5 @@ module.exports = {
getTontoHoy,
getAllTontos,
getTontoPorId,
getTontosMes,
getTontosPorRango,
};
10 changes: 5 additions & 5 deletions server/tontos/tontos.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ async function saveTodays(cowboyId) {
return await tontoRepository.getTontoById(cowboyId)
}

async function getTontoByMes(year, month) {
const startOfMonth = new Date(Date.UTC(year, month - 1, 1, 0, 0, 0, 0));
const endOfMonth = new Date(Date.UTC(year, month, 0, 23, 59, 59, 999));
return await tontoRepository.getTontoByPeriod(startOfMonth, endOfMonth);
async function getTontosPorRango(inicio, fin) {
const start = new Date(inicio);
const end = new Date(fin);
return await tontoRepository.getTontoByPeriod(start, end);
}

async function getToday() {
Expand All @@ -30,7 +30,7 @@ async function getAll() {
}

module.exports = {
getTontoByMes,
getTontosPorRango,
getTontoById,
saveTodays,
getToday,
Expand Down

0 comments on commit 7a826a8

Please sign in to comment.