82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
package storm
|
|
|
|
import (
|
|
"forge.cadoles.com/wpetit/gitea-kan/internal/repository"
|
|
"github.com/asdine/storm"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type BoardRepository struct {
|
|
db *storm.DB
|
|
}
|
|
|
|
type boardItem struct {
|
|
ID string `storm:"id"`
|
|
Board *repository.Board
|
|
}
|
|
|
|
func (r *BoardRepository) Init() error {
|
|
if err := r.db.Init(&boardItem{}); err != nil {
|
|
return errors.Wrap(err, "could not init 'boardItem' collection")
|
|
}
|
|
|
|
if err := r.db.ReIndex(&boardItem{}); err != nil {
|
|
return errors.Wrap(err, "could not reindex 'boardItem' collection")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *BoardRepository) List() ([]*repository.Board, error) {
|
|
boardItems := make([]*boardItem, 0)
|
|
|
|
if err := r.db.All(&boardItems); err != nil {
|
|
return nil, errors.Wrap(err, "could not retrieve board items")
|
|
}
|
|
|
|
boards := make([]*repository.Board, 0, len(boardItems))
|
|
|
|
for _, b := range boardItems {
|
|
boards = append(boards, b.Board)
|
|
}
|
|
|
|
return boards, nil
|
|
}
|
|
|
|
func (r *BoardRepository) Get(id repository.BoardID) (*repository.Board, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (r *BoardRepository) Save(board *repository.Board) error {
|
|
b := &boardItem{
|
|
ID: string(board.ID),
|
|
Board: board,
|
|
}
|
|
|
|
if err := r.db.Save(b); err != nil {
|
|
return errors.Wrap(err, "could not save board item")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *BoardRepository) Delete(id repository.BoardID) error {
|
|
b := &boardItem{
|
|
ID: string(id),
|
|
}
|
|
|
|
if err := r.db.DeleteStruct(b); err != nil {
|
|
if err == storm.ErrNotFound {
|
|
return repository.ErrNotFound
|
|
}
|
|
|
|
return errors.Wrapf(err, "could not delete board '%s'", id)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func NewBoardRepository(db *storm.DB) *BoardRepository {
|
|
return &BoardRepository{db}
|
|
}
|