|
|
|
@@ -0,0 +1,75 @@ |
|
|
|
package controller |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"encoding/json" |
|
|
|
"net/http" |
|
|
|
|
|
|
|
"github.com/AFASystems/presence/internal/pkg/api/response" |
|
|
|
"github.com/AFASystems/presence/internal/pkg/model" |
|
|
|
"github.com/AFASystems/presence/internal/pkg/validation" |
|
|
|
"github.com/gorilla/mux" |
|
|
|
"gorm.io/gorm" |
|
|
|
) |
|
|
|
|
|
|
|
func FloorAddController(db *gorm.DB, context context.Context) http.HandlerFunc { |
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
var floor model.Floor |
|
|
|
if err := json.NewDecoder(r.Body).Decode(&floor); err != nil { |
|
|
|
response.BadRequest(w, "invalid request body") |
|
|
|
return |
|
|
|
} |
|
|
|
if err := validation.Struct(&floor); err != nil { |
|
|
|
response.BadRequest(w, err.Error()) |
|
|
|
return |
|
|
|
} |
|
|
|
if err := db.WithContext(context).Create(&floor).Error; err != nil { |
|
|
|
response.InternalError(w, "failed to create floor", err) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
response.JSON(w, http.StatusCreated, map[string]string{"status": "created"}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func FloorListController(db *gorm.DB, context context.Context) http.HandlerFunc { |
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
var floors []model.Floor |
|
|
|
if err := db.WithContext(context).Find(&floors).Error; err != nil { |
|
|
|
response.InternalError(w, "failed to list floors", err) |
|
|
|
return |
|
|
|
} |
|
|
|
if err := db.WithContext(context).Find(&floors).Error; err != nil { |
|
|
|
response.InternalError(w, "failed to list floors", err) |
|
|
|
return |
|
|
|
} |
|
|
|
response.JSON(w, http.StatusOK, floors) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func FloorUpdateController(db *gorm.DB, context context.Context) http.HandlerFunc { |
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
var floor model.Floor |
|
|
|
if err := json.NewDecoder(r.Body).Decode(&floor); err != nil { |
|
|
|
response.BadRequest(w, "invalid request body") |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func FloorDeleteController(db *gorm.DB, context context.Context) http.HandlerFunc { |
|
|
|
return func(w http.ResponseWriter, r *http.Request) { |
|
|
|
id := mux.Vars(r)["id"] |
|
|
|
res := db.WithContext(context).Delete(&model.Floor{}, "id = ?", id) |
|
|
|
if res.RowsAffected == 0 { |
|
|
|
response.NotFound(w, "floor not found") |
|
|
|
return |
|
|
|
} |
|
|
|
if res.Error != nil { |
|
|
|
response.InternalError(w, "failed to delete floor", res.Error) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
response.JSON(w, http.StatusOK, map[string]string{"status": "deleted"}) |
|
|
|
} |
|
|
|
} |