From dcc6dc0b98a3999d07da54d347e966834dba5ffd Mon Sep 17 00:00:00 2001 From: miaurizius Date: Sun, 7 Jun 2026 23:28:03 +0200 Subject: [PATCH] item and location names will be shown instead of their ids --- frontend/assets/js/api.js | 15 +++++++++------ handlers/api.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/frontend/assets/js/api.js b/frontend/assets/js/api.js index f966733..82f073d 100644 --- a/frontend/assets/js/api.js +++ b/frontend/assets/js/api.js @@ -288,17 +288,18 @@ async function reloadStockTable(itemId) { return; } - data.stock.forEach(st => { + for (const st of data.stock) { + const locData = await apiRequest(`/api/location?id=${st.location_id}`); tbody.innerHTML += ` - ${st.location_name || `Location #${st.location_id}`} + ${locData.name || `Location #${st.location_id}`} ${st.quantity} `; - }); + } } catch (e) { tbody.innerHTML = 'Failed to load.'; } @@ -358,17 +359,19 @@ async function reloadAssociationTable(projectId) { return; } - data.associations.forEach(asc => { + for (const asc of data.associations) { + const itemData = await apiRequest(`/api/item?id=${asc.item_id}`); + console.log(itemData) tbody.innerHTML += ` - ${asc.item_name || `Item #${asc.item_id}`} + ${itemData.name || `Item #${asc.item_id}`} ${asc.quantity} `; - }); + } } catch (e) { tbody.innerHTML = 'Failed to load.'; } diff --git a/handlers/api.go b/handlers/api.go index 4a516a4..85d56d1 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -214,6 +214,34 @@ func Item(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: + + idStr := r.URL.Query().Get("id") + + if idStr != "" { + id, err := strconv.Atoi(idStr) + if err != nil { + http.Error(w, "Invalid ID parameter", http.StatusBadRequest) + return + } + + var item models.Item + err = storage.DB.QueryRow("SELECT id, name, category, description, total_quantity FROM items WHERE id = ?", id).Scan(&item.ID, &item.Name, &item.Category, &item.Description, &item.TotalQuantity) + if err != nil { + if err == sql.ErrNoRows { + log.Println("GET [api/locations] " + r.RemoteAddr + ": Location not found (ID " + idStr + ")") + http.Error(w, "Location not found", http.StatusNotFound) + return + } + log.Println("GET [api/item] DB Error: " + err.Error()) + http.Error(w, "Internal server error", http.StatusInternalServerError) + return + } + + json.NewEncoder(w).Encode(item) + log.Println("GET [api/item] " + r.RemoteAddr + ": Successfully retrieved item ID " + idStr) + return + } + query := ` SELECT i.id, i.name, i.category, i.description,