From 61f8e9f459b1d5eb03c40963cd09f3b86af81170 Mon Sep 17 00:00:00 2001 From: blazSmehov Date: Fri, 19 Dec 2025 09:59:54 +0100 Subject: [PATCH] feat: implement ORM and DB persistence for zones and tracker zones --- cmd/server/main.go | 14 +++- .../pkg/controller/gateways_controller.go | 10 +-- .../pkg/controller/trackerzones_controller.go | 68 +++++++++++++++++++ internal/pkg/controller/zone_controller.go | 67 ++++++++++++++++++ internal/pkg/database/database.go | 2 +- internal/pkg/model/tracker_zones.go | 2 +- internal/pkg/model/zones.go | 2 +- 7 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 internal/pkg/controller/trackerzones_controller.go create mode 100644 internal/pkg/controller/zone_controller.go diff --git a/cmd/server/main.go b/cmd/server/main.go index 3f8beb5..a4eb443 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -94,8 +94,18 @@ func main() { r.HandleFunc("/reslevis/getGateways", controller.GatewayListController(db)).Methods("GET") r.HandleFunc("/reslevis/postGateway", controller.GatewayAddController(db)).Methods("POST") - r.HandleFunc("/reslevis/removeGateway/{gateway_id}", controller.GatewayDeleteController(db)).Methods("DELETE") - r.HandleFunc("/reslevis/updateGateway/{gateway_id}", controller.GatewayUpdateController(db)).Methods("PUT") + r.HandleFunc("/reslevis/removeGateway/{id}", controller.GatewayDeleteController(db)).Methods("DELETE") + r.HandleFunc("/reslevis/updateGateway/{id}", controller.GatewayUpdateController(db)).Methods("PUT") + + r.HandleFunc("/reslevis/getZones", controller.ZoneListController(db)).Methods("GET") + r.HandleFunc("/reslevis/postZone", controller.ZoneAddController(db)).Methods("POST") + r.HandleFunc("/reslevis/removeZone/{id}", controller.ZoneDeleteController(db)).Methods("DELETE") + r.HandleFunc("/reslevis/updateZone", controller.ZoneUpdateController(db)).Methods("PUT") + + r.HandleFunc("/reslevis/getTrackerZones", controller.TrackerListController(db)).Methods("GET") + r.HandleFunc("/reslevis/postTrackerZone", controller.TrackerAddController(db)).Methods("POST") + r.HandleFunc("/reslevis/removeTrackerZone/{id}", controller.TrackerDeleteController(db)).Methods("DELETE") + r.HandleFunc("/reslevis/updateTrackerZone", controller.TrackerUpdateController(db)).Methods("PUT") wsHandler := http.HandlerFunc(serveWs(appState, ctx)) restApiHandler := handlers.CORS(originsOk, headersOk, methodsOk)(r) diff --git a/internal/pkg/controller/gateways_controller.go b/internal/pkg/controller/gateways_controller.go index 7d24b84..fbb9324 100644 --- a/internal/pkg/controller/gateways_controller.go +++ b/internal/pkg/controller/gateways_controller.go @@ -2,7 +2,6 @@ package controller import ( "encoding/json" - "fmt" "net/http" "github.com/AFASystems/presence/internal/pkg/model" @@ -29,7 +28,6 @@ func GatewayListController(db *gorm.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var gateways []model.Gateway db.Find(&gateways) - fmt.Printf("gateways: %+v", gateways) res, err := json.Marshal(gateways) if err != nil { http.Error(w, err.Error(), 400) @@ -42,8 +40,7 @@ func GatewayListController(db *gorm.DB) http.HandlerFunc { func GatewayDeleteController(db *gorm.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - id := vars["gateway_id"] + id := mux.Vars(r)["id"] if res := db.Delete(&model.Gateway{}, "id = ?", id); res.RowsAffected == 0 { http.Error(w, "no gateway with such ID found", 400) return @@ -55,10 +52,9 @@ func GatewayDeleteController(db *gorm.DB) http.HandlerFunc { func GatewayUpdateController(db *gorm.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - id := vars["gateway_id"] + id := mux.Vars(r)["id"] - if err := db.First(&model.Gateway{}, id).Error; err != nil { + if err := db.First(&model.Gateway{}, "id = ?", id).Error; err != nil { http.Error(w, err.Error(), 400) return } diff --git a/internal/pkg/controller/trackerzones_controller.go b/internal/pkg/controller/trackerzones_controller.go new file mode 100644 index 0000000..1d61616 --- /dev/null +++ b/internal/pkg/controller/trackerzones_controller.go @@ -0,0 +1,68 @@ +package controller + +import ( + "encoding/json" + "net/http" + + "github.com/AFASystems/presence/internal/pkg/model" + "github.com/gorilla/mux" + "gorm.io/gorm" +) + +// controller/tracker_controller.go +func TrackerAddController(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var tz model.TrackerZones + if err := json.NewDecoder(r.Body).Decode(&tz); err != nil { + http.Error(w, err.Error(), 400) + return + } + db.Create(&tz) + w.Write([]byte("ok")) + } +} + +func TrackerListController(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var list []model.TrackerZones + db.Find(&list) + json.NewEncoder(w).Encode(list) + } +} + +func TrackerUpdateController(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var tz model.TrackerZones + + if err := json.NewDecoder(r.Body).Decode(&tz); err != nil { + http.Error(w, "Invalid JSON", 400) + return + } + + id := tz.ID + + if err := db.First(&model.TrackerZones{}, "id = ?", id).Error; err != nil { + http.Error(w, err.Error(), 400) + return + } + + if err := db.Save(&tz).Error; err != nil { + http.Error(w, err.Error(), 500) + return + } + + w.Write([]byte("ok")) + } +} + +func TrackerDeleteController(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + id := mux.Vars(r)["id"] + if res := db.Delete(&model.TrackerZones{}, "id = ?", id); res.RowsAffected == 0 { + http.Error(w, "no tracker zone with such ID found", 400) + return + } + + w.Write([]byte("ok")) + } +} diff --git a/internal/pkg/controller/zone_controller.go b/internal/pkg/controller/zone_controller.go new file mode 100644 index 0000000..33c49c1 --- /dev/null +++ b/internal/pkg/controller/zone_controller.go @@ -0,0 +1,67 @@ +package controller + +import ( + "encoding/json" + "net/http" + + "github.com/AFASystems/presence/internal/pkg/model" + "github.com/gorilla/mux" + "gorm.io/gorm" +) + +func ZoneAddController(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var zone model.Zone + if err := json.NewDecoder(r.Body).Decode(&zone); err != nil { + http.Error(w, err.Error(), 400) + return + } + db.Create(&zone) + w.Write([]byte("ok")) + } +} + +func ZoneListController(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var zones []model.Zone + db.Find(&zones) + json.NewEncoder(w).Encode(zones) // Groups will appear as ["a", "b"] in JSON + } +} + +func ZoneUpdateController(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var zone model.Zone + + if err := json.NewDecoder(r.Body).Decode(&zone); err != nil { + http.Error(w, err.Error(), 400) + return + } + + id := zone.ID + + if err := db.First(&model.Zone{}, "id = ?", id); err != nil { + http.Error(w, "zone with this ID does not yet exist", 500) + return + } + + if err := db.Save(&zone).Error; err != nil { + http.Error(w, err.Error(), 500) + return + } + + w.Write([]byte("ok")) + } +} + +func ZoneDeleteController(db *gorm.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + id := mux.Vars(r)["id"] + if res := db.Delete(&model.Zone{}, "id = ?", id); res.RowsAffected == 0 { + http.Error(w, "no zone with such ID found", 400) + return + } + + w.Write([]byte("ok")) + } +} diff --git a/internal/pkg/database/database.go b/internal/pkg/database/database.go index 49ec4bd..a39b561 100644 --- a/internal/pkg/database/database.go +++ b/internal/pkg/database/database.go @@ -26,7 +26,7 @@ func Connect(cfg *config.Config) (*gorm.DB, error) { return nil, err } - if err := db.AutoMigrate(&model.Gateway{}); err != nil { + if err := db.AutoMigrate(&model.Gateway{}, model.Zone{}, model.TrackerZones{}); err != nil { return nil, err } diff --git a/internal/pkg/model/tracker_zones.go b/internal/pkg/model/tracker_zones.go index 7197cf8..4f724e6 100644 --- a/internal/pkg/model/tracker_zones.go +++ b/internal/pkg/model/tracker_zones.go @@ -2,7 +2,7 @@ package model type TrackerZones struct { ID string `json:"id"` - ZoneList []string `json:"zoneList"` + ZoneList []string `json:"zoneList" gorm:"serializer:json"` Tracker string `json:"tracker"` Days string `json:"days"` Time string `json:"time"` diff --git a/internal/pkg/model/zones.go b/internal/pkg/model/zones.go index 42cfc00..693e627 100644 --- a/internal/pkg/model/zones.go +++ b/internal/pkg/model/zones.go @@ -3,7 +3,7 @@ package model type Zone struct { ID string `json:"id" gorm:"primaryKey"` Name string `json:"name"` - Groups []string `json:"groups"` + Groups []string `json:"groups" gorm:"serializer:json"` Floor string `json:"floor"` Building string `json:"building"` }