generated from sumelms/microservice-template
/
delete_lesson.go
58 lines (46 loc) · 1.35 KB
/
delete_lesson.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package endpoints
import (
"context"
"fmt"
"net/http"
"github.com/go-kit/kit/endpoint"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/sumelms/microservice-syllabus/internal/lesson/domain"
)
type deleteLessonRequest struct {
UUID uuid.UUID `json:"uuid" validate:"required"`
}
func NewDeleteLessonHandler(s domain.ServiceInterface, opts ...kithttp.ServerOption) *kithttp.Server {
return kithttp.NewServer(
makeDeleteLessonEndpoint(s),
decodeDeleteLessonRequest,
encodeDeleteLessonResponse,
opts...,
)
}
func makeDeleteLessonEndpoint(s domain.ServiceInterface) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req, ok := request.(deleteLessonRequest)
if !ok {
return nil, fmt.Errorf("invalid argument")
}
if err := s.DeleteLesson(ctx, req.UUID); err != nil {
return nil, err
}
return nil, nil
}
}
func decodeDeleteLessonRequest(_ context.Context, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
id, ok := vars["uuid"]
if !ok {
return nil, fmt.Errorf("invalid argument")
}
uid := uuid.MustParse(id)
return deleteLessonRequest{UUID: uid}, nil
}
func encodeDeleteLessonResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
return kithttp.EncodeJSONResponse(ctx, w, response)
}