2019-12-01 22:12:13 +01:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2019-12-13 13:28:59 +01:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
|
2019-12-01 22:12:13 +01:00
|
|
|
"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"))
|
|
|
|
}
|
|
|
|
|
2019-12-13 13:28:59 +01:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
2019-12-01 22:12:13 +01:00
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
2019-12-13 13:28:59 +01:00
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
|
2019-12-01 22:12:13 +01:00
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
if err := encoder.Encode(board); err != nil {
|
|
|
|
panic(errors.Wrap(err, "could not encode board"))
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 13:28:59 +01:00
|
|
|
|
|
|
|
func deleteBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
boardID := repository.BoardID(chi.URLParam(r, "boardID"))
|
|
|
|
|
|
|
|
ctn := container.Must(r.Context())
|
|
|
|
repo := repository.Must(ctn)
|
|
|
|
|
|
|
|
if err := repo.Boards().Delete(boardID); err != nil {
|
|
|
|
if err == repository.ErrNotFound {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|