This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
/
contenttype.go
111 lines (92 loc) · 2.91 KB
/
contenttype.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package http
import (
"net/http"
"github.com/asaskevich/govalidator"
"github.com/asdine/storm"
"github.com/labstack/echo"
"github.com/pkg/errors"
"github.com/sbani/amycr/contenttype"
"github.com/sbani/amycr/storage"
"github.com/xeipuuv/gojsonschema"
)
// ContentTypeHandler is the Handler for contenttype
type ContentTypeHandler struct {
Storage storage.ContentTypeManager
}
const (
// ContentTypeHandlerPath is the root path for all contenttype actions
ContentTypeHandlerPath = "/contenttype"
)
// SetRoutes adds the routes related to the handler
func (h *ContentTypeHandler) SetRoutes(e *echo.Echo) {
e.POST(ContentTypeHandlerPath, h.Put)
e.PUT(ContentTypeHandlerPath, h.Put)
e.GET(ContentTypeHandlerPath, h.List)
e.GET(ContentTypeHandlerPath+"/:key", h.Get)
e.DELETE(ContentTypeHandlerPath+"/:key", h.Delete)
}
// Put api call to create or updates a content type.
// Expecting a post request
func (h *ContentTypeHandler) Put(c echo.Context) error {
var ct contenttype.ContentType
// Bind input
if err := c.Bind(&ct); err != nil {
return c.JSON(http.StatusBadRequest, err)
}
// Validate input
if v, err := govalidator.ValidateStruct(ct); err != nil {
return c.JSON(http.StatusBadRequest, err)
} else if !v {
return c.JSON(http.StatusBadRequest, errors.New("Payload did not validate.").Error())
}
// Validate schema
l := gojsonschema.NewStringLoader(ct.Validation)
schema, err := gojsonschema.NewSchema(l)
if err != nil {
return c.JSON(http.StatusBadRequest, errors.Wrap(err, "Schema not valid").Error())
}
ct.SetSchema(schema)
// Put to storage
if err := h.Storage.Put(ct); err != nil {
return c.JSON(http.StatusBadRequest, errors.Wrap(err, "Storage").Error())
}
return c.JSON(http.StatusOK, ct)
}
// List api call lists all content types
func (h *ContentTypeHandler) List(c echo.Context) error {
contentTypes, err := h.Storage.FindAll()
if err != nil {
return c.JSON(http.StatusInternalServerError, errors.Wrap(err, "Storage").Error())
}
return c.JSON(http.StatusOK, contentTypes)
}
// Get api call to get content type info
func (h *ContentTypeHandler) Get(c echo.Context) error {
ct, err := h.Storage.Get(c.Param("key"))
if err != nil {
switch err {
case storm.ErrNotFound:
return ErrContentTypeNotFound
default:
return c.JSON(http.StatusInternalServerError, errors.Wrap(err, "Storage").Error())
}
}
return c.JSON(http.StatusOK, ct)
}
// Delete api action to delete a single content type
func (h *ContentTypeHandler) Delete(c echo.Context) error {
ct, err := h.Storage.Get(c.Param("key"))
if err != nil {
switch err {
case storm.ErrNotFound:
return c.JSON(http.StatusNotFound, "Content type not found")
default:
return c.JSON(http.StatusInternalServerError, errors.Wrap(err, "Storage").Error())
}
}
err = h.Storage.Delete(ct)
if err != nil {
return c.JSON(http.StatusInternalServerError, errors.Wrap(err, "Storage").Error())
}
return c.JSON(http.StatusNoContent, "")
}