@ -1,5 +1,13 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
boards BoardRepository
|
||||
}
|
||||
|
@ -61,6 +61,18 @@ func (r *BoardRepository) Save(board *repository.Board) error {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@ 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"
|
||||
@ -18,6 +20,8 @@ func serveBoards(w http.ResponseWriter, r *http.Request) {
|
||||
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"))
|
||||
@ -39,8 +43,28 @@ func saveBoard(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ func Mount(r *chi.Mux, config *config.Config) {
|
||||
r.Get("/logout", handleLogout)
|
||||
r.Get("/api/boards", serveBoards)
|
||||
r.Post("/api/boards", saveBoard)
|
||||
r.Delete("/api/boards/{boardID}", deleteBoard)
|
||||
r.Handle("/gitea/api/*", http.StripPrefix("/gitea", http.HandlerFunc(proxyAPIRequest)))
|
||||
r.Get("/*", static.Dir(config.HTTP.PublicDir, "", html5PushStateHandler))
|
||||
})
|
||||
|
Reference in New Issue
Block a user