gengitkan/internal/route/board.go

47 lines
1.1 KiB
Go

package route
import (
"encoding/json"
"net/http"
"forge.cadoles.com/wpetit/gitea-kan/internal/repository"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
)
func serveBoards(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
repo := repository.Must(ctn)
boards, err := repo.Boards().List()
if err != nil {
panic(errors.Wrap(err, "could not retrieve boards list"))
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(boards); err != nil {
panic(errors.Wrap(err, "could not encode boards list"))
}
}
func saveBoard(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
repo := repository.Must(ctn)
decoder := json.NewDecoder(r.Body)
board := &repository.Board{}
if err := decoder.Decode(board); err != nil {
panic(errors.Wrap(err, "could not decode board"))
}
if err := repo.Boards().Save(board); err != nil {
panic(errors.Wrap(err, "could not save board"))
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(board); err != nil {
panic(errors.Wrap(err, "could not encode board"))
}
}