Skip to content

Commit

Permalink
Merge pull request #18 from Linesmerrill/feature/add-calls-database-a…
Browse files Browse the repository at this point in the history
…nd-models

Feature/add calls database and models
  • Loading branch information
Linesmerrill authored Mar 27, 2022
2 parents 5681d0f + fff2311 commit 70b483a
Show file tree
Hide file tree
Showing 12 changed files with 1,347 additions and 20 deletions.
12 changes: 8 additions & 4 deletions api/handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func (a *App) New() *mux.Router {
f := Firearm{DB: databases.NewFirearmDatabase(a.dbHelper)}
e := Ems{DB: databases.NewEmsDatabase(a.dbHelper)}
ev := EmsVehicle{DB: databases.NewEmsVehicleDatabase(a.dbHelper)}
call := Call{DB: databases.NewCallDatabase(a.dbHelper)}

//healthchex
// healthchex
r.HandleFunc("/health", healthCheckHandler)

apiCreate := r.PathPrefix("/api/v1").Subrouter()
Expand All @@ -64,6 +65,9 @@ func (a *App) New() *mux.Router {
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")
apiCreate.Handle("/call/{call_id}", api.Middleware(http.HandlerFunc(call.CallByIDHandler))).Methods("GET")
apiCreate.Handle("/calls", api.Middleware(http.HandlerFunc(call.CallHandler))).Methods("GET")
apiCreate.Handle("/calls/community/{community_id}", api.Middleware(http.HandlerFunc(call.CallsByCommunityIDHandler))).Methods("GET")

// swagger docs hosted at "/"
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("./docs/"))))
Expand All @@ -75,21 +79,21 @@ func (a *App) Initialize() error {

client, err := databases.NewClient(&a.Config)
if err != nil {
//if we fail to create a new database client, then kill the pod
// if we fail to create a new database client, then kill the pod
zap.S().With(err).Error("failed to create new client")
return err
}

a.dbHelper = databases.NewDatabase(&a.Config, client)
err = client.Connect()
if err != nil {
//if we fail to connect to the database, then kill the pod
// if we fail to connect to the database, then kill the pod
zap.S().With(err).Error("failed to connect to database")
return err
}
zap.S().Info("police-cad-api has connected to the database")

//initialize api router
// initialize api router
a.initializeRoutes()
return nil

Expand Down
111 changes: 111 additions & 0 deletions api/handlers/call.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package handlers

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

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

// Call exported for testing purposes
type Call struct {
DB databases.CallDatabase
}

// CallHandler returns all calls
func (c Call) CallHandler(w http.ResponseWriter, r *http.Request) {
dbResp, err := c.DB.Find(context.TODO(), bson.M{})
if err != nil {
config.ErrorStatus("failed to get calls", http.StatusNotFound, w, err)
return
}
// Because the frontend requires that the data elements inside models.Calls exist, if
// len == 0 then we will just return an empty data object
if len(dbResp) == 0 {
dbResp = []models.Call{}
}
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)
}

// CallByIDHandler returns a call by ID
func (c Call) CallByIDHandler(w http.ResponseWriter, r *http.Request) {
civID := mux.Vars(r)["call_id"]

zap.S().Debugf("call_id: %v", civID)

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

dbResp, err := c.DB.FindOne(context.Background(), bson.M{"_id": cID})
if err != nil {
config.ErrorStatus("failed to get call 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)
}

// CallsByCommunityIDHandler returns all calls that contain the given communityID
func (c Call) CallsByCommunityIDHandler(w http.ResponseWriter, r *http.Request) {
communityID := mux.Vars(r)["community_id"]
status := r.URL.Query().Get("status")
zap.S().Debugf("community_id: '%v'", communityID)
zap.S().Debugf("status: '%v'", status)

statusB, err := strconv.ParseBool(status)
if err != nil {
// if no value is passed or it fails to parse, we will default
// grab the events that are true
statusB = true
err = nil
}

var dbResp []models.Call
if communityID != "" && communityID != "null" && communityID != "undefined" {
dbResp, err = c.DB.Find(context.TODO(), bson.M{
"call.communityID": communityID,
"call.status": statusB,
})
if err != nil {
config.ErrorStatus("failed to get calls with community id", http.StatusNotFound, w, err)
return
}
}

// Because the frontend requires that the data elements inside models.Calls exist, if
// len == 0 then we will just return an empty data object
if len(dbResp) == 0 {
dbResp = []models.Call{}
}
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 70b483a

Please sign in to comment.