generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
environments_handlers.go
73 lines (59 loc) · 2.35 KB
/
environments_handlers.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
package v1handlers
import (
"errors"
"github.com/broadinstitute/sherlock/internal/controllers/v1controllers"
"github.com/broadinstitute/sherlock/internal/models/v1models"
"net/http"
"github.com/gin-gonic/gin"
)
// ErrBadEnvironmentCreateRequest is an error type used when a create environment request fails validation checks
var ErrBadEnvironmentCreateRequest error = errors.New("error invalid create environment request. environment name is required")
// RegisterEnvironmentHandlers accepts a routergroup and will attach all the handlers for
// working with environment entities to it
func RegisterEnvironmentHandlers(routerGroup *gin.RouterGroup, ec *v1controllers.EnvironmentController) {
routerGroup.GET("", getEnvironments(ec))
routerGroup.GET("/:name", getEnvironmentByName(ec))
routerGroup.POST("", createEnvironment(ec))
}
func getEnvironments(ec *v1controllers.EnvironmentController) func(c *gin.Context) {
return func(c *gin.Context) {
environments, err := ec.ListAll()
if err != nil {
c.JSON(http.StatusInternalServerError, v1controllers.Response{Error: err.Error()})
return
}
c.JSON(http.StatusOK, v1controllers.Response{Environments: ec.Serialize(environments...)})
}
}
func getEnvironmentByName(ec *v1controllers.EnvironmentController) func(c *gin.Context) {
return func(c *gin.Context) {
name := c.Param("name")
environment, err := ec.GetByName(name)
if err != nil {
switch err {
case v1models.ErrEnvironmentNotFound:
c.JSON(http.StatusNotFound, v1controllers.Response{Error: err.Error()})
return
default:
c.JSON(http.StatusInternalServerError, v1controllers.Response{Error: err.Error()})
return
}
}
c.JSON(http.StatusOK, v1controllers.Response{Environments: ec.Serialize(environment)})
}
}
func createEnvironment(ec *v1controllers.EnvironmentController) func(c *gin.Context) {
return func(c *gin.Context) {
var newEnvironment v1models.CreateEnvironmentRequest
if err := c.BindJSON(&newEnvironment); err != nil {
c.JSON(http.StatusBadRequest, v1controllers.Response{Error: ErrBadEnvironmentCreateRequest.Error()})
return
}
savedEnvironment, err := ec.CreateNew(newEnvironment)
if err != nil {
c.JSON(http.StatusInternalServerError, v1controllers.Response{Error: err.Error()})
return
}
c.JSON(http.StatusCreated, v1controllers.Response{Environments: ec.Serialize(savedEnvironment)})
}
}