package route import ( "encoding/json" "net/http" "github.com/go-chi/chi" "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")) } w.Header().Add("Content-Type", "application/json") 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")) } w.Header().Add("Content-Type", "application/json") encoder := json.NewEncoder(w) if err := encoder.Encode(board); err != nil { panic(errors.Wrap(err, "could not encode board")) } } 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) }