add short link edit option

This commit is contained in:
Adora Laura Kalb 2023-06-16 12:46:08 +02:00
parent d422af7fe1
commit 4c9a8d8013
Signed by: adoralaura
GPG key ID: 7A4552166FC8C056
6 changed files with 94 additions and 13 deletions

View file

@ -1,12 +1,12 @@
pipeline: steps:
docker-deploy-push: docker-deploy-push:
when: when:
event: - event: push
- push branch: main
image: woodpeckerci/plugin-docker-buildx image: woodpeckerci/plugin-docker-buildx
settings: settings:
dockerfile: Dockerfile 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 repo: codeberg.org/lauralani/go-urlsh
registry: codeberg.org registry: codeberg.org
tags: latest tags: latest
@ -21,7 +21,7 @@ pipeline:
image: woodpeckerci/plugin-docker-buildx image: woodpeckerci/plugin-docker-buildx
settings: settings:
dockerfile: Dockerfile 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 repo: codeberg.org/lauralani/go-urlsh
registry: codeberg.org registry: codeberg.org
auto_tag: true auto_tag: true

View file

@ -9,10 +9,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- added Changelog - added Changelog
- Ability to change the url and description of shortlinks
### Changed ### Changed
- License changed to MIT - License changed to MIT
### Removed
- Docker image support for linux/ppc64le
## [0.1.2] - 2014-08-09 ## [0.1.2] - 2014-08-09
### Fixed ### Fixed

View file

@ -5,6 +5,7 @@ import (
"codeberg.org/lauralani/go-urlsh/internal/misc" "codeberg.org/lauralani/go-urlsh/internal/misc"
"codeberg.org/lauralani/go-urlsh/models" "codeberg.org/lauralani/go-urlsh/models"
"context" "context"
"database/sql"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -35,7 +36,7 @@ func HandleLinkGetAll(c *fiber.Ctx) error {
} }
func HandleLinkGet(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 { func HandleLinkPost(c *fiber.Ctx) error {
@ -77,6 +78,47 @@ func HandleLinkPost(c *fiber.Ctx) error {
} }
func HandleLinkPut(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 return nil
} }

View file

@ -35,7 +35,7 @@
</ul> </ul>
</nav> </nav>
<main class="container" style="padding: 0"> <main class="container" style="padding: 0">
<form action="javascript:HandleSubmit();" style="margin: 0"> <form action="javascript:HandleLinkEditSubmit();" style="margin: 0">
<fieldset id="form_fields"> <fieldset id="form_fields">
<hgroup> <hgroup>
<h1>Edit shortlink</h1> <h1>Edit shortlink</h1>

View file

@ -72,3 +72,7 @@ summary[role="link"].secondary:is([aria-current], :hover, :active, :focus) {
body>footer { body>footer {
padding: 1rem 0; padding: 1rem 0;
} }
input[readonly] {
color: gray;
}

View file

@ -21,21 +21,52 @@ async function HandleLinkIndexCopy(id) {
// Link Add // Link Add
async function HandleLinkAddSubmit() { async function HandleLinkAddSubmit() {
await LinkAction("add")
}
// Link edit
async function HandleLinkEditSubmit() {
await LinkAction("edit")
}
async function LinkAction(action){
document.getElementById("submit").active = false document.getElementById("submit").active = false
let slug = document.getElementById("linkname").value let slug = document.getElementById("linkname").value
let url = document.getElementById("link").value let url = document.getElementById("link").value
let description = document.getElementById("description").value let description = document.getElementById("description").value
let body = {
"id" : slug, let method, endpoint = ""
"url" : url, let body;
"description" : description 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", credentials: "include",
body: JSON.stringify(body), body: JSON.stringify(body),
mode: "same-origin", mode: "same-origin",
method: "POST" method: method
}); });
if (!response.ok) { if (!response.ok) {