diff --git a/card.go b/card.go index e250ed4..5ebcc9c 100644 --- a/card.go +++ b/card.go @@ -19,6 +19,7 @@ package trello import ( "encoding/json" "net/url" + "strconv" ) type Card struct { @@ -154,14 +155,14 @@ func (c *Card) Actions() (actions []Action, err error) { // AddChecklist will add a checklist to the card. // https://developers.trello.com/advanced-reference/card#post-1-cards-card-id-or-shortlink-checklists func (c *Card) AddChecklist(name string) (*Checklist, error) { - newList := &Checklist{} - payload := url.Values{} payload.Set("name", name) body, err := c.client.Post("/cards/"+c.Id+"/checklists", payload) if err != nil { return nil, err } + + newList := &Checklist{} if err = json.Unmarshal(body, newList); err != nil { return nil, err } @@ -172,10 +173,92 @@ func (c *Card) AddChecklist(name string) (*Checklist, error) { // AddComment will add a new comment to the card // https://developers.trello.com/advanced-reference/card#post-1-cards-card-id-or-shortlink-actions-comments -func (c *Card) AddComment(text string) ([]byte, error) { +func (c *Card) AddComment(text string) (*Action, error) { payload := url.Values{} payload.Set("text", text) body, err := c.client.Post("/cards/"+c.Id+"/actions/comments", payload) - return body, err + if err != nil { + return nil, err + } + + newAction := &Action{} + if err = json.Unmarshal(body, newAction); err != nil { + return nil, err + } + newAction.client = c.client + return newAction, nil +} + +// Archive will archive the card +// https://developers.trello.com/advanced-reference/card#put-1-cards-card-id-or-shortlink-closed +func (c *Card) Archive() (*Card, error) { + payload := url.Values{} + payload.Set("value", "true") + + body, err := c.client.Put("/cards/"+c.Id+"/closed", payload) + if err != nil { + return nil, err + } + + newCard := &Card{} + if err = json.Unmarshal(body, newCard); err != nil { + return nil, err + } + newCard.client = c.client + return newCard, nil +} + +// SendToBoard will dearchive the card, or send the card to the board back from archive +// https://developers.trello.com/advanced-reference/card#put-1-cards-card-id-or-shortlink-closed +func (c *Card) SendToBoard() (*Card, error) { + payload := url.Values{} + payload.Set("value", "false") + + body, err := c.client.Put("/cards/"+c.Id+"/closed", payload) + if err != nil { + return nil, err + } + newCard := &Card{} + if err = json.Unmarshal(body, newCard); err != nil { + return nil, err + } + newCard.client = c.client + return newCard, nil +} + +// MoveToList will move the card to another list +// https://developers.trello.com/advanced-reference/card#put-1-cards-card-id-or-shortlink-idlist +func (c *Card) MoveToList(listId string) (*Card, error) { + payload := url.Values{} + payload.Set("value", listId) + + body, err := c.client.Put("/cards/"+c.Id+"/idList", payload) + if err != nil { + return nil, err + } + newCard := &Card{} + if err = json.Unmarshal(body, newCard); err != nil { + return nil, err + } + newCard.client = c.client + return newCard, nil +} + +// MoveToPos will move card to the specified position +// https://developers.trello.com/advanced-reference/card#put-1-cards-card-id-or-shortlink-pos +func (c *Card) MoveToPos(pos int) (*Card, error) { + payload := url.Values{} + payload.Set("value", strconv.Itoa(pos)) + + body, err := c.client.Put("/cards/"+c.Id+"/pos", payload) + if err != nil { + return nil, err + } + newCard := &Card{} + if err = json.Unmarshal(body, newCard); err != nil { + return nil, err + } + newCard.client = c.client + return newCard, nil } diff --git a/client.go b/client.go index 2f7d03d..6af9682 100644 --- a/client.go +++ b/client.go @@ -66,6 +66,16 @@ func (c *Client) Post(resource string, data url.Values) ([]byte, error) { return c.do(req) } +func (c *Client) Put(resource string, data url.Values) ([]byte, error) { + req, err := http.NewRequest("PUT", c.endpoint+resource, strings.NewReader(data.Encode())) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + return c.do(req) +} + func (c *Client) Delete(resource string) ([]byte, error) { req, err := http.NewRequest("DELETE", c.endpoint+resource, nil) if err != nil {