focalboard/server/app/cards.go
Doug Lauder 4652a15bab
Card APIs (#3760)
* cards apis wip

* create card API

* validate cards when creating

* create card fixes

* patch card wip

* wip

* unit test for createCard; CardPatch2BlockPatch

* unit test for PatchCard

* more APIs

* unit tests for GetCardByID

* register GetCard API

* Set FOCALBOARD_UNIT_TESTING for integration tests

* integration tests for CreateCard

* more integration tests for CreateCard

* integtration tests for PatchCard

* fix integration tests for PatchCard

* integration tests for GetCard

* GetCards API wip

* fix merge conflict

* GetCards API and unit tests

* fix linter issues

* fix flaky unit test for mySQL

* Update server/api/api.go

Co-authored-by: Miguel de la Cruz <mgdelacroix@gmail.com>

* Update server/api/api.go

Co-authored-by: Miguel de la Cruz <mgdelacroix@gmail.com>

* address review comments

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Miguel de la Cruz <mgdelacroix@gmail.com>
2022-09-08 13:01:33 +02:00

96 lines
2.2 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"fmt"
"github.com/mattermost/focalboard/server/model"
"github.com/mattermost/focalboard/server/utils"
)
func (a *App) CreateCard(card *model.Card, boardID string, userID string, disableNotify bool) (*model.Card, error) {
// Convert the card struct to a block and insert the block.
now := utils.GetMillis()
card.ID = utils.NewID(utils.IDTypeCard)
card.BoardID = boardID
card.CreatedBy = userID
card.ModifiedBy = userID
card.CreateAt = now
card.UpdateAt = now
card.DeleteAt = 0
block := model.Card2Block(card)
newBlocks, err := a.InsertBlocksAndNotify([]model.Block{*block}, userID, disableNotify)
if err != nil {
return nil, fmt.Errorf("cannot create card: %w", err)
}
newCard, err := model.Block2Card(&newBlocks[0])
if err != nil {
return nil, err
}
return newCard, nil
}
func (a *App) GetCardsForBoard(boardID string, page int, perPage int) ([]*model.Card, error) {
opts := model.QueryBlocksOptions{
BoardID: boardID,
BlockType: model.TypeCard,
Page: page,
PerPage: perPage,
}
blocks, err := a.store.GetBlocks(opts)
if err != nil {
return nil, err
}
cards := make([]*model.Card, 0, len(blocks))
for _, blk := range blocks {
b := blk
if card, err := model.Block2Card(&b); err != nil {
return nil, fmt.Errorf("Block2Card fail: %w", err)
} else {
cards = append(cards, card)
}
}
return cards, nil
}
func (a *App) PatchCard(cardPatch *model.CardPatch, cardID string, userID string, disableNotify bool) (*model.Card, error) {
blockPatch, err := model.CardPatch2BlockPatch(cardPatch)
if err != nil {
return nil, err
}
newBlock, err := a.PatchBlockAndNotify(cardID, blockPatch, userID, disableNotify)
if err != nil {
return nil, fmt.Errorf("cannot patch card %s: %w", cardID, err)
}
newCard, err := model.Block2Card(newBlock)
if err != nil {
return nil, err
}
return newCard, nil
}
func (a *App) GetCardByID(cardID string) (*model.Card, error) {
cardBlock, err := a.GetBlockByID(cardID)
if err != nil {
return nil, err
}
card, err := model.Block2Card(cardBlock)
if err != nil {
return nil, err
}
return card, nil
}