88 "net/http"
99 gin "github.com/gin-gonic/gin"
1010 badger "github.com/dgraph-io/badger"
11+ utils "github.com/polygon-io/errands-server/utils"
12+ schemas "github.com/polygon-io/errands-server/schemas"
1113)
1214
1315
@@ -18,7 +20,7 @@ type UpdateRequest struct {
1820 Logs []string `json:"logs"`
1921}
2022func ( s * ErrandsServer ) updateErrand ( c * gin.Context ){
21- var updatedErrand * Errand
23+ var updatedErrand * schemas. Errand
2224 var updateReq UpdateRequest
2325 if err := c .ShouldBind (& updateReq ); err != nil {
2426 c .JSON (http .StatusBadRequest , gin.H {
@@ -27,7 +29,7 @@ func ( s *ErrandsServer ) updateErrand( c *gin.Context ){
2729 })
2830 return
2931 }
30- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * Errand ) error {
32+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas. Errand ) error {
3133 if errand .Status != "active" {
3234 return errors .New ("Errand must be in active state to update progress" )
3335 }
@@ -63,7 +65,7 @@ type FailedRequest struct {
6365 Reason string `json:"reason" binding:"required"`
6466}
6567func ( s * ErrandsServer ) failedErrand ( c * gin.Context ){
66- var updatedErrand * Errand
68+ var updatedErrand * schemas. Errand
6769 var failedReq FailedRequest
6870 if err := c .ShouldBind (& failedReq ); err != nil {
6971 c .JSON (http .StatusBadRequest , gin.H {
@@ -72,15 +74,15 @@ func ( s *ErrandsServer ) failedErrand( c *gin.Context ){
7274 })
7375 return
7476 }
75- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * Errand ) error {
77+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas. Errand ) error {
7678 // if errand.Status != "active" {
7779 // return errors.New("Errand must be in active state to fail")
7880 // }
7981 // Update this errand attributes:
80- if err := errand .addToLogs ("ERROR" , failedReq .Reason ); err != nil {
82+ if err := errand .AddToLogs ("ERROR" , failedReq .Reason ); err != nil {
8183 return err
8284 }
83- errand .Failed = getTimestamp ()
85+ errand .Failed = utils . GetTimestamp ()
8486 errand .Status = "failed"
8587 errand .Progress = 0
8688 if errand .Options .Retries > 0 {
@@ -112,7 +114,7 @@ type CompletedRequest struct {
112114 Results * gin.H `json:"results"`
113115}
114116func ( s * ErrandsServer ) completeErrand ( c * gin.Context ){
115- var updatedErrand * Errand
117+ var updatedErrand * schemas. Errand
116118 var compReq CompletedRequest
117119 if err := c .ShouldBind (& compReq ); err != nil {
118120 c .JSON (http .StatusBadRequest , gin.H {
@@ -122,15 +124,15 @@ func ( s *ErrandsServer ) completeErrand( c *gin.Context ){
122124 return
123125 }
124126 shouldDelete := false
125- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * Errand ) error {
127+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas. Errand ) error {
126128 // if errand.Status != "active" {
127129 // return errors.New("Errand must be in active state to complete")
128130 // }
129131 // Update this errand attributes:
130- if err := errand .addToLogs ("INFO" , "Completed!" ); err != nil {
132+ if err := errand .AddToLogs ("INFO" , "Completed!" ); err != nil {
131133 return err
132134 }
133- errand .Completed = getTimestamp ()
135+ errand .Completed = utils . GetTimestamp ()
134136 errand .Status = "completed"
135137 errand .Progress = 100
136138 errand .Results = compReq .Results
@@ -163,13 +165,13 @@ func ( s *ErrandsServer ) completeErrand( c *gin.Context ){
163165
164166
165167func ( s * ErrandsServer ) retryErrand ( c * gin.Context ){
166- var updatedErrand * Errand
167- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * Errand ) error {
168+ var updatedErrand * schemas. Errand
169+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas. Errand ) error {
168170 if errand .Status == "inactive" {
169171 return errors .New ("Cannot retry errand which is in inactive state" )
170172 }
171173 // Update this errand attributes:
172- if err := errand .addToLogs ("INFO" , "Retrying!" ); err != nil {
174+ if err := errand .AddToLogs ("INFO" , "Retrying!" ); err != nil {
173175 return err
174176 }
175177 errand .Status = "inactive"
@@ -194,20 +196,20 @@ func ( s *ErrandsServer ) retryErrand( c *gin.Context ){
194196
195197
196198func ( s * ErrandsServer ) logToErrand ( c * gin.Context ){
197- var logReq Log
199+ var logReq schemas. Log
198200 if err := c .ShouldBind (& logReq ); err != nil {
199201 c .JSON (http .StatusBadRequest , gin.H {
200202 "message" : "Invalid Parameters" ,
201203 "error" : err .Error (),
202204 })
203205 return
204206 }
205- updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * Errand ) error {
207+ updatedErrand , err := s .UpdateErrandByID (c .Param ("id" ), func ( errand * schemas. Errand ) error {
206208 if errand .Status != "active" {
207209 return errors .New ("Errand must be in active state to log to" )
208210 }
209211 // Update this errand attributes:
210- return errand .addToLogs (logReq .Severity , logReq .Message )
212+ return errand .AddToLogs (logReq .Severity , logReq .Message )
211213 })
212214 if err != nil {
213215 c .JSON (http .StatusInternalServerError , gin.H {
@@ -260,14 +262,14 @@ func ( s *ErrandsServer ) deleteErrandByID( id string ) error {
260262 If no error is returned, the errand will be saved in the DB with the new
261263 attributes.
262264 */
263- func ( s * ErrandsServer ) UpdateErrandByID ( id string , fn func ( * Errand ) error ) ( * Errand , error ) {
264- var updatedErrand * Errand
265+ func ( s * ErrandsServer ) UpdateErrandByID ( id string , fn func ( * schemas. Errand ) error ) ( * schemas. Errand , error ) {
266+ var updatedErrand * schemas. Errand
265267 err := s .DB .Update (func (txn * badger.Txn ) error {
266268 item , err := txn .Get ([]byte ( id )); if err != nil {
267269 return err
268270 }
269271 err = item .Value (func (v []byte ) error {
270- errand := & Errand {}
272+ errand := & schemas. Errand {}
271273 err := errand .UnmarshalJSON ( v ); if err != nil {
272274 return err
273275 }
0 commit comments