generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builds_handlers.go
89 lines (76 loc) · 3.04 KB
/
builds_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package v1handlers
// builds_handlers.go contains all the logic for parsing requests and sending responses for
// the /builds api group. No business logic or database logic should be present in this file.
import (
"errors"
"github.com/broadinstitute/sherlock/internal/controllers/v1controllers"
"github.com/broadinstitute/sherlock/internal/models/v1models"
"github.com/broadinstitute/sherlock/internal/serializers/v1serializers"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
// ErrInvalidBuildID is returned when the getByID method receives an id param that can't be converted to int
var ErrInvalidBuildID error = errors.New("unable to lookup build, received invalid id parameter")
// RegisterBuildHandlers accepts a gin router group and attaches handlers for working
// with build entities
func RegisterBuildHandlers(routerGroup *gin.RouterGroup, bc *v1controllers.BuildController) {
routerGroup.GET("", getBuilds(bc))
routerGroup.POST("", createBuild(bc))
routerGroup.GET("/:id", getByID(bc))
}
func getBuilds(bc *v1controllers.BuildController) func(c *gin.Context) {
return func(c *gin.Context) {
builds, err := bc.ListAll()
if err != nil {
c.JSON(http.StatusInternalServerError, v1serializers.BuildsResponse{Error: err.Error()})
return
}
c.JSON(http.StatusOK, v1serializers.BuildsResponse{Builds: bc.Serialize(builds...)})
}
}
func createBuild(bc *v1controllers.BuildController) func(c *gin.Context) {
return func(c *gin.Context) {
var newBuild v1controllers.CreateBuildRequest
// decode the post request body into a Service struct
if err := c.BindJSON(&newBuild); err != nil {
c.JSON(http.StatusBadRequest, v1serializers.BuildsResponse{Error: v1models.ErrBadCreateRequest.Error()})
return
}
// validate and create new build will ensure that the service id associated
// with the build is valid. If it doesn't exist it will create a new service entity
// and then associate it with the build
build, err := bc.CreateNew(newBuild)
if err != nil {
if errors.Is(err, v1models.ErrDuplicateVersionString) {
c.JSON(http.StatusBadRequest, v1serializers.BuildsResponse{Error: err.Error()})
return
}
c.JSON(http.StatusInternalServerError, v1serializers.BuildsResponse{Error: err.Error()})
return
}
c.JSON(http.StatusCreated, v1serializers.BuildsResponse{Builds: bc.Serialize(build)})
}
}
func getByID(bc *v1controllers.BuildController) func(c *gin.Context) {
return func(c *gin.Context) {
// the id param is a string by default, parse to int
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, v1serializers.BuildsResponse{Error: ErrInvalidBuildID.Error()})
return
}
build, err := bc.GetByID(id)
if err != nil {
switch err {
case v1models.ErrBuildNotFound:
c.JSON(http.StatusNotFound, v1serializers.BuildsResponse{Error: err.Error()})
return
default:
c.JSON(http.StatusInternalServerError, v1serializers.BuildsResponse{Error: err.Error()})
return
}
}
c.JSON(http.StatusOK, v1serializers.BuildsResponse{Builds: bc.Serialize(build)})
}
}