-
Notifications
You must be signed in to change notification settings - Fork 0
/
trello.go
151 lines (127 loc) · 3.9 KB
/
trello.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package controllers
import (
"errors"
"net/http"
"strings"
"github.com/juliotorresmoreno/trello-app/configs"
trello_service "github.com/juliotorresmoreno/trello-app/internal/app/services/trello-service"
"github.com/labstack/echo/v4"
)
/**
* trelloApi: The objective of this structure is to group all the possible data necessary to
* make it work, for example, if fixed parameters were required or data that does not change
* in each call of its functions.
*/
type TrelloApi struct {
}
func AttachTrelloApi(g *echo.Group) *echo.Group {
c := &TrelloApi{}
g.POST("", c.Create)
g.DELETE("/{id}", c.Delete)
return g
}
type createSchema struct {
Type string `json:"type"`
Title string `json:"title"`
Description string `json:"description"`
Category string `json:"category"`
}
var trelloConf = configs.GetConfig().Trello
var trelloService = trello_service.NewTrelloService(trelloConf.BoardId)
// i could not find how can i create new cards with types.
func getLabels(labels trello_service.Labels, Type string, Category string) []string {
list := make([]string, 0)
if label, ok := labels[Type]; ok {
list = append(list, label.Id)
}
if Type == "task" && Category == "Maintenance" {
category := strings.ToLower(Category)
if label, ok := labels[category]; ok {
list = append(list, label.Id)
}
}
return list
}
type strSlice []string
func (slice strSlice) SliceIndex(value string) int {
for p, v := range slice {
if v == value {
return p
}
}
return -1
}
func createValidation(payload *createSchema) error {
if payload.Title == "" {
return errors.New("title is required")
}
if payload.Type == "" {
return errors.New("type is required")
}
types := strSlice{"task", "bug", "issue"}
if types.SliceIndex(payload.Type) < 0 {
return errors.New("title only can be: task, bug or issue")
}
if payload.Type == "task" {
types := strSlice{"Maintenance"}
if types.SliceIndex(payload.Category) < 0 {
return errors.New("category only can be: Maintenance")
}
}
return nil
}
// This is the place where the magic happens. It is the controller that registers card creation requests in Trello
func (t TrelloApi) Create(c echo.Context) error {
var err error
payload := new(createSchema)
err = c.Bind(payload)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": err.Error(),
})
}
response := map[string]interface{}{}
err = createValidation(payload)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]interface{}{
"message": err.Error(),
})
}
Alllabels, err := trelloService.GetLabels()
if err != nil {
return c.JSON(http.StatusInternalServerError, InternalServerErrorResponse)
}
labels := getLabels(Alllabels, payload.Type, payload.Category)
data := trello_service.CreateCardScheme{
Name: payload.Title,
Desc: payload.Description,
IdLabels: labels,
}
card, err := trelloService.CreateCard(data)
if err != nil {
return c.JSON(http.StatusInternalServerError, InternalServerErrorResponse)
}
response["id"] = card.ID
// It's ideal that you do not send answer because you are send status code 201.
return c.JSON(http.StatusCreated, response)
}
// This is the place where the magic happens. It is the controller that registers card creation requests in Trello
func (t TrelloApi) Delete(c echo.Context) error {
var err error
id := c.Param("id")
response := map[string]interface{}{}
err = trelloService.DeleteCard(id)
if err != nil {
return c.JSON(http.StatusInternalServerError, InternalServerErrorResponse)
}
return c.JSON(http.StatusNoContent, response)
}
// This is the place where the magic happens. It is the controller that registers card creation requests in Trello
func (t TrelloApi) Get(c echo.Context) error {
var err error
cards, err := trelloService.GetCards()
if err != nil {
return c.JSON(http.StatusInternalServerError, InternalServerErrorResponse)
}
return c.JSON(http.StatusOK, cards)
}