generated from sumelms/microservice-template
/
find_course.go
86 lines (74 loc) · 2.2 KB
/
find_course.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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-course/internal/course/domain"
)
type FindCourseRequest struct {
UUID uuid.UUID `json:"uuid"`
}
type FindCourseResponse struct {
Course *CourseResponse `json:"course"`
}
// NewFindCourseHandler find course handler
// @Summary Find course
// @Description Find a new course
// @Tags courses
// @Produce json
// @Param uuid path string true "Course UUID" Format(uuid)
// @Success 200 {object} FindCourseResponse
// @Failure 400 {object} error
// @Failure 404 {object} error
// @Failure 500 {object} error
// @Router /courses/{uuid} [get].
func NewFindCourseHandler(s domain.ServiceInterface, opts ...kithttp.ServerOption) *kithttp.Server {
return kithttp.NewServer(
makeFindCourseEndpoint(s),
decodeFindCourseRequest,
encodeFindCourseResponse,
opts...,
)
}
func makeFindCourseEndpoint(s domain.ServiceInterface) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req, ok := request.(FindCourseRequest)
if !ok {
return nil, fmt.Errorf("invalid argument")
}
course, err := s.Course(ctx, req.UUID)
if err != nil {
return nil, err
}
return &FindCourseResponse{
Course: &CourseResponse{
UUID: course.UUID,
Code: course.Code,
Name: course.Name,
Underline: course.Underline,
Image: course.Image,
ImageCover: course.ImageCover,
Excerpt: course.Excerpt,
Description: course.Description,
CreatedAt: course.CreatedAt,
UpdatedAt: course.UpdatedAt,
},
}, nil
}
}
func decodeFindCourseRequest(_ 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 FindCourseRequest{UUID: uid}, nil
}
func encodeFindCourseResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
return kithttp.EncodeJSONResponse(ctx, w, response)
}