From ab61af503e67b11bd94fde803d44ec309b27e1ba Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Wed, 12 Mar 2025 10:22:55 -0300 Subject: [PATCH] feat(router): endpoints for getting and creating projects --- router/router.go | 41 ++++++++++++++++++++++++++++++++++++++++ templates/dashboard.html | 8 +++++--- templates/project.html | 9 +++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 templates/project.html diff --git a/router/router.go b/router/router.go index b6f83d9..5e57817 100644 --- a/router/router.go +++ b/router/router.go @@ -131,6 +131,24 @@ func (router *router) newProject(w http.ResponseWriter, r *http.Request) { router.assert.NotNil(r) router.assert.NotNil(router.service) + if r.Method != http.MethodPost { + exception. + MethodNotAllowed([]string{http.MethodPost}). + ServeHTTP(w, r) + return + } + + router.log.Debug("Creating new project", slog.Any("servce", router.service)) + p, err := router.service.CreateProject() + if err != nil { + exception.InternalServerError(err).ServeHTTP(w, r) + return + } + + router.log.Debug("New project created", slog.String("id", p.ID)) + router.assert.NotZero(p.ID) + + http.Redirect(w, r, path.Join(r.URL.Path, p.ID), http.StatusSeeOther) } func (router *router) getProject(w http.ResponseWriter, r *http.Request) { @@ -153,4 +171,27 @@ func (router *router) getProject(w http.ResponseWriter, r *http.Request) { ServeHTTP(w, r) return } + + p, err := router.service.GetProject(id) + switch { + case errors.Is(err, service.ErrProjectNotExists): + exception.NotFound().ServeHTTP(w, r) + return + + case errors.Is(err, service.ErrProjectInvalidUUID): + exception. + BadRequest(fmt.Errorf("provided ID %q is not valid", id)). + ServeHTTP(w, r) + return + + case err != nil: + exception.InternalServerError(err).ServeHTTP(w, r) + return + } + + err = router.templates.ExecuteTemplate(w, "project", p) + if err != nil { + exception.InternalServerError(err).ServeHTTP(w, r) + return + } } diff --git a/templates/dashboard.html b/templates/dashboard.html index 6315ee0..02a2965 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -5,9 +5,11 @@

Projects

{{else}}
- +
+ +
{{end}} diff --git a/templates/project.html b/templates/project.html new file mode 100644 index 0000000..0613dc6 --- /dev/null +++ b/templates/project.html @@ -0,0 +1,9 @@ +{{define "project"}} +{{template "layout-page-start" (args "Title" .Title)}} +
+

{{.Title}}

+

{{.ID}}

+
{{.Contents}}
+
+{{template "layout-page-end"}} +{{end}}