/
organizations.go
63 lines (55 loc) · 1.51 KB
/
organizations.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
package controller
import (
"github.com/chushi-io/chushi/internal/resource/organization"
"github.com/gin-gonic/gin"
"net/http"
)
type OrganizationsController struct {
Repository organization.OrganizationRepository
}
func NewOrganizationsController(repo organization.OrganizationRepository) *OrganizationsController {
return &OrganizationsController{Repository: repo}
}
func (ctrl *OrganizationsController) SetContext(c *gin.Context) {
org, err := ctrl.Repository.FindById(c.Param("organization"))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
} else {
c.Set("organization", org)
}
}
func (ctrl *OrganizationsController) Get(c *gin.Context) {
org, err := ctrl.Repository.FindById(c.Param("organization"))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
} else {
c.JSON(http.StatusOK, gin.H{
"organization": org,
})
}
}
func (ctrl *OrganizationsController) Create(c *gin.Context) {
var org organization.Organization
if err := c.ShouldBindJSON(&org); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
err := ctrl.Repository.Save(&org)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
} else {
c.JSON(http.StatusOK, gin.H{
"organization": org,
})
}
}
func (ctrl *OrganizationsController) List(c *gin.Context) {
orgs, err := ctrl.Repository.All()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
} else {
c.JSON(http.StatusOK, gin.H{
"organizations": orgs,
})
}
}