feat(router,template): creation of new publications via dashboard
This commit is contained in:
27
editor/router/dashboard.go
Normal file
27
editor/router/dashboard.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
68
editor/router/publication.go
Normal file
68
editor/router/publication.go
Normal file
@@ -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))
|
||||||
|
}
|
||||||
@@ -29,6 +29,17 @@ func New(cfg Config) http.Handler {
|
|||||||
)
|
)
|
||||||
|
|
||||||
r.Use(middleware.Logger(log.WithGroup("requests")))
|
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
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,31 @@
|
|||||||
{{define "editor-dashboard"}} {{template "layout-base"}}
|
{{define "editor-dashboard"}} {{template "layout-base"}}
|
||||||
|
<body class="bg-gray-900 text-gray-50 has-[#first-publication]:h-svw">
|
||||||
|
<main class="has-[#first-publication]:h-full flex flex-col">
|
||||||
|
{{if .Publications}}
|
||||||
|
<p>Publications</p>
|
||||||
|
{{else}}
|
||||||
|
<h1>Create your first publication</h1>
|
||||||
|
<form method="post" action="/publication/" id="first-publication">
|
||||||
|
<input
|
||||||
|
id="title"
|
||||||
|
type="text"
|
||||||
|
name="title"
|
||||||
|
value="{{if .RandTitle}}{{.RandTitle}}{{end}}"
|
||||||
|
/><button type="submit">Create</button>
|
||||||
|
</form>
|
||||||
|
<style>
|
||||||
|
body:has(:is(#first-publication)) {
|
||||||
|
height: 100svh;
|
||||||
|
& > main {
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
{{end}}
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
{{template "layout-base-end"}} {{end}}
|
{{template "layout-base-end"}} {{end}}
|
||||||
|
|||||||
Reference in New Issue
Block a user