0

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

BaaDe
  • 77
  • 1
  • 9

1 Answers1

0

If you are using express (it is just an assumption) then:

app.get('/squares/:x/:y', function (req, res) {
  // console.log(req.params.x, req.params.y)
  const {x, y} = req.params
  res.send(yourBigSquare[x][y])
})
cape_bsas
  • 638
  • 5
  • 13
  • I think your code doesn't work because `territories.MATRIZ2` doesn't exists (=== undefined), am I right? – cape_bsas Dec 15 '20 at 23:08
  • you need to have many `territorie` inside `territories`? – cape_bsas Dec 15 '20 at 23:10
  • yes, actually im trying to do that [challenge](https://github.com/vitta-hiring/case-back-end/tree/master/challenges/2-SquareOfSquares) Im doing the 2nd step now – BaaDe Dec 15 '20 at 23:13
  • are you applying to this job? – cape_bsas Dec 15 '20 at 23:18
  • Actually not my friend was, but he was unable to complete the challenge I'm studying node now (I'm new to it) so I thought it would be a good personal challenge (because it would mean that I would be able to go through future processes that I could participate in) – BaaDe Dec 15 '20 at 23:23
  • If I can give you my humble opinion, and it is just that, a opinion of somebody: if I were you I'd start more smoothly, this excercise isn't an easy one at all. It takes a couple of javascript concepts to be very very clear in the programmer to succed in this task. I don't know what language you come from but, to give you an example, in javascript you don't need to (actually you don't) initialize an `Array` – cape_bsas Dec 15 '20 at 23:35
  • Thanks for the advice! I'm always looking to learn a little more, I know it is a challenge that is considerably difficult, but I believe that I need to go after some content, maybe a little more challenging, the minimum I can acquire is already a great help in the future. In this case in question, I find myself without ideas on how to proceed. I have a post route, which I created the matrix to use the indices as coordinates for each square. – BaaDe Dec 15 '20 at 23:52
  • Example: a territory using the post with 5 reach, generates a matriz for me [12345], [12345], [12345], [12345], [12345] I am currently trying in countless ways to access these indexes as a GET route to / squares /: x /: y Whenever I take the vestments by req.params.x or req.params.y and try to get the indexes of the matrix, it returns me for example: TypeError: Cannot read propety '1' of undefined. Can you help me access this matriz to access the route in question? – BaaDe Dec 15 '20 at 23:53
  • First of all, if you don't feel comfortable with the debugger, get help from the old good `console.log`, just go throwing to the console every variable, object or array that you doubt about it's content. Second, your `MATRIZ2` as you've declared it, is a **local variable** inside your `salvarTerritorie` function. That means that it **CAN'T BE ACCESSED** from outside the scope of that function. If you want it to be a property of the `territoires` object then you should use it that way `territoires.MATRIZ2` in the `salvarTerritorie` function – cape_bsas Dec 16 '20 at 01:18
  • I got it! Thank you very much! can you help me with my last problem please? I edited the topic can you take a look? – BaaDe Dec 16 '20 at 02:48