Skip to content

Commit

Permalink
return area on store response
Browse files Browse the repository at this point in the history
  • Loading branch information
medilies committed Mar 31, 2023
1 parent fe5cb2e commit 83f2e9b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
19 changes: 15 additions & 4 deletions internal/controllers/areas_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controllers

import (
"encoding/json"
"fmt"
"io"
"net/http"

Expand Down Expand Up @@ -48,7 +47,7 @@ func (AreaController) Store(w http.ResponseWriter, r *http.Request) {
var req RequestBody
err = json.Unmarshal(body, &req)
if err != nil {
fmt.Println(err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

Expand All @@ -59,12 +58,24 @@ func (AreaController) Store(w http.ResponseWriter, r *http.Request) {
return
}

_, err = stmt.Exec("polygon.Name", req.Perimeter)
// TODO: use a real name
result, err := stmt.Exec("area_name", req.Perimeter)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// TODO: use ID in reponse
_, err = result.LastInsertId()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode("yo")
err = json.NewEncoder(w).Encode(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
6 changes: 2 additions & 4 deletions vite/src/apis/storeArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ export default (geoJson) => {
};

// send a POST request to the API with the GeoJSON data
fetch("https://127.0.0.1:8000/api/areas", {
return fetch("https://127.0.0.1:8000/api/areas", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((data) => {
// handle the response
})
.then((data) => data)
.catch((error) => console.error(error));
};
12 changes: 6 additions & 6 deletions vite/src/mapProviders/leaflet/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ export class Map {
_handleGeometryCreated(event) {
let geoJson = event.layer.toGeoJSON();

storeArea(geoJson);
storeArea(geoJson).then((storedArea) => {
const area = this.addArea(storedArea.perimeter);
// console.log(storedArea);

// TODO: use addArea on successful response
const area = this.addArea(geoJson);

// zoom the map to the geometry
this.map.fitBounds(area.getBounds());
// zoom the map to the geometry
this.map.fitBounds(area.getBounds());
});
}
}

0 comments on commit 83f2e9b

Please sign in to comment.