I have a route that defines the size of my square(one big territory), and within that square I have countless other little squares on a 1x1 scale that is, if my calculation matches, if my biggest square is a 50x50 I have 2500 small squares inside them, and they are all an object that should have this information:
{
"data": {
"x": 1,
"y": 2,
"painted": false
},
"error": false
}
that would be the coordinates of where they are in my big square and whether or not they are painted I should have this return information when I submit a request like this:
GET / squares /: x /: y
where x and y are the coordinates of the small square I chose
the question is How do I create this squares route for the entire area of my largest square? I mean, the amount of objects within my squares route will depend on the size of the larger square that I define then ... how I create a route in a way that is "dynamic" and create objects according to the value of something else ?
Thats my code:
Thats in my bancoDeDados.js
function salvarTerritorie(territorie,area) { //Define o Id seguinte para o territorie ou utiliza um ID definido caso tenha
if (!territorie.id) territorie.id = sequence.id
territories[territorie.id] = territorie
var MATRIZ2 = [];
for (var i = 0; i < area; i++) {
MATRIZ2[i] = [];
for (var j = 0; j < area; j++) {
MATRIZ2[i][j] = ''
}
}
for (var L = 0; L < area; L++) {
for (var C = 0; C < area; C++) {
MATRIZ2[L][C] = C+1
}
}
return territorie
}
And thats in my servidor.js
function getSquares(x,y) {
return territories.MATRIZ2[0][0]
}
//-
app.get('/squares/:x/:y',(req, res, next) => {
// console.log(req.params.x, req.params.y)
const {x, y} = req.params
res.send(bancoDeDados.getSquares(x, y))
})
EDIT Now i need to update my painted attribute using this route:
PATCH /squares/:x/:y/paint
and that should return me:
{
"data": {
"x": 1,
"y": 2,
"painted": true
},
"error": false
}
Until now, im doing like that
function patchSquare(x, y) {
const stringQuadrado = JSON.stringify(territories.matriz)
const dadosQuadrado = JSON.parse(stringQuadrado)
dadosQuadrado[x][y].data.painted = true
return dadosQuadrado[x][y]
}
and it returns me right but it doesn't update because in my route
GET /squares/:x/:y
keep returning me painted: false and I need patchSquare to actually update so that even this get route can get the updated attribute