2023-04-25 18:43:01 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-16 12:46:08 +02:00
|
|
|
"database/sql"
|
2023-04-25 18:43:01 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
2024-05-04 17:06:01 +02:00
|
|
|
|
|
|
|
"code.lila.network/adoralaura/go-urlsh/internal/constants"
|
|
|
|
"code.lila.network/adoralaura/go-urlsh/internal/db"
|
|
|
|
"code.lila.network/adoralaura/go-urlsh/internal/misc"
|
|
|
|
"code.lila.network/adoralaura/go-urlsh/models"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-04-25 18:43:01 +02:00
|
|
|
)
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
func (am APIRouteManager) HandleLinkGetAll(c *fiber.Ctx) error {
|
2024-05-04 17:06:01 +02:00
|
|
|
if !db.IsCookieValid(c.Cookies(constants.LoginCookieName, "")) && !db.IsApiKeyValid(c.GetRespHeader("x-api-key", "")) {
|
2023-05-05 15:16:54 +02:00
|
|
|
return fiber.NewError(fiber.StatusUnauthorized, "401 Unauthorized")
|
|
|
|
}
|
|
|
|
|
2023-04-25 18:43:01 +02:00
|
|
|
var links []models.Link
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
err := am.db.NewSelect().Model(&links).Scan(context.Background())
|
2023-04-25 18:43:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error querying links: %v", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, link := range links {
|
|
|
|
log.Printf("id: %v, url: %v", link.ID, link.URL)
|
|
|
|
}
|
2023-05-05 15:16:54 +02:00
|
|
|
err = c.JSON(links)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2023-04-25 18:43:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
func (am APIRouteManager) HandleLinkGet(c *fiber.Ctx) error {
|
2023-06-16 12:46:08 +02:00
|
|
|
return fiber.NewError(fiber.StatusNotImplemented, "501 Not Implemented")
|
2023-04-25 18:43:01 +02:00
|
|
|
}
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
func (am APIRouteManager) HandleLinkPost(c *fiber.Ctx) error {
|
2024-05-04 17:06:01 +02:00
|
|
|
if !db.IsCookieValid(c.Cookies(constants.LoginCookieName, "")) && !db.IsApiKeyValid(c.GetRespHeader("x-api-key", "")) {
|
2023-04-27 19:03:28 +02:00
|
|
|
return fiber.NewError(fiber.StatusUnauthorized, "401 Unauthorized")
|
|
|
|
}
|
|
|
|
|
2023-04-25 18:43:01 +02:00
|
|
|
var newlink models.Link
|
|
|
|
|
|
|
|
err := json.Unmarshal(c.Body(), &newlink)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "400 Bad Request: "+err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check if ID is already taken when ID is given
|
|
|
|
|
|
|
|
if newlink.ID == "" {
|
|
|
|
newlink.ID = misc.RandomString(6)
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
newlink.Created = now
|
|
|
|
newlink.Modified = now
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
_, err = am.db.NewInsert().Model(&newlink).Exec(context.Background())
|
2023-04-25 18:43:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, "500 Internal Server Error: "+err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Append("Location", c.BaseURL()+"/api/v1/links/"+newlink.ID)
|
2023-04-27 19:03:28 +02:00
|
|
|
c.Status(fiber.StatusCreated)
|
2023-05-05 15:16:54 +02:00
|
|
|
|
|
|
|
err = c.JSON(newlink)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2023-04-25 18:43:01 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
func (am APIRouteManager) HandleLinkPut(c *fiber.Ctx) error {
|
2024-05-04 17:06:01 +02:00
|
|
|
if !db.IsCookieValid(c.Cookies(constants.LoginCookieName, "")) && !db.IsApiKeyValid(c.GetRespHeader("x-api-key", "")) {
|
2023-06-16 12:46:08 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
err := am.db.NewSelect().Model(&dblink).Where("id = ?", id).Scan(context.Background())
|
2023-06-16 12:46:08 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
_, err = am.db.NewUpdate().Model(&dblink).WherePK().Exec(context.Background())
|
2023-06-16 12:46:08 +02:00
|
|
|
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)
|
|
|
|
|
2023-04-25 18:43:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
func (am APIRouteManager) HandleLinkDelete(c *fiber.Ctx) error {
|
2023-04-25 18:43:01 +02:00
|
|
|
id := c.Params("id")
|
|
|
|
|
|
|
|
if id == "" {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "400 Bad Request")
|
|
|
|
}
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
numrows, err := am.db.NewSelect().Model((*models.Link)(nil)).Where("id = ?", id).Count(context.Background())
|
2023-04-25 18:43:01 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, "500 Internal Server Error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if numrows < 1 {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, "404 Not Found")
|
|
|
|
}
|
|
|
|
|
2024-09-30 13:19:01 +02:00
|
|
|
_, err = am.db.NewDelete().Model((*models.Link)(nil)).Where("id = ?", id).Exec(context.Background())
|
2023-04-25 18:43:01 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err.Error())
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, "500 Internal Server Error")
|
|
|
|
}
|
2023-05-05 15:16:54 +02:00
|
|
|
|
|
|
|
c.Status(fiber.StatusNoContent)
|
|
|
|
err = c.SendString("204 No Content")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2023-04-25 18:43:01 +02:00
|
|
|
return nil
|
|
|
|
}
|