refactor(router): move projects endpoint to dedicated file

This commit is contained in:
Guz
2025-03-12 14:48:43 -03:00
parent 9e5a15963e
commit a2ca597578
2 changed files with 89 additions and 79 deletions

89
router/projects.go Normal file
View File

@@ -0,0 +1,89 @@
package router
import (
"errors"
"fmt"
"log/slog"
"net/http"
"path"
"forge.capytal.company/capytalcode/project-comicverse/service"
"forge.capytal.company/loreddev/x/smalltrip/exception"
)
func (router *router) projects(w http.ResponseWriter, r *http.Request) {
router.assert.NotNil(w)
router.assert.NotNil(r)
id := r.PathValue("id")
if id != "" {
router.getProject(w, r)
return
}
router.createProject(w, r)
}
func (router *router) createProject(w http.ResponseWriter, r *http.Request) {
router.assert.NotNil(w)
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) {
router.assert.NotNil(w)
router.assert.NotNil(r)
router.assert.NotNil(router.service)
router.assert.NotNil(router.templates)
if r.Method != http.MethodGet && r.Method != http.MethodHead {
exception.
MethodNotAllowed([]string{http.MethodGet, http.MethodHead}).
ServeHTTP(w, r)
return
}
id := r.PathValue("id")
if id == "" {
exception.
BadRequest(fmt.Errorf(`a valid path value of "id" must be provided`)).
ServeHTTP(w, r)
return
}
p, err := router.service.GetProject(id)
switch {
case errors.Is(err, service.ErrProjectNotExists):
exception.NotFound().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

@@ -2,12 +2,10 @@ package router
import (
"errors"
"fmt"
"html/template"
"io/fs"
"log/slog"
"net/http"
"path"
"forge.capytal.company/capytalcode/project-comicverse/service"
"forge.capytal.company/loreddev/x/smalltrip"
@@ -112,80 +110,3 @@ func (router *router) dashboard(w http.ResponseWriter, r *http.Request) {
exception.InternalServerError(err).ServeHTTP(w, r)
}
}
func (router *router) projects(w http.ResponseWriter, r *http.Request) {
router.assert.NotNil(w)
router.assert.NotNil(r)
id := r.PathValue("id")
if id != "" {
router.getProject(w, r)
return
}
router.newProject(w, r)
}
func (router *router) newProject(w http.ResponseWriter, r *http.Request) {
router.assert.NotNil(w)
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) {
router.assert.NotNil(w)
router.assert.NotNil(r)
router.assert.NotNil(router.service)
router.assert.NotNil(router.templates)
if r.Method != http.MethodGet && r.Method != http.MethodHead {
exception.
MethodNotAllowed([]string{http.MethodGet, http.MethodHead}).
ServeHTTP(w, r)
return
}
id := r.PathValue("id")
if id == "" {
exception.
BadRequest(fmt.Errorf(`a valid path value of "id" must be provided`)).
ServeHTTP(w, r)
return
}
p, err := router.service.GetProject(id)
switch {
case errors.Is(err, service.ErrProjectNotExists):
exception.NotFound().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
}
}