dashboard locations and projects will now show items

This commit is contained in:
2026-06-08 14:46:52 +02:00
parent 405502cb20
commit 339d5a709c
2 changed files with 37 additions and 17 deletions

View File

@@ -289,5 +289,5 @@ func UserInfo(w http.ResponseWriter, r *http.Request) {
log.Println("GET [api/userinfo] " + r.RemoteAddr + ": " + err.Error()) log.Println("GET [api/userinfo] " + r.RemoteAddr + ": " + err.Error())
return return
} }
log.Println("GET [api/userinfo] " + r.RemoteAddr + ": Successfully retrieved user info") log.Println("GET [api/userinfo] " + r.RemoteAddr + ": Successfully retrieved user info of " + user.Username + " (" + user.ID + ")")
} }

View File

@@ -21,14 +21,20 @@ func Location(w http.ResponseWriter, r *http.Request) {
contentMode := r.URL.Query().Get("content") contentMode := r.URL.Query().Get("content")
if idStr != "" && contentMode == "true" { if idStr != "" && contentMode == "true" {
_, _ = strconv.Atoi(idStr) locationID, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid location ID", http.StatusBadRequest)
return
}
query := ` query := `
SELECT s.item_id, i.name, s.quantity SELECT s.item_id, i.name, s.quantity
FROM stock s FROM stock s
JOIN items i ON s.item_id = i.id JOIN items i ON s.item_id = i.id
WHERE s.location_id = ? AND s.quantity > 0 WHERE s.location_id = ? AND s.quantity > 0
` `
rows, err := storage.DB.Query(query)
rows, err := storage.DB.Query(query, locationID)
if err != nil { if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError) http.Error(w, "Database error", http.StatusInternalServerError)
return return
@@ -38,9 +44,13 @@ func Location(w http.ResponseWriter, r *http.Request) {
var contents []models.LocationContent var contents []models.LocationContent
for rows.Next() { for rows.Next() {
var c models.LocationContent var c models.LocationContent
rows.Scan(&c.ItemID, &c.ItemName, &c.Quantity) if err := rows.Scan(&c.ItemID, &c.ItemName, &c.Quantity); err != nil {
http.Error(w, "Row scan error", http.StatusInternalServerError)
return
}
contents = append(contents, c) contents = append(contents, c)
} }
json.NewEncoder(w).Encode(map[string]interface{}{"contents": contents}) json.NewEncoder(w).Encode(map[string]interface{}{"contents": contents})
return return
} }
@@ -348,14 +358,20 @@ func Project(w http.ResponseWriter, r *http.Request) {
detailsMode := r.URL.Query().Get("details") detailsMode := r.URL.Query().Get("details")
if idStr != "" && detailsMode == "true" { if idStr != "" && detailsMode == "true" {
_, _ = strconv.Atoi(idStr) projectID, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid project ID", http.StatusBadRequest)
return
}
query := ` query := `
SELECT pi.item_id, i.name, pi.quantity SELECT pi.item_id, i.name, pi.quantity
FROM project_items pi FROM project_items pi
JOIN items i ON pi.item_id = i.id JOIN items i ON pi.item_id = i.id
WHERE pi.project_id = ? WHERE pi.project_id = ?
` `
rows, err := storage.DB.Query(query)
rows, err := storage.DB.Query(query, projectID)
if err != nil { if err != nil {
http.Error(w, "Database error", http.StatusInternalServerError) http.Error(w, "Database error", http.StatusInternalServerError)
return return
@@ -365,9 +381,13 @@ func Project(w http.ResponseWriter, r *http.Request) {
var details []models.ProjectDetailItem var details []models.ProjectDetailItem
for rows.Next() { for rows.Next() {
var d models.ProjectDetailItem var d models.ProjectDetailItem
rows.Scan(&d.ItemID, &d.ItemName, &d.Quantity) if err := rows.Scan(&d.ItemID, &d.ItemName, &d.Quantity); err != nil {
http.Error(w, "Row scan error", http.StatusInternalServerError)
return
}
details = append(details, d) details = append(details, d)
} }
json.NewEncoder(w).Encode(map[string]interface{}{"items": details}) json.NewEncoder(w).Encode(map[string]interface{}{"items": details})
return return
} }