55 "encoding/json"
66 "errors"
77 "fmt"
8+ "log"
89 "net/http"
910
1011 "github.com/bstaijen/mariadb-for-microservices/photo-service/app/models"
@@ -20,7 +21,6 @@ import (
2021 "github.com/bstaijen/mariadb-for-microservices/shared/helper"
2122 sharedModels "github.com/bstaijen/mariadb-for-microservices/shared/models"
2223 "github.com/bstaijen/mariadb-for-microservices/shared/util"
23- "github.com/buger/jsonparser"
2424 jwt "github.com/dgrijalva/jwt-go"
2525)
2626
@@ -74,28 +74,24 @@ func TopRatedHandler(connection *sql.DB, cnf config.Config) negroni.HandlerFunc
7474 return
7575 }
7676
77- data , err := ioutil .ReadAll (res .Body )
78- logrus .Infof ("ipc/toprated: %v" , string (data ))
79-
80- if err == nil && len (data ) > 0 {
81- _ , err := jsonparser .ArrayEach (data , func (value []byte , dataType jsonparser.ValueType , offset int , err error ) {
82-
83- obj := & sharedModels.TopRatedPhotoResponse {}
84- json .Unmarshal (value , obj )
85- photo , err := db .GetPhotoById (connection , obj .PhotoID )
86- if err != nil {
87- logrus .Fatal (err )
88- }
89- if photo != nil {
90- photos = append (photos , photo )
91- }
92- }, "results" )
77+ // Happy path
78+ type Collection struct {
79+ Objects []* sharedModels.TopRatedPhotoResponse `json:"results"`
80+ }
81+ col := & Collection {}
82+ col .Objects = make ([]* sharedModels.TopRatedPhotoResponse , 0 )
83+ err := util .ResponseJSONToObject (res , & col )
84+ if err != nil {
85+ log .Fatal (err )
86+ }
87+ for _ , v := range col .Objects {
88+ photo , err := db .GetPhotoById (connection , v .PhotoID )
9389 if err != nil {
94- util .SendError (w , err )
95- return
90+ logrus .Fatal (err )
91+ }
92+ if photo != nil {
93+ photos = append (photos , photo )
9694 }
97- } else {
98- logrus .Infof ("No results for call to : %v" , url )
9995 }
10096 })
10197 if err != nil {
@@ -137,24 +133,23 @@ func HotHandler(connection *sql.DB, cnf config.Config) negroni.HandlerFunc {
137133 }
138134
139135 // Happy path
140- data , err := ioutil .ReadAll (res .Body )
141- logrus .Infof ("/ipc/hot: %v" , string (data ))
142- if err == nil && len (data ) > 0 {
143- _ , err := jsonparser .ArrayEach (data , func (value []byte , dataType jsonparser.ValueType , offset int , err error ) {
144- obj := & sharedModels.TopRatedPhotoResponse {}
145- json .Unmarshal (value , obj )
146- photo , err := db .GetPhotoById (connection , obj .PhotoID )
147- if err != nil {
148- logrus .Fatal (err )
149- }
150- photos = append (photos , photo )
151- }, "results" )
136+ type Collection struct {
137+ Objects []* sharedModels.TopRatedPhotoResponse `json:"results"`
138+ }
139+ col := & Collection {}
140+ col .Objects = make ([]* sharedModels.TopRatedPhotoResponse , 0 )
141+ err := util .ResponseJSONToObject (res , & col )
142+ if err != nil {
143+ log .Fatal (err )
144+ }
145+ for _ , v := range col .Objects {
146+ photo , err := db .GetPhotoById (connection , v .PhotoID )
152147 if err != nil {
153- util .SendError (w , err )
154- return
148+ logrus .Fatal (err )
149+ }
150+ if photo != nil {
151+ photos = append (photos , photo )
155152 }
156- } else {
157- logrus .Infof ("No results for call to : %v" , url )
158153 }
159154 })
160155 if err != nil {
@@ -343,21 +338,16 @@ func getUsername(cnf config.Config, input []*sharedModels.GetUsernamesRequest) [
343338 }
344339
345340 // Happy path
346- data , err := ioutil .ReadAll (res .Body )
347- logrus .Infof ("/ipc/usernames: %v" , string (data ))
348- defer res .Body .Close ()
349- if err == nil && len (data ) > 0 {
350- _ , err := jsonparser .ArrayEach (data , func (value []byte , dataType jsonparser.ValueType , offset int , err error ) {
351- username := & sharedModels.GetUsernamesResponse {}
352- json .Unmarshal (value , username )
353- usernames = append (usernames , username )
354- }, "usernames" )
355- if err != nil {
356- logrus .Fatal (err )
357- }
358- } else {
359- logrus .Infof ("No results for call to : %v" , url )
341+ type Collection struct {
342+ Objects []* sharedModels.GetUsernamesResponse `json:"usernames"`
343+ }
344+ col := & Collection {}
345+ col .Objects = make ([]* sharedModels.GetUsernamesResponse , 0 )
346+ err := util .ResponseJSONToObject (res , & col )
347+ if err != nil {
348+ log .Fatal (err )
360349 }
350+ usernames = col .Objects
361351 })
362352 if err != nil {
363353 logrus .Fatal (err )
@@ -392,24 +382,16 @@ func getComments(cnf config.Config, input []*sharedModels.CommentRequest) []*sha
392382 }
393383
394384 // Happy path
395- data , err := ioutil .ReadAll (res .Body )
396- logrus .Infof ("/ipc/getLast10: %v" , string (data ))
397- defer res .Body .Close ()
398- if err == nil && len (data ) > 0 {
399- _ , err := jsonparser .ArrayEach (data , func (value []byte , dataType jsonparser.ValueType , offset int , err error ) {
400-
401- logrus .Infof ("getComments: length : %v" , len (value ))
402-
403- comment := & sharedModels.CommentResponse {}
404- json .Unmarshal (value , comment )
405- comments = append (comments , comment )
406- }, "comments" )
407- if err != nil {
408- logrus .Fatal (err )
409- }
410- } else {
411- logrus .Infof ("No results for call to : %v" , url )
385+ type Collection struct {
386+ Objects []* sharedModels.CommentResponse `json:"comments"`
387+ }
388+ col := & Collection {}
389+ col .Objects = make ([]* sharedModels.CommentResponse , 0 )
390+ err := util .ResponseJSONToObject (res , & col )
391+ if err != nil {
392+ log .Fatal (err )
412393 }
394+ comments = col .Objects
413395 })
414396 if err != nil {
415397 logrus .Fatal (err )
@@ -443,21 +425,16 @@ func getCommentCount(cnf config.Config, input []*sharedModels.CommentCountReques
443425 }
444426
445427 // Happy path
446- data , err := ioutil .ReadAll (res .Body )
447- logrus .Infof ("/ipc/getCount: %v" , string (data ))
448- defer res .Body .Close ()
449- if err == nil && len (data ) > 0 {
450- _ , err := jsonparser .ArrayEach (data , func (value []byte , dataType jsonparser.ValueType , offset int , err error ) {
451- comment := & sharedModels.CommentCountResponse {}
452- json .Unmarshal (value , comment )
453- comments = append (comments , comment )
454- }, "result" )
455- if err != nil {
456- logrus .Fatal (err )
457- }
458- } else {
459- logrus .Infof ("No results for call to : %v" , url )
428+ type Collection struct {
429+ Objects []* sharedModels.CommentCountResponse `json:"result"`
430+ }
431+ col := & Collection {}
432+ col .Objects = make ([]* sharedModels.CommentCountResponse , 0 )
433+ err := util .ResponseJSONToObject (res , & col )
434+ if err != nil {
435+ log .Fatal (err )
460436 }
437+ comments = col .Objects
461438 })
462439 if err != nil {
463440 logrus .Fatal (err )
@@ -489,21 +466,16 @@ func getVotes(cnf config.Config, input []*sharedModels.VoteCountRequest) []*shar
489466 }
490467
491468 // Happy path
492- data , err := ioutil .ReadAll (res .Body )
493- logrus .Infof ("/ipc/count: %v" , string (data ))
494- defer res .Body .Close ()
495- if err == nil && len (data ) > 0 {
496- _ , err := jsonparser .ArrayEach (data , func (value []byte , dataType jsonparser.ValueType , offset int , err error ) {
497- vote := & sharedModels.VoteCountResponse {}
498- json .Unmarshal (value , vote )
499- votes = append (votes , vote )
500- }, "results" )
501- if err != nil {
502- logrus .Fatal (err )
503- }
504- } else {
505- logrus .Infof ("No results for call to : %v" , url )
469+ type Collection struct {
470+ Objects []* sharedModels.VoteCountResponse `json:"results"`
471+ }
472+ col := & Collection {}
473+ col .Objects = make ([]* sharedModels.VoteCountResponse , 0 )
474+ err := util .ResponseJSONToObject (res , & col )
475+ if err != nil {
476+ log .Fatal (err )
506477 }
478+ votes = col .Objects
507479 })
508480 if err != nil {
509481 logrus .Fatal (err )
@@ -535,21 +507,16 @@ func voted(cnf config.Config, input []*sharedModels.HasVotedRequest) []*sharedMo
535507 }
536508
537509 // Happy path
538- data , err := ioutil .ReadAll (res .Body )
539- logrus .Infof ("/ipc/voted: %v" , string (data ))
540- defer res .Body .Close ()
541- if err == nil && len (data ) > 0 {
542- _ , err := jsonparser .ArrayEach (data , func (value []byte , dataType jsonparser.ValueType , offset int , err error ) {
543- vote := & sharedModels.HasVotedResponse {}
544- json .Unmarshal (value , vote )
545- hasVoted = append (hasVoted , vote )
546- }, "results" )
547- if err != nil {
548- logrus .Fatal (err )
549- }
550- } else {
551- logrus .Infof ("No results for call to : %v" , url )
510+ type Collection struct {
511+ Objects []* sharedModels.HasVotedResponse `json:"results"`
512+ }
513+ col := & Collection {}
514+ col .Objects = make ([]* sharedModels.HasVotedResponse , 0 )
515+ err := util .ResponseJSONToObject (res , & col )
516+ if err != nil {
517+ log .Fatal (err )
552518 }
519+ hasVoted = col .Objects
553520 })
554521 if err != nil {
555522 logrus .Fatal (err )
0 commit comments