1-
21package main
32
4-
53import (
64 // "fmt"
75 "errors"
86 "net/http"
7+
98 gin "github.com/gin-gonic/gin"
10- badger "github.com/dgraph-io/badger"
11- utils "github.com/polygon-io/errands-server/utils"
129 schemas "github.com/polygon-io/errands-server/schemas"
10+ utils "github.com/polygon-io/errands-server/utils"
1311)
1412
15-
16-
17-
1813type UpdateRequest struct {
19- Progress float64 `json:"progress"`
20- Logs []string `json:"logs"`
14+ Progress float64 `json:"progress"`
15+ Logs []string `json:"logs"`
2116}
22- func ( s * ErrandsServer ) updateErrand ( c * gin.Context ){
17+
18+ func (s * ErrandsServer ) updateErrand (c * gin.Context ) {
2319 var updatedErrand * schemas.Errand
2420 var updateReq UpdateRequest
2521 if err := c .ShouldBind (& updateReq ); err != nil {
2622 c .JSON (http .StatusBadRequest , gin.H {
2723 "message" : "Invalid Parameters" ,
28- "error" : err .Error (),
24+ "error" : err .Error (),
2925 })
3026 return
3127 }
32- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas.Errand ) error {
28+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func (errand * schemas.Errand ) error {
3329 if errand .Status != "active" {
3430 return errors .New ("Errand must be in active state to update progress" )
3531 }
@@ -38,43 +34,39 @@ func ( s *ErrandsServer ) updateErrand( c *gin.Context ){
3834 if updateReq .Progress < 0 || updateReq .Progress >= 101 {
3935 return errors .New ("Progress must be between 0 - 100" )
4036 }
41- errand .Progress = float64 ( updateReq .Progress )
37+ errand .Progress = float64 (updateReq .Progress )
4238 }
4339 return nil
4440 })
4541 if err != nil {
4642 c .JSON (http .StatusInternalServerError , gin.H {
4743 "message" : "Internal Server Error!" ,
48- "error" : err .Error (),
44+ "error" : err .Error (),
4945 })
5046 return
5147 }
52- s .AddNotification ( "updated" , updatedErrand )
48+ s .AddNotification ("updated" , updatedErrand )
5349 c .JSON (http .StatusOK , gin.H {
54- "status" : "OK" ,
50+ "status" : "OK" ,
5551 "results" : updatedErrand ,
5652 })
5753}
5854
59-
60-
61-
62-
63-
6455type FailedRequest struct {
65- Reason string `json:"reason" binding:"required"`
56+ Reason string `json:"reason" binding:"required"`
6657}
67- func ( s * ErrandsServer ) failedErrand ( c * gin.Context ){
58+
59+ func (s * ErrandsServer ) failedErrand (c * gin.Context ) {
6860 var updatedErrand * schemas.Errand
6961 var failedReq FailedRequest
7062 if err := c .ShouldBind (& failedReq ); err != nil {
7163 c .JSON (http .StatusBadRequest , gin.H {
7264 "message" : "Invalid Parameters" ,
73- "error" : err .Error (),
65+ "error" : err .Error (),
7466 })
7567 return
7668 }
77- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas.Errand ) error {
69+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func (errand * schemas.Errand ) error {
7870 // if errand.Status != "active" {
7971 // return errors.New("Errand must be in active state to fail")
8072 // }
@@ -96,77 +88,70 @@ func ( s *ErrandsServer ) failedErrand( c *gin.Context ){
9688 if err != nil {
9789 c .JSON (http .StatusInternalServerError , gin.H {
9890 "message" : "Internal Server Error!" ,
99- "error" : err .Error (),
91+ "error" : err .Error (),
10092 })
10193 return
10294 }
103- s .AddNotification ( "failed" , updatedErrand )
95+ s .AddNotification ("failed" , updatedErrand )
10496 c .JSON (http .StatusOK , gin.H {
105- "status" : "OK" ,
97+ "status" : "OK" ,
10698 "results" : updatedErrand ,
10799 })
108100}
109101
110-
111-
112-
113102type CompletedRequest struct {
114- Results * gin.H `json:"results"`
103+ Results * gin.H `json:"results"`
115104}
116- func ( s * ErrandsServer ) completeErrand ( c * gin.Context ){
105+
106+ func (s * ErrandsServer ) completeErrand (c * gin.Context ) {
117107 var updatedErrand * schemas.Errand
118108 var compReq CompletedRequest
119109 if err := c .ShouldBind (& compReq ); err != nil {
120110 c .JSON (http .StatusBadRequest , gin.H {
121111 "message" : "Invalid Parameters" ,
122- "error" : err .Error (),
112+ "error" : err .Error (),
123113 })
124114 return
125115 }
126116 shouldDelete := false
127- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas.Errand ) error {
128- // if errand.Status != "active" {
129- // return errors.New("Errand must be in active state to complete")
130- // }
117+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func (errand * schemas.Errand ) error {
131118 // Update this errand attributes:
132119 if err := errand .AddToLogs ("INFO" , "Completed!" ); err != nil {
133120 return err
134121 }
135122 errand .Completed = utils .GetTimestamp ()
136123 errand .Status = "completed"
137124 errand .Progress = 100
138- errand .Results = compReq .Results
125+ // errand.Results = compReq.Results
139126 // If we should delete this errand upon completion:
140127 if errand .Options .DeleteOnCompleted == true {
141128 shouldDelete = true
142129 }
143130 return nil
144131 })
145132 if err == nil && shouldDelete == true && updatedErrand .ID != "" {
146- err = s .deleteErrandByID ( updatedErrand .ID )
133+ err = s .deleteErrandByID (updatedErrand .ID )
147134 }
148- if err != nil {
149- c .JSON (http .StatusInternalServerError , gin.H {
150- "message" : "Internal Server Error!" ,
151- "error" : err .Error (),
152- })
153- return
135+
136+ if shouldDelete == true && updatedErrand .ID != "" {
137+ if err := s .deleteErrandByID (updatedErrand .ID ); err != nil {
138+ c .JSON (http .StatusInternalServerError , gin.H {
139+ "message" : "Internal Server Error!" ,
140+ "error" : err .Error (),
141+ })
142+ return
143+ }
154144 }
155- s .AddNotification ( "completed" , updatedErrand )
145+ s .AddNotification ("completed" , updatedErrand )
156146 c .JSON (http .StatusOK , gin.H {
157- "status" : "OK" ,
147+ "status" : "OK" ,
158148 "results" : updatedErrand ,
159149 })
160150}
161151
162-
163-
164-
165-
166-
167- func ( s * ErrandsServer ) retryErrand ( c * gin.Context ){
152+ func (s * ErrandsServer ) retryErrand (c * gin.Context ) {
168153 var updatedErrand * schemas.Errand
169- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas.Errand ) error {
154+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func (errand * schemas.Errand ) error {
170155 if errand .Status == "inactive" {
171156 return errors .New ("Cannot retry errand which is in inactive state" )
172157 }
@@ -181,30 +166,27 @@ func ( s *ErrandsServer ) retryErrand( c *gin.Context ){
181166 if err != nil {
182167 c .JSON (http .StatusInternalServerError , gin.H {
183168 "message" : "Internal Server Error!" ,
184- "error" : err .Error (),
169+ "error" : err .Error (),
185170 })
186171 return
187172 }
188- s .AddNotification ( "retry" , updatedErrand )
173+ s .AddNotification ("retry" , updatedErrand )
189174 c .JSON (http .StatusOK , gin.H {
190- "status" : "OK" ,
175+ "status" : "OK" ,
191176 "results" : updatedErrand ,
192177 })
193178}
194179
195-
196-
197-
198- func ( s * ErrandsServer ) logToErrand ( c * gin.Context ){
180+ func (s * ErrandsServer ) logToErrand (c * gin.Context ) {
199181 var logReq schemas.Log
200182 if err := c .ShouldBind (& logReq ); err != nil {
201183 c .JSON (http .StatusBadRequest , gin.H {
202184 "message" : "Invalid Parameters" ,
203- "error" : err .Error (),
185+ "error" : err .Error (),
204186 })
205187 return
206188 }
207- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas.Errand ) error {
189+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func (errand * schemas.Errand ) error {
208190 if errand .Status != "active" {
209191 return errors .New ("Errand must be in active state to log to" )
210192 }
@@ -214,26 +196,23 @@ func ( s *ErrandsServer ) logToErrand( c *gin.Context ){
214196 if err != nil {
215197 c .JSON (http .StatusInternalServerError , gin.H {
216198 "message" : "Internal Server Error!" ,
217- "error" : err .Error (),
199+ "error" : err .Error (),
218200 })
219201 return
220202 }
221203 c .JSON (http .StatusOK , gin.H {
222- "status" : "OK" ,
204+ "status" : "OK" ,
223205 "results" : updatedErrand ,
224206 })
225207}
226208
227-
228-
229-
230-
231- func ( s * ErrandsServer ) deleteErrand ( c * gin.Context ){
232- err := s .deleteErrandByID ( c .Param ("id" ) )
209+ func (s * ErrandsServer ) deleteErrand (c * gin.Context ) {
210+ s .Store .Delete (c .Param ("id" ))
211+ err := s .deleteErrandByID (c .Param ("id" ))
233212 if err != nil {
234213 c .JSON (http .StatusInternalServerError , gin.H {
235214 "message" : "Internal Server Error!" ,
236- "error" : err .Error (),
215+ "error" : err .Error (),
237216 })
238217 return
239218 }
@@ -242,48 +221,25 @@ func ( s *ErrandsServer ) deleteErrand( c *gin.Context ){
242221 })
243222}
244223
245-
246-
247-
248-
249-
250-
251-
252- func ( s * ErrandsServer ) deleteErrandByID ( id string ) error {
253- return s .DB .Update (func (txn * badger.Txn ) error {
254- return txn .Delete ([]byte ( id ))
255- })
224+ func (s * ErrandsServer ) deleteErrandByID (id string ) error {
225+ s .Store .Delete (id )
226+ return nil
256227}
257228
258-
259-
260229/*
261- Pass in a function which will be called allowing you to update the errand.
262- If no error is returned, the errand will be saved in the DB with the new
230+ UpdateErrandByID Lets you pass in a function which will be called allowing you to update the errand.
231+ If no error is returned, the errand will be saved in the DB with the new
263232 attributes.
264- */
265- func ( s * ErrandsServer ) UpdateErrandByID ( id string , fn func ( * schemas.Errand ) error ) ( * schemas.Errand , error ) {
266- var updatedErrand * schemas.Errand
267- err := s .DB .Update (func (txn * badger.Txn ) error {
268- item , err := txn .Get ([]byte ( id )); if err != nil {
269- return err
270- }
271- err = item .Value (func (v []byte ) error {
272- errand := & schemas.Errand {}
273- err := errand .UnmarshalJSON ( v ); if err != nil {
274- return err
275- }
276- err = fn ( errand ); if err != nil {
277- return err
278- }
279- updatedErrand = errand
280- err = s .saveErrand ( txn , errand ); if err != nil {
281- return err
282- }
283- return nil
284- })
285- return err
286- })
287- return updatedErrand , err
233+ */
234+ func (s * ErrandsServer ) UpdateErrandByID (id string , fn func (* schemas.Errand ) error ) (* schemas.Errand , error ) {
235+ errandObj , found := s .Store .Get (id )
236+ if ! found {
237+ return nil , errors .New ("Errand with this ID not found" )
238+ }
239+ errand := errandObj .(schemas.Errand )
240+ if err := fn (& errand ); err != nil {
241+ return nil , errors .New ("Error in given update function (fn)" )
242+ }
243+ s .Store .SetDefault (id , errand )
244+ return & errand , nil
288245}
289-
0 commit comments