diff --git a/editor/router/dashboard.go b/editor/router/dashboard.go new file mode 100644 index 0000000..a139323 --- /dev/null +++ b/editor/router/dashboard.go @@ -0,0 +1,27 @@ +package router + +import ( + "net/http" + + "code.capytal.cc/capytal/comicverse/editor" + "code.capytal.cc/capytal/comicverse/editor/internals/randname" + "code.capytal.cc/loreddev/smalltrip/problem" + "code.capytal.cc/loreddev/x/xtemplate" +) + +type dashboardController struct { + editor *editor.Editor + templater xtemplate.Templater +} + +func (ctrl *dashboardController) dashboard(w http.ResponseWriter, r *http.Request) { + randtitle := randname.New() + + err := ctrl.templater.ExecuteTemplate(w, "editor-dashboard", map[string]any{ + "RandTitle": randtitle, + }) + if err != nil { + problem.NewInternalServerError(err).ServeHTTP(w, r) + return + } +} diff --git a/editor/router/publication.go b/editor/router/publication.go new file mode 100644 index 0000000..a6e2046 --- /dev/null +++ b/editor/router/publication.go @@ -0,0 +1,68 @@ +package router + +import ( + "errors" + "fmt" + "net/http" + + "code.capytal.cc/capytal/comicverse/editor" + "code.capytal.cc/capytal/comicverse/editor/internals/randname" + "code.capytal.cc/loreddev/smalltrip/problem" + "code.capytal.cc/loreddev/x/xtemplate" + "github.com/google/uuid" + "golang.org/x/text/language" +) + +type publicationController struct { + editor *editor.Editor + templater xtemplate.Templater +} + +func (ctrl *publicationController) createPublication(w http.ResponseWriter, r *http.Request) { + title := r.FormValue("title") + if title == "" { + title = randname.New() + } + + lang := language.English + + id, err := uuid.NewV7() + if err != nil { + problem.NewInternalServerError(err).ServeHTTP(w, r) + return + } + + _, err = ctrl.editor.New(id, title, lang) + if err != nil { + problem.NewInternalServerError(err).ServeHTTP(w, r) + return + } + + http.Redirect(w, r, fmt.Sprintf("./%s", id), http.StatusTemporaryRedirect) +} + +func (ctrl *publicationController) getPublication(w http.ResponseWriter, r *http.Request) { + idstr := r.PathValue("publicationID") + if idstr == "" { + problem.NewBadRequest("Missing publication ID in path").ServeHTTP(w, r) + return + } + + id, err := uuid.Parse(idstr) + if err != nil { + problem.NewBadRequest("Invalid UUID in path", problem.WithError(err)).ServeHTTP(w, r) + return + } + + pkg, err := ctrl.editor.Open(id) + if errors.Is(err, editor.ErrNotExists) { + problem.NewNotFound().ServeHTTP(w, r) + return + } else if err != nil { + problem.NewInternalServerError(err).ServeHTTP(w, r) + return + + } + + w.Write(fmt.Appendf([]byte{}, "%+v", pkg)) +} diff --git a/editor/router/router.go b/editor/router/router.go index 7f06730..c7ec2c4 100644 --- a/editor/router/router.go +++ b/editor/router/router.go @@ -29,6 +29,17 @@ func New(cfg Config) http.Handler { ) r.Use(middleware.Logger(log.WithGroup("requests"))) + // r.Use(problem.Middleware(problem.DefaultHandler)) + + r.Handle("GET /assets/{asset...}", http.StripPrefix("/assets/", http.FileServerFS(cfg.Assets))) + + dashboardCtrl := &dashboardController{editor: cfg.Editor, templater: cfg.Templater} + publicationCtrl := &publicationController{editor: cfg.Editor, templater: cfg.Templater} + + r.HandleFunc("GET /{$}", dashboardCtrl.dashboard) + + r.HandleFunc("POST /publication/{$}", publicationCtrl.createPublication) + r.HandleFunc("GET /publication/{publicationID}/{$}", publicationCtrl.getPublication) return r } diff --git a/editor/template/dashboard.html b/editor/template/dashboard.html index e181fae..ca8a544 100644 --- a/editor/template/dashboard.html +++ b/editor/template/dashboard.html @@ -1,2 +1,31 @@ {{define "editor-dashboard"}} {{template "layout-base"}} + +
+ {{if .Publications}} +

Publications

+ {{else}} +

Create your first publication

+
+ +
+ + {{end}} +
+ {{template "layout-base-end"}} {{end}}