Skip to content

Commit

Permalink
Get filtered errands list, Update/Delete all errands ( filtered )
Browse files Browse the repository at this point in the history
  • Loading branch information
qrpike committed Mar 20, 2019
1 parent 1168fee commit 7c5c297
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 12 deletions.
105 changes: 97 additions & 8 deletions errands-routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ func ( s *ErrandsServer ) saveErrand( txn *badger.Txn, errand *Errand ) error {



func ( s *ErrandsServer ) getErrands( c *gin.Context ){
errands, err := s.GetErrandsBy()
func ( s *ErrandsServer ) getAllErrands( c *gin.Context ){
errands, err := s.GetErrandsBy(func( errand *Errand ) bool {
return true
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
Expand All @@ -80,6 +82,94 @@ func ( s *ErrandsServer ) getErrands( c *gin.Context ){



func ( s *ErrandsServer ) getFilteredErrands( c *gin.Context ){
key := c.Param("key")
value := c.Param("val")
errands, err := s.GetErrandsBy(func( errand *Errand ) bool {
switch key {
case "status":
return ( errand.Status == value )
case "type":
return ( errand.Type == value )
default:
return false
}
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"results": errands,
})
}




type filteredUpdateReq struct {
Status string `json:"status"`
Delete bool `json:"delete"`
}
func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){
key := c.Param("key")
value := c.Param("val")
var updateReq filteredUpdateReq
if err := c.ShouldBind(&updateReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
})
return
}
errands, err := s.GetErrandsBy(func( errand *Errand ) bool {
switch key {
case "status":
return ( errand.Status == value )
case "type":
return ( errand.Type == value )
default:
return false
}
})
if err == nil {
for _, errand := range errands {
if updateReq.Delete == true {
err = s.deleteErrandByID( errand.ID ); if err != nil {
break
}
}else {
if updateReq.Status != "" {
_, err = s.UpdateErrandByID( errand.ID, func( e *Errand ) error {
e.Status = updateReq.Status
return nil
})
if err != nil {
break
}
}
}
}
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"count": len( errands ),
})
}






Expand Down Expand Up @@ -171,9 +261,8 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){



func ( s *ErrandsServer ) GetErrandsBy() ( []Errand, error ) {

errands := []Errand{}
func ( s *ErrandsServer ) GetErrandsBy( fn func ( *Errand ) bool ) ( []*Errand, error ) {
errands := make([]*Errand, 0)
err := s.DB.View(func( txn *badger.Txn ) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 50
Expand All @@ -191,7 +280,9 @@ func ( s *ErrandsServer ) GetErrandsBy() ( []Errand, error ) {
err = dec.Decode( &errand ); if err != nil {
return err
}
errands = append( errands, errand )
if( fn( &errand ) ){
errands = append( errands, &errand )
}
return nil
})
if err != nil {
Expand All @@ -200,9 +291,7 @@ func ( s *ErrandsServer ) GetErrandsBy() ( []Errand, error ) {
}
return nil
})

return errands, err

}


Expand Down
6 changes: 3 additions & 3 deletions errands-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ func ( s *ErrandsServer) createAPI(){
// Create a new errand:
s.ErrandsRoutes.POST("/", s.createErrand)
// Get all errands:
s.ErrandsRoutes.GET("/", s.getErrands)
s.ErrandsRoutes.GET("/", s.getAllErrands)
// Ready to process an errand:
s.ErrandsRoutes.POST("/process", s.processErrand)
// Get all errands in a current type or state:
s.ErrandsRoutes.GET("/list/:key/:val", s.createErrand)
s.ErrandsRoutes.GET("/list/:key/:val", s.getFilteredErrands)
// Update all errands in this state:
s.ErrandsRoutes.POST("/update/:type", s.createErrand)
s.ErrandsRoutes.POST("/update/:key/:val", s.updateFilteredErrands)
}

s.Server = &http.Server{
Expand Down
121 changes: 120 additions & 1 deletion postman/Errands.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,64 @@
},
"response": []
},
{
"name": "Get Filtered Errands",
"event": [
{
"listen": "test",
"script": {
"id": "6c5b1ca0-0441-43c4-91c3-68f0e3fda5b8",
"exec": [
"var schema = {",
" type: \"object\",",
" properties: {",
" status: {",
" type: \"string\"",
" },",
" results: {",
" type: \"array\",",
" items: {",
" $ref: \"errand\",",
" }",
" }",
" }",
"};",
"pm.test(\"Status code is 200\", function (){ ",
" pm.response.to.have.status(200);",
"});",
"pm.test('Schema is valid', function() {",
" pm.expect(tv4.validate(pm.response.json(), schema)).to.be.true;",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "http://localhost:5555/v1/errands/list/status/inactive",
"protocol": "http",
"host": [
"localhost"
],
"port": "5555",
"path": [
"v1",
"errands",
"list",
"status",
"inactive"
]
}
},
"response": []
},
{
"name": "Process Errand - Start",
"event": [
Expand Down Expand Up @@ -506,6 +564,67 @@
},
"response": []
},
{
"name": "Update All Errands ( filtered )",
"event": [
{
"listen": "test",
"script": {
"id": "e243f842-ec20-43f2-a7a9-a568b8251109",
"exec": [
"var jsonData = pm.response.json();",
"var schema = {",
" type: \"object\",",
" properties: {",
" status: {",
" type: \"string\"",
" },",
" count: \"number\"",
" }",
"};",
"pm.test(\"Status code is 200\", function (){ ",
" pm.response.to.have.status(200);",
"});",
"pm.test('Schema is valid', function() {",
" pm.expect(tv4.validate(jsonData, schema)).to.be.true;",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"type": "text",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"status\": \"inactive\"\n}"
},
"url": {
"raw": "http://localhost:5555/v1/errands/update/status/active",
"protocol": "http",
"host": [
"localhost"
],
"port": "5555",
"path": [
"v1",
"errands",
"update",
"status",
"active"
]
}
},
"response": []
},
{
"name": "Delete Errand",
"event": [
Expand Down Expand Up @@ -607,7 +726,7 @@
],
"variable": [
{
"id": "291d10f1-3d30-4b0a-81bd-1cec4032401f",
"id": "28d42a29-c19c-4e20-beae-a96c7a552bd0",
"key": "errand",
"value": "ERRAND_ID",
"type": "string"
Expand Down

0 comments on commit 7c5c297

Please sign in to comment.