multy-stub/server/routers/epja-2024-1/ecliptica/plants_calendar/index.js

42 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-10-16 11:06:23 +03:00
const express = require('express')
2024-10-12 23:03:52 +03:00
const plantsRouter = express.Router()
const plants = [
{
name: "Rose",
image: "https://ervanarium.com.br/wp-content/uploads/2019/03/cactus-3142589_1920.jpg",
frequency: 3,
startDate: "2024-10-09",
},
{
name: "Sunflower",
image: "https://avatars.mds.yandex.net/i?id=31da587c9aabc83ad3615023f91d7284781be06c-10701700-images-thumbs&n=13",
frequency: 3,
startDate: "2024-10-05",
},
2024-10-16 11:06:23 +03:00
]
2024-10-12 23:03:52 +03:00
const calculateWateringDates = (startDate, frequency) => {
2024-10-16 11:06:23 +03:00
const dates = []
const start = new Date(startDate)
2024-10-12 23:03:52 +03:00
for (let i = 0; i < 30; i += frequency) {
2024-10-16 11:06:23 +03:00
const nextWateringDate = new Date(start)
nextWateringDate.setDate(start.getDate() + i)
dates.push(nextWateringDate.toISOString().split('T')[0])
2024-10-12 23:03:52 +03:00
}
2024-10-16 11:06:23 +03:00
return dates
}
2024-10-12 23:03:52 +03:00
const plantsWithDates = plants.map(plant => ({
...plant,
wateringDates: calculateWateringDates(plant.startDate, plant.frequency),
2024-10-16 11:06:23 +03:00
}))
2024-10-12 23:03:52 +03:00
2024-10-15 14:43:06 +03:00
plantsRouter.get('/api/plants', (req, res) => {
2024-10-16 11:06:23 +03:00
res.json(plantsWithDates)
})
2024-10-12 23:03:52 +03:00
2024-10-16 11:06:23 +03:00
module.exports = plantsRouter