From 4c9a8d80136c033b235cf62aaaeefdc74357adf1 Mon Sep 17 00:00:00 2001 From: lauralani Date: Fri, 16 Jun 2023 12:46:08 +0200 Subject: [PATCH] add short link edit option --- .woodpecker/deploy-docker.yml | 10 ++++---- CHANGELOG.md | 4 ++++ internal/api/links.go | 44 ++++++++++++++++++++++++++++++++++- views/edit_link.tmpl | 2 +- web/custom.css | 4 ++++ web/main.js | 43 +++++++++++++++++++++++++++++----- 6 files changed, 94 insertions(+), 13 deletions(-) diff --git a/.woodpecker/deploy-docker.yml b/.woodpecker/deploy-docker.yml index b58b347..acee694 100644 --- a/.woodpecker/deploy-docker.yml +++ b/.woodpecker/deploy-docker.yml @@ -1,12 +1,12 @@ -pipeline: +steps: docker-deploy-push: when: - event: - - push + - event: push + branch: main image: woodpeckerci/plugin-docker-buildx settings: dockerfile: Dockerfile - platforms: linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le + platforms: linux/arm/v7,linux/arm64/v8,linux/amd64 repo: codeberg.org/lauralani/go-urlsh registry: codeberg.org tags: latest @@ -21,7 +21,7 @@ pipeline: image: woodpeckerci/plugin-docker-buildx settings: dockerfile: Dockerfile - platforms: linux/arm/v7,linux/arm64/v8,linux/amd64,linux/ppc64le + platforms: linux/arm/v7,linux/arm64/v8,linux/amd64 repo: codeberg.org/lauralani/go-urlsh registry: codeberg.org auto_tag: true diff --git a/CHANGELOG.md b/CHANGELOG.md index f3e82a5..ff5cee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,10 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - added Changelog +- Ability to change the url and description of shortlinks ### Changed - License changed to MIT +### Removed +- Docker image support for linux/ppc64le + ## [0.1.2] - 2014-08-09 ### Fixed diff --git a/internal/api/links.go b/internal/api/links.go index 1e36a66..2bbd29d 100644 --- a/internal/api/links.go +++ b/internal/api/links.go @@ -5,6 +5,7 @@ import ( "codeberg.org/lauralani/go-urlsh/internal/misc" "codeberg.org/lauralani/go-urlsh/models" "context" + "database/sql" "encoding/json" "fmt" "github.com/gofiber/fiber/v2" @@ -35,7 +36,7 @@ func HandleLinkGetAll(c *fiber.Ctx) error { } func HandleLinkGet(c *fiber.Ctx) error { - return nil + return fiber.NewError(fiber.StatusNotImplemented, "501 Not Implemented") } func HandleLinkPost(c *fiber.Ctx) error { @@ -77,6 +78,47 @@ func HandleLinkPost(c *fiber.Ctx) error { } func HandleLinkPut(c *fiber.Ctx) error { + if !db.IsCookieValid(c.Cookies(misc.CookieName, "")) && !db.IsApiKeyValid(c.GetRespHeader("x-api-key", "")) { + return fiber.NewError(fiber.StatusUnauthorized, "401 Unauthorized") + } + + var dblink models.Link + var editlink models.Link + + id := c.Params("id") + if id == "" { + return fiber.NewError(fiber.StatusBadRequest, "400 Bad Request") + } + + err := models.DB.NewSelect().Model(&dblink).Where("id = ?", id).Scan(context.Background()) + + if err != nil { + if err == sql.ErrNoRows { + log.Printf("[HandleAdminLinkEditGet] Shortlink %v not found\n", id) + return fiber.NewError(fiber.StatusNotFound, "404 Not Found") + } else { + log.Printf("[HandleAdminLinkEditGet] Error querying Shortlink %v from database: %v\n", id, err) + return fiber.NewError(fiber.StatusInternalServerError, "500 Internal Server Error") + } + } + + err = json.Unmarshal(c.Body(), &editlink) + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, "400 Bad Request: "+err.Error()) + } + + dblink.Description = editlink.Description + dblink.URL = editlink.URL + dblink.Modified = time.Now() + + _, err = models.DB.NewUpdate().Model(&dblink).Where("id = ?", id).Exec(context.Background()) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, "500 Internal Server Error: "+err.Error()) + } + + c.Append("Location", c.BaseURL()+"/api/v1/links/"+editlink.ID) + c.Status(fiber.StatusNoContent) + return nil } diff --git a/views/edit_link.tmpl b/views/edit_link.tmpl index dd56808..0bc3dd4 100644 --- a/views/edit_link.tmpl +++ b/views/edit_link.tmpl @@ -35,7 +35,7 @@
-
+

Edit shortlink

diff --git a/web/custom.css b/web/custom.css index 8f0f130..06b24af 100644 --- a/web/custom.css +++ b/web/custom.css @@ -71,4 +71,8 @@ summary[role="link"].secondary:is([aria-current], :hover, :active, :focus) { /* Footer */ body>footer { padding: 1rem 0; +} + +input[readonly] { + color: gray; } \ No newline at end of file diff --git a/web/main.js b/web/main.js index a4f19b7..9c1b656 100644 --- a/web/main.js +++ b/web/main.js @@ -21,21 +21,52 @@ async function HandleLinkIndexCopy(id) { // Link Add async function HandleLinkAddSubmit() { + await LinkAction("add") +} + +// Link edit + +async function HandleLinkEditSubmit() { + await LinkAction("edit") +} + +async function LinkAction(action){ document.getElementById("submit").active = false let slug = document.getElementById("linkname").value let url = document.getElementById("link").value let description = document.getElementById("description").value - let body = { - "id" : slug, - "url" : url, - "description" : description + + let method, endpoint = "" + let body; + switch(action) { + case "add": + method = "POST" + endpoint = "/api/v1/links/" + body = { + "id" : slug, + "url" : url, + "description" : description + } + break; + case "edit": + method = "PUT" + endpoint = "/api/v1/links/" + slug + body = { + "url" : url, + "description" : description + } + break; + default: + return } - let response = await fetch("/api/v1/links", { + + + let response = await fetch(endpoint, { credentials: "include", body: JSON.stringify(body), mode: "same-origin", - method: "POST" + method: method }); if (!response.ok) {