add lofi by api
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s

This commit is contained in:
Thomas Peetz
2026-10-07 22:39:53 +02:00
parent 094dffb1c7
commit 7116869b42
6 changed files with 102 additions and 25 deletions
+12 -18
View File
@@ -1,14 +1,14 @@
package cmd
import (
"encoding/json"
"fmt"
"kontor-api-gin/internal/helpers"
"kontor-api-gin/internal/models"
"log/slog"
"os"
"github.com/go-stomp/stomp/v3"
"github.com/spf13/cobra"
"resty.dev/v3"
)
var addLinksCmd = &cobra.Command{
@@ -17,14 +17,6 @@ var addLinksCmd = &cobra.Command{
Long: "Add Links",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
conn, err := stomp.Dial("tcp", "localhost:61616",
stomp.ConnOpt.Login("artemis", "artemis"), // Provide username and password
)
if err != nil {
slog.Info("failed to connect to Artemis: %s", err)
os.Exit(1)
}
defer conn.Disconnect()
fileName := args[0]
slog.Info(fmt.Sprintf("add Links from %s", fileName))
@@ -33,18 +25,20 @@ var addLinksCmd = &cobra.Command{
slog.Warn("add links failed", "error", err)
os.Exit(1)
}
var login LoginResponse
for _, line := range lines {
b, jsonErr := json.Marshal(line)
if jsonErr != nil {
slog.Info("Json encoding failed", "error", jsonErr.Error())
}
err = conn.Send("media.link", "application/json", b)
lofiAdd := models.MediaLofiAdd{Url: line}
client := resty.New()
PostJSON(client, fmt.Sprintf("%s/%s/%s", server, "api", "login"), &login)
response, err := client.R().
SetBody(lofiAdd).
SetHeader("Accept", "application/json").
Post("http://localhost:8550/api/v1/media/lofi")
if err != nil {
slog.Info("send failed", "error", err.Error())
slog.Info("Add Lofi", "error", err.Error())
}
slog.Info("Add Lofi", "status", response.StatusCode(), "body", response.Body)
}
},
}
@@ -33,6 +33,41 @@ func (handler *MediaLoFiHandler) List(c *gin.Context) {
c.JSON(http.StatusOK, dtos)
}
func (handler *MediaLoFiHandler) Get(c *gin.Context) {
id := c.Param("id")
lofi, err := handler.Service.Get(c.Request.Context(), id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Lofi not found"})
return
}
c.JSON(http.StatusOK, lofi)
}
func (handler *MediaLoFiHandler) Add(c *gin.Context) {
var input models.MediaLofiAdd
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
lofi, err := handler.Service.Add(c.Request.Context(), input.Url)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "lofi not added"})
return
}
c.JSON(http.StatusOK, lofi)
}
func (handler *MediaLoFiHandler) Delete(c *gin.Context) {
id := c.Param("id")
err := handler.Service.Delete(c.Request.Context(), id)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "lofi not found"})
return
}
c.JSON(http.StatusOK, gin.H{"ok": "true"})
}
func (handler *MediaLoFiHandler) ListPage(c *gin.Context) {
session := sessions.Default(c)
user := session.Get(globals.UserKey)
+4
View File
@@ -22,6 +22,10 @@ func (media_lofi *MediaLoFi) TableName() string {
return "media_lofi"
}
type MediaLofiAdd struct {
Url string `json:"url"`
}
type MediaLoFiDTO struct {
ID string `json:"id" binding:"required"`
CreatedAt helpers.KontorTime `json:"created_date"`
@@ -25,3 +25,28 @@ func (repository *MediaLoFiRepository) FindAll(context context.Context) ([]model
return lofis, nil
}
func (repository *MediaLoFiRepository) GetById(context context.Context, id string) (models.MediaLoFi, error) {
var lofi = models.MediaLoFi{ID: id}
slog.Info("MediaLoFiRepository#GetById", "id", id)
result := repository.db.First(&lofi)
if result.Error != nil {
slog.Info("MediaLoFiRepository#GetById", "error", result.Error.Error())
}
return lofi, result.Error
}
func (repository *MediaLoFiRepository) Add(context context.Context, url string) (models.MediaLoFi, error) {
slog.Info("MediaLoFiRepository#Add", "url", url)
panic("unimplemented")
}
func (repository *MediaLoFiRepository) Delete(context context.Context, id string) error {
var actor = models.MediaLoFi{ID: id}
slog.Info("MediaLoFiRepository#Delete", "id", id)
result := repository.db.Delete(&actor)
if result.Error != nil {
slog.Info("MediaLoFiRepository#Delete", "error", result.Error.Error())
}
return result.Error
}
+11 -7
View File
@@ -101,13 +101,17 @@ func New(admin *handlers.AdminHandler, bookshelf *handlers.BookshelfHandler, com
secured.GET("comics/storyarcs", comics.StoryArcHandler.List)
secured.GET("comics/comicworks", comics.ComicworkHandler.List)
secured.GET("comics/issueworks", comics.IssueworkHandler.List)
secured.GET("media/files", media.MediaFileHandler.List)
secured.GET("media/videos", media.MediaVideoHandler.List)
secured.GET("media/lofis", media.MediaLoFiHandler.List)
secured.GET("media/actors", media.MediaActorHandler.List)
secured.GET("media/actors/:id", media.MediaActorHandler.Get)
secured.DELETE("media/actors/:id", media.MediaActorHandler.Delete)
secured.GET("media/actorfiles", media.MediaActorFileHandler.List)
mediaGroup := secured.Group("/media")
mediaGroup.GET("/files", media.MediaFileHandler.List)
mediaGroup.GET("/videos", media.MediaVideoHandler.List)
mediaGroup.GET("/lofis", media.MediaLoFiHandler.List)
mediaGroup.POST("/lofis", media.MediaLoFiHandler.Add)
mediaGroup.GET("/lofis/:id", media.MediaLoFiHandler.Get)
mediaGroup.DELETE("/lofis", media.MediaLoFiHandler.Delete)
mediaGroup.GET("/actors", media.MediaActorHandler.List)
mediaGroup.GET("/actors/:id", media.MediaActorHandler.Get)
mediaGroup.DELETE("/actors/:id", media.MediaActorHandler.Delete)
mediaGroup.GET("/actorfiles", media.MediaActorFileHandler.List)
}
return router
}
+15
View File
@@ -20,3 +20,18 @@ func (service *MediaLoFiService) List(context context.Context) ([]models.MediaLo
slog.Info("MediaLoFiService#List")
return service.repository.FindAll(context)
}
func (service *MediaLoFiService) Get(context context.Context, id string) (models.MediaLoFi, error) {
slog.Info("MediaLoFiService#Get")
return service.repository.GetById(context, id)
}
func (service *MediaLoFiService) Add(context context.Context, url string) (models.MediaLoFi, error) {
slog.Info("MediaLoFiService#Add")
return service.repository.Add(context, url)
}
func (service *MediaLoFiService) Delete(context context.Context, id string) error {
slog.Info("MediaLoFiService#Delete")
return service.repository.Delete(context, id)
}