-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from Linesmerrill/feature/add-calls-database-a…
…nd-models Feature/add calls database and models
- Loading branch information
Showing
12 changed files
with
1,347 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.