Skip to content

Commit

Permalink
Merge pull request #17 from Linesmerrill/feature/add-emsVehicles-data…
Browse files Browse the repository at this point in the history
…base-and-models

add new emsVehicle route and tests
  • Loading branch information
Linesmerrill authored Mar 27, 2022
2 parents b4eef4d + c87541b commit 5681d0f
Show file tree
Hide file tree
Showing 9 changed files with 1,287 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (a *App) New() *mux.Router {
v := Vehicle{DB: databases.NewVehicleDatabase(a.dbHelper)}
f := Firearm{DB: databases.NewFirearmDatabase(a.dbHelper)}
e := Ems{DB: databases.NewEmsDatabase(a.dbHelper)}
ev := EmsVehicle{DB: databases.NewEmsVehicleDatabase(a.dbHelper)}

//healthchex
r.HandleFunc("/health", healthCheckHandler)
Expand All @@ -60,6 +61,9 @@ func (a *App) New() *mux.Router {
apiCreate.Handle("/ems/{ems_id}", api.Middleware(http.HandlerFunc(e.EmsByIDHandler))).Methods("GET")
apiCreate.Handle("/ems", api.Middleware(http.HandlerFunc(e.EmsHandler))).Methods("GET")
apiCreate.Handle("/ems/user/{user_id}", api.Middleware(http.HandlerFunc(e.EmsByUserIDHandler))).Methods("GET")
apiCreate.Handle("/emsVehicle/{ems_vehicle_id}", api.Middleware(http.HandlerFunc(ev.EmsVehicleByIDHandler))).Methods("GET")
apiCreate.Handle("/emsVehicles", api.Middleware(http.HandlerFunc(ev.EmsVehicleHandler))).Methods("GET")
apiCreate.Handle("/emsVehicles/user/{user_id}", api.Middleware(http.HandlerFunc(ev.EmsVehiclesByUserIDHandler))).Methods("GET")

// swagger docs hosted at "/"
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./docs/"))))
Expand Down
123 changes: 123 additions & 0 deletions api/handlers/emsVehicle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package handlers

import (
"context"
"encoding/json"
"net/http"

"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"

"github.com/linesmerrill/police-cad-api/config"
"github.com/linesmerrill/police-cad-api/databases"
"github.com/linesmerrill/police-cad-api/models"
)

// EmsVehicle exported for testing purposes
type EmsVehicle struct {
DB databases.EmsVehicleDatabase
}

// EmsVehicleHandler returns all emsVehicles
func (v EmsVehicle) EmsVehicleHandler(w http.ResponseWriter, r *http.Request) {
dbResp, err := v.DB.Find(context.TODO(), bson.M{})
if err != nil {
config.ErrorStatus("failed to get emsVehicles", http.StatusNotFound, w, err)
return
}
// Because the frontend requires that the data elements inside models.EmsVehicles exist, if
// len == 0 then we will just return an empty data object
if len(dbResp) == 0 {
dbResp = []models.EmsVehicle{}
}
b, err := json.Marshal(dbResp)
if err != nil {
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(b)
}

// EmsVehicleByIDHandler returns a emsVehicle by ID
func (v EmsVehicle) EmsVehicleByIDHandler(w http.ResponseWriter, r *http.Request) {
emsVehicleID := mux.Vars(r)["ems_vehicle_id"]

zap.S().Debugf("ems_vehicle_id: %v", emsVehicleID)

evID, err := primitive.ObjectIDFromHex(emsVehicleID)
if err != nil {
config.ErrorStatus("failed to get objectID from Hex", http.StatusBadRequest, w, err)
return
}

dbResp, err := v.DB.FindOne(context.Background(), bson.M{"_id": evID})
if err != nil {
config.ErrorStatus("failed to get emsVehicle by ID", http.StatusNotFound, w, err)
return
}

b, err := json.Marshal(dbResp)
if err != nil {
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(b)
}

// EmsVehiclesByUserIDHandler returns all emsVehicles that contain the given userID
func (v EmsVehicle) EmsVehiclesByUserIDHandler(w http.ResponseWriter, r *http.Request) {
userID := mux.Vars(r)["user_id"]
activeCommunityID := r.URL.Query().Get("active_community_id")

zap.S().Debugf("user_id: '%v'", userID)
zap.S().Debugf("active_community: '%v'", activeCommunityID)

var dbResp []models.EmsVehicle

// If the user is in a community then we want to search for emsVehicles that
// are in that same community. This way each user can have different emsVehicles
// across different communities.
//
// Likewise, if the user is not in a community, then we will display only the emsVehicles
// that are not in a community
var err error
if activeCommunityID != "" && activeCommunityID != "null" && activeCommunityID != "undefined" {
dbResp, err = v.DB.Find(context.TODO(), bson.M{
"emsVehicle.userID": userID,
"emsVehicle.activeCommunityID": activeCommunityID,
})
if err != nil {
config.ErrorStatus("failed to get emsVehicles with active community id", http.StatusNotFound, w, err)
return
}
} else {
dbResp, err = v.DB.Find(context.TODO(), bson.M{
"emsVehicle.userID": userID,
"$or": []bson.M{
{"emsVehicle.activeCommunityID": nil},
{"emsVehicle.activeCommunityID": ""},
},
})
if err != nil {
config.ErrorStatus("failed to get emsVehicles with empty active community id", http.StatusNotFound, w, err)
return
}
}

// Because the frontend requires that the data elements inside models.EmsVehicles exist, if
// len == 0 then we will just return an empty data object
if len(dbResp) == 0 {
dbResp = []models.EmsVehicle{}
}
b, err := json.Marshal(dbResp)
if err != nil {
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(b)
}
Loading

0 comments on commit 5681d0f

Please sign in to comment.