feat(router): endpoints for getting and creating projects

This commit is contained in:
Guz
2025-03-12 10:22:55 -03:00
parent 8fbb9e1671
commit ab61af503e
3 changed files with 55 additions and 3 deletions

View File

@@ -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
}
}

View File

@@ -5,9 +5,11 @@
<h2>Projects</h2>
{{else}}
<div class="w-full h-screen flex justify-center items-center fixed top">
<button class="bg-slate-700 text-slate-100 p-2 px-5 rounded-full">
New project
</button>
<form action="/projects/" method="post">
<button class="bg-slate-700 text-slate-100 p-2 px-5 rounded-full">
New project
</button>
</form>
</div>
{{end}}
</main>

9
templates/project.html Normal file
View File

@@ -0,0 +1,9 @@
{{define "project"}}
{{template "layout-page-start" (args "Title" .Title)}}
<main class="justify-center align-middle w-full h-full">
<h1>{{.Title}}</h1>
<p>{{.ID}}</p>
<article>{{.Contents}}</article>
</main>
{{template "layout-page-end"}}
{{end}}