diff --git a/router/editor.go b/router/editor.go index 06699cf..67d34d2 100644 --- a/router/editor.go +++ b/router/editor.go @@ -5,7 +5,9 @@ import ( "fmt" "io" "net/http" + "strconv" + "forge.capytal.company/capytalcode/project-comicverse/internals/randstr" "forge.capytal.company/capytalcode/project-comicverse/service" "forge.capytal.company/loreddev/x/smalltrip/exception" ) @@ -14,6 +16,7 @@ func (router *router) pages(w http.ResponseWriter, r *http.Request) { router.assert.NotNil(w) router.assert.NotNil(r) + // TODO: Check if project exists id := r.PathValue("ID") if id == "" { exception. @@ -136,3 +139,102 @@ func (router *router) deletePage(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, fmt.Sprintf("/projects/%s/", id), http.StatusSeeOther) } + +func (router *router) interactions(w http.ResponseWriter, r *http.Request) { + router.assert.NotNil(w) + router.assert.NotNil(r) + + // TODO: Check if the project exists + id := r.PathValue("ID") + if id == "" { + exception. + BadRequest(fmt.Errorf(`a valid path value of "ID" must be provided`)). + ServeHTTP(w, r) + return + } + + // TODO: Check if page exists + pageID := r.PathValue("PageID") + if pageID == "" { + exception. + BadRequest(fmt.Errorf(`a valid path value of "PageID" must be provided`)). + ServeHTTP(w, r) + return + } + + interactionID := r.PathValue("InteractionID") + + switch getMethod(r) { + case http.MethodPost: + router.addInteraction(w, r) + + default: + exception. + MethodNotAllowed([]string{ + http.MethodPost, + }). + ServeHTTP(w, r) + } +} + +func (router *router) addInteraction(w http.ResponseWriter, r *http.Request) { + router.assert.NotNil(w) + router.assert.NotNil(r) + router.assert.NotNil(router.service) + + id := r.PathValue("ID") + router.assert.NotZero(id, "This method should be used after the path values are checked") + + pageID := r.PathValue("PageID") + router.assert.NotZero(pageID, "This method should be used after the path values are checked") + + // TODO: Methods to manipulate interactions, instead of router need to do this logic + page, err := router.service.GetPage(id, pageID) + if err != nil { + exception.InternalServerError(err).ServeHTTP(w, r) + return + } + page.Image = nil // HACK: Prevent image update on S3 + + x, err := strconv.ParseUint(r.FormValue("x"), 10, 0) + if err != nil { + exception. + BadRequest(errors.Join(errors.New(`value "x" should be a valid non-negative integer`), err)). + ServeHTTP(w, r) + return + } + y, err := strconv.ParseUint(r.FormValue("y"), 10, 0) + if err != nil { + exception. + BadRequest(errors.Join(errors.New(`value "y" should be a valid non-negative integer`), err)). + ServeHTTP(w, r) + return + } + + link := r.FormValue("link") + if link == "" { + exception.BadRequest(errors.New(`missing parameter "link" in request`)).ServeHTTP(w, r) + return + } + + intID, err := randstr.NewHex(6) + if err != nil { + exception.InternalServerError(err).ServeHTTP(w, r) + return + } + + page.Interactions[intID] = service.PageInteraction{ + X: uint16(x), + Y: uint16(y), + URL: link, + } + + err = router.service.UpdatePage(id, page) + if err != nil { + exception.InternalServerError(err).ServeHTTP(w, r) + return + } + + http.Redirect(w, r, fmt.Sprintf("/projects/%s/", id), http.StatusSeeOther) +} + diff --git a/router/router.go b/router/router.go index 73ab3bf..9bf907c 100644 --- a/router/router.go +++ b/router/router.go @@ -99,6 +99,8 @@ func (router *router) setup() http.Handler { r.HandleFunc("/projects/{ID}/", router.projects) r.HandleFunc("/projects/{ID}/pages/{$}", router.pages) r.HandleFunc("/projects/{ID}/pages/{PageID}", router.pages) + r.HandleFunc("/projects/{ID}/pages/{PageID}/interactions/{$}", router.interactions) + r.HandleFunc("/projects/{ID}/pages/{PageID}/interactions/{InteractionID}", router.interactions) return r } diff --git a/templates/project.html b/templates/project.html index 6c635f6..e4f8f6f 100644 --- a/templates/project.html +++ b/templates/project.html @@ -7,10 +7,37 @@
- {{range $pageID, $page := .Pages}} -
- -
+ {{range $page := .Pages}} +
+ + +
+
+
+ {{range $key, $interaction := $page.Interactions}} + + + + {{end}} +
+ +
+ +
+ + + + +