Kaynağa Gözat

feat: implement ORM and DB persistence for zones and tracker zones

master
Blaz Smehov 1 ay önce
ebeveyn
işleme
61f8e9f459
7 değiştirilmiş dosya ile 153 ekleme ve 12 silme
  1. +12
    -2
      cmd/server/main.go
  2. +3
    -7
      internal/pkg/controller/gateways_controller.go
  3. +68
    -0
      internal/pkg/controller/trackerzones_controller.go
  4. +67
    -0
      internal/pkg/controller/zone_controller.go
  5. +1
    -1
      internal/pkg/database/database.go
  6. +1
    -1
      internal/pkg/model/tracker_zones.go
  7. +1
    -1
      internal/pkg/model/zones.go

+ 12
- 2
cmd/server/main.go Dosyayı Görüntüle

@@ -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)


+ 3
- 7
internal/pkg/controller/gateways_controller.go Dosyayı Görüntüle

@@ -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
}


+ 68
- 0
internal/pkg/controller/trackerzones_controller.go Dosyayı Görüntüle

@@ -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"))
}
}

+ 67
- 0
internal/pkg/controller/zone_controller.go Dosyayı Görüntüle

@@ -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"))
}
}

+ 1
- 1
internal/pkg/database/database.go Dosyayı Görüntüle

@@ -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
}



+ 1
- 1
internal/pkg/model/tracker_zones.go Dosyayı Görüntüle

@@ -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"`


+ 1
- 1
internal/pkg/model/zones.go Dosyayı Görüntüle

@@ -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"`
}

Yükleniyor…
İptal
Kaydet