File tree Expand file tree Collapse file tree 4 files changed +81
-1
lines changed Expand file tree Collapse file tree 4 files changed +81
-1
lines changed Original file line number Diff line number Diff line change
1
+ package settingfetcher
2
+
3
+ import (
4
+ "context"
5
+ "encoding/json"
6
+ "strconv"
7
+
8
+ "github.com/gemsorg/assignment/pkg/apierror"
9
+ "github.com/gemsorg/assignment/pkg/authentication"
10
+ ds "github.com/gemsorg/assignment/pkg/datastore"
11
+ "github.com/gemsorg/assignment/pkg/service"
12
+ "github.com/go-kit/kit/endpoint"
13
+ )
14
+
15
+ func makeSettingFetcherEndpoint (svc service.AssignmentService ) endpoint.Endpoint {
16
+ return func (ctx context.Context , request interface {}) (interface {}, error ) {
17
+ data , _ := authentication .ParseAuthData (ctx )
18
+ svc .SetAuthData (data )
19
+ req := request .(SettingRequest )
20
+
21
+ jobID , err := strconv .ParseUint (req .JobID , 10 , 64 )
22
+ if err != nil {
23
+ return nil , errorResponse (err )
24
+ }
25
+
26
+ s , err := svc .GetSettings (jobID )
27
+ if _ , ok := err .(ds.NoRowErr ); ok {
28
+ return json .RawMessage ("{}" ), nil
29
+ }
30
+ if err != nil {
31
+ return nil , errorResponse (err )
32
+ }
33
+ return s , nil
34
+ }
35
+ }
36
+
37
+ func errorResponse (err error ) * apierror.APIError {
38
+ return apierror .New (500 , err .Error (), err )
39
+ }
40
+
41
+ type SettingRequest struct {
42
+ JobID string
43
+ }
Original file line number Diff line number Diff line change
1
+ package settingfetcher
2
+
3
+ import (
4
+ "context"
5
+ "encoding/json"
6
+ "fmt"
7
+ "net/http"
8
+
9
+ "github.com/gemsorg/assignment/pkg/service"
10
+ kithttp "github.com/go-kit/kit/transport/http"
11
+ "github.com/gorilla/mux"
12
+ )
13
+
14
+ func MakeHandler (s service.AssignmentService ) http.Handler {
15
+ return kithttp .NewServer (
16
+ makeSettingFetcherEndpoint (s ),
17
+ decodeSettingFetcherRequest ,
18
+ encodeResponse ,
19
+ )
20
+ }
21
+
22
+ func encodeResponse (ctx context.Context , w http.ResponseWriter , response interface {}) error {
23
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
24
+ return json .NewEncoder (w ).Encode (response )
25
+ }
26
+
27
+ func decodeSettingFetcherRequest (_ context.Context , r * http.Request ) (interface {}, error ) {
28
+ vars := mux .Vars (r )
29
+ var ok bool
30
+ jobID , ok := vars ["job_id" ]
31
+ if ! ok {
32
+ return nil , fmt .Errorf ("missing job_id parameter" )
33
+ }
34
+ return SettingRequest {JobID : jobID }, nil
35
+ }
Original file line number Diff line number Diff line change @@ -20,5 +20,5 @@ func (err AssignmentNotFound) Error() string {
20
20
type NoRowErr struct {}
21
21
22
22
func (err NoRowErr ) Error () string {
23
- return "Query returned no rows "
23
+ return "No Records "
24
24
}
Original file line number Diff line number Diff line change 7
7
"github.com/gemsorg/assignment/pkg/api/assignmentdestroyer"
8
8
"github.com/gemsorg/assignment/pkg/api/assignmentfetcher"
9
9
"github.com/gemsorg/assignment/pkg/api/assignmentupdater"
10
+ "github.com/gemsorg/assignment/pkg/api/settingfetcher"
10
11
11
12
"github.com/gemsorg/assignment/pkg/authentication"
12
13
@@ -29,6 +30,7 @@ func New(
29
30
r .Handle ("/assignments" , assignmentcreator .MakeHandler (s )).Methods ("POST" )
30
31
r .Handle ("/assignments" , assignmentdestroyer .MakeHandler (s )).Methods ("DELETE" )
31
32
r .Handle ("/assignments" , assignmentupdater .MakeHandler (s )).Methods ("PATCH" )
33
+ r .Handle ("/settings/{job_id}" , settingfetcher .MakeHandler (s )).Methods ("GET" )
32
34
r .Use (authentication .AuthMiddleware )
33
35
return withHandlers (r )
34
36
}
You can’t perform that action at this time.
0 commit comments