board.go

  1package cmdjson
  2
  3import (
  4	"github.com/git-bug/git-bug/cache"
  5	"github.com/git-bug/git-bug/entities/board"
  6)
  7
  8type BoardSnapshot struct {
  9	Id         string `json:"id"`
 10	HumanId    string `json:"human_id"`
 11	CreateTime Time   `json:"create_time"`
 12	EditTime   Time   `json:"edit_time"`
 13
 14	Title        string        `json:"title"`
 15	Description  string        `json:"description"`
 16	Participants []Identity    `json:"participants"`
 17	Columns      []BoardColumn `json:"columns"`
 18}
 19
 20func NewBoardSnapshot(snapshot *board.Snapshot) BoardSnapshot {
 21	jsonBoard := BoardSnapshot{
 22		Id:          snapshot.Id().String(),
 23		HumanId:     snapshot.Id().Human(),
 24		CreateTime:  NewTime(snapshot.CreateTime, 0),
 25		EditTime:    NewTime(snapshot.EditTime(), 0),
 26		Title:       snapshot.Title,
 27		Description: snapshot.Description,
 28	}
 29
 30	jsonBoard.Participants = make([]Identity, len(snapshot.Participants))
 31	for i, element := range snapshot.Participants {
 32		jsonBoard.Participants[i] = NewIdentity(element)
 33	}
 34
 35	jsonBoard.Columns = make([]BoardColumn, len(snapshot.Columns))
 36	for i, column := range snapshot.Columns {
 37		jsonBoard.Columns[i] = NewBoardColumn(column)
 38	}
 39
 40	return jsonBoard
 41}
 42
 43type BoardColumn struct {
 44	Id      string `json:"id"`
 45	HumanId string `json:"human_id"`
 46	Name    string `json:"name"`
 47	Items   []any  `json:"items"`
 48}
 49
 50func NewBoardColumn(column *board.Column) BoardColumn {
 51	jsonColumn := BoardColumn{
 52		Id:      column.Id.String(),
 53		HumanId: column.Id.Human(),
 54		Name:    column.Name,
 55	}
 56	jsonColumn.Items = make([]any, len(column.Items))
 57	for j, item := range column.Items {
 58		switch item := item.(type) {
 59		case *board.Draft:
 60			jsonColumn.Items[j] = NewBoardDraftItem(item)
 61		case *board.BugItem:
 62			jsonColumn.Items[j] = NewBugSnapshot(item.Bug.Compile())
 63		default:
 64			panic("unknown item type")
 65		}
 66	}
 67	return jsonColumn
 68}
 69
 70type BoardDraftItem struct {
 71	Id      string   `json:"id"`
 72	HumanId string   `json:"human_id"`
 73	Author  Identity `json:"author"`
 74	Title   string   `json:"title"`
 75	Message string   `json:"message"`
 76}
 77
 78func NewBoardDraftItem(item *board.Draft) BoardDraftItem {
 79	return BoardDraftItem{
 80		Id:      item.CombinedId().String(),
 81		HumanId: item.CombinedId().Human(),
 82		Author:  NewIdentity(item.Author),
 83		Title:   item.Title,
 84		Message: item.Message,
 85	}
 86}
 87
 88type BoardExcerpt struct {
 89	Id         string `json:"id"`
 90	HumanId    string `json:"human_id"`
 91	CreateTime Time   `json:"create_time"`
 92	EditTime   Time   `json:"edit_time"`
 93
 94	Title        string     `json:"title"`
 95	Description  string     `json:"description"`
 96	Participants []Identity `json:"participants"`
 97
 98	Items    int               `json:"items"`
 99	Metadata map[string]string `json:"metadata"`
100}
101
102func NewBoardExcerpt(backend *cache.RepoCache, b *cache.BoardExcerpt) (BoardExcerpt, error) {
103	jsonBoard := BoardExcerpt{
104		Id:          b.Id().String(),
105		HumanId:     b.Id().Human(),
106		CreateTime:  NewTime(b.CreateTime(), b.CreateLamportTime),
107		EditTime:    NewTime(b.EditTime(), b.EditLamportTime),
108		Title:       b.Title,
109		Description: b.Description,
110		Items:       b.ItemCount,
111		Metadata:    b.CreateMetadata,
112	}
113
114	jsonBoard.Participants = make([]Identity, len(b.Participants))
115	for i, element := range b.Participants {
116		participant, err := backend.Identities().ResolveExcerpt(element)
117		if err != nil {
118			return BoardExcerpt{}, err
119		}
120		jsonBoard.Participants[i] = NewIdentityFromExcerpt(participant)
121	}
122	return jsonBoard, nil
123}