-
-
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.
add route to fetch all spotlight items
- Loading branch information
1 parent
8828bbd
commit e25efba
Showing
5 changed files
with
225 additions
and
0 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,79 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gorilla/mux" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
"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" | ||
) | ||
|
||
// Spotlight exported for testing purposes | ||
type Spotlight struct { | ||
DB databases.SpotlightDatabase | ||
} | ||
|
||
// SpotlightHandler returns all spotlights | ||
func (e Spotlight) SpotlightHandler(w http.ResponseWriter, r *http.Request) { | ||
Limit, err := strconv.Atoi(r.URL.Query().Get("limit")) | ||
if err != nil { | ||
zap.S().Warnf(fmt.Sprintf("limit not set, using default of %v, err: %v", Limit|10, err)) | ||
} | ||
limit64 := int64(Limit) | ||
Page = getPage(Page, r) | ||
skip64 := int64(Page * Limit) | ||
dbResp, err := e.DB.Find(context.TODO(), bson.D{}, &options.FindOptions{Limit: &limit64, Skip: &skip64}) | ||
if err != nil { | ||
config.ErrorStatus("failed to get spotlight", http.StatusNotFound, w, err) | ||
return | ||
} | ||
// Because the frontend requires that the data elements inside models.Spotlight exist, if | ||
// len == 0 then we will just return an empty data object | ||
if len(dbResp) == 0 { | ||
dbResp = []models.Spotlight{} | ||
} | ||
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) | ||
} | ||
|
||
// SpotlightByIDHandler returns a spotlight by ID | ||
func (e Spotlight) SpotlightByIDHandler(w http.ResponseWriter, r *http.Request) { | ||
spotlightID := mux.Vars(r)["spotlight_id"] | ||
|
||
zap.S().Debugf("spotlight_id: %v", spotlightID) | ||
|
||
cID, err := primitive.ObjectIDFromHex(spotlightID) | ||
if err != nil { | ||
config.ErrorStatus("failed to get objectID from Hex", http.StatusBadRequest, w, err) | ||
return | ||
} | ||
|
||
dbResp, err := e.DB.FindOne(context.Background(), bson.M{"_id": cID}) | ||
if err != nil { | ||
config.ErrorStatus("failed to get spotlight 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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
package databases | ||
|
||
// go generate: mockery --name SpotlightDatabase | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/linesmerrill/police-cad-api/models" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
const spotlightName = "spotlight" | ||
|
||
// SpotlightDatabase contains the methods to use with the spotlight database | ||
type SpotlightDatabase interface { | ||
FindOne(context.Context, interface{}, ...*options.FindOneOptions) (*models.Spotlight, error) | ||
Find(context.Context, interface{}, ...*options.FindOptions) ([]models.Spotlight, error) | ||
} | ||
|
||
type spotlightDatabase struct { | ||
db DatabaseHelper | ||
} | ||
|
||
// NewSpotlightDatabase initializes a new instance of user database with the provided db connection | ||
func NewSpotlightDatabase(db DatabaseHelper) SpotlightDatabase { | ||
return &spotlightDatabase{ | ||
db: db, | ||
} | ||
} | ||
|
||
func (c *spotlightDatabase) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (*models.Spotlight, error) { | ||
spotlight := &models.Spotlight{} | ||
err := c.db.Collection(spotlightName).FindOne(ctx, filter, opts...).Decode(&spotlight) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return spotlight, nil | ||
} | ||
|
||
func (c *spotlightDatabase) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]models.Spotlight, error) { | ||
var spotlight []models.Spotlight | ||
err := c.db.Collection(spotlightName).Find(ctx, filter, opts...).Decode(&spotlight) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return spotlight, nil | ||
} |
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,18 @@ | ||
package models | ||
|
||
// Spotlight holds the structure for the spotlight collection in mongo | ||
type Spotlight struct { | ||
ID string `json:"_id" bson:"_id"` | ||
Details SpotlightDetails `json:"spotlight" bson:"spotlight"` | ||
Version int32 `json:"__v" bson:"__v"` | ||
} | ||
|
||
// SpotlightDetails holds the structure for the inner user structure as | ||
// defined in the spotlight collection in mongo | ||
type SpotlightDetails struct { | ||
Image string `json:"image" bson:"image"` | ||
Title string `json:"title" bson:"title"` | ||
Time string `json:"time" bson:"time"` | ||
CreatedAt interface{} `json:"createdAt" bson:"createdAt"` | ||
UpdatedAt interface{} `json:"updatedAt" bson:"updatedAt"` | ||
} |