Refactored project structure
This commit is contained in:
68
backend/handlers/api_moderator.go
Normal file
68
backend/handlers/api_moderator.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (app *App) HandleGetMenuRaw(w http.ResponseWriter, r *http.Request) {
|
||||
rows, err := app.DB.Query("SELECT id, category, name FROM menu_items ORDER BY id ASC")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []map[string]any
|
||||
for rows.Next() {
|
||||
var id int
|
||||
var category, name string
|
||||
if err := rows.Scan(&id, &category, &name); err == nil {
|
||||
items = append(items, map[string]any{
|
||||
"id": id,
|
||||
"category": category,
|
||||
"name": name,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
writeJSON(w, items)
|
||||
}
|
||||
|
||||
// POST new finalize time
|
||||
func (app *App) HandleSetFinalizeTime(w http.ResponseWriter, r *http.Request) {
|
||||
var in struct {
|
||||
Time string `json:"time"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||||
http.Error(w, "bad request", 400)
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = app.DB.Exec(`INSERT OR REPLACE INTO settings (key,value) VALUES ('finalize_time', ?)`, in.Time)
|
||||
|
||||
// notify scheduler
|
||||
select {
|
||||
case app.FinalizeUpdate <- struct{}{}:
|
||||
default: // don’t block if already queued
|
||||
}
|
||||
|
||||
writeJSON(w, map[string]string{"status": "ok"})
|
||||
}
|
||||
|
||||
// POST trigger finalize now
|
||||
func (app *App) HandleFinalizeNow(w http.ResponseWriter, r *http.Request) {
|
||||
if err := finalizeOrders(app); err != nil {
|
||||
http.Error(w, "failed", 500)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]string{"status": "done"})
|
||||
}
|
||||
|
||||
func (app *App) HandleGetLastSummary(w http.ResponseWriter, r *http.Request) {
|
||||
if app.LastSummary == nil {
|
||||
writeJSON(w, map[string]string{"status": "none"})
|
||||
return
|
||||
}
|
||||
writeJSON(w, app.LastSummary)
|
||||
}
|
||||
Reference in New Issue
Block a user