add project kontor-api-echo
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"kontor-api-echo/pkg/schema"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
func SetupMediaRoutes(api *echo.Group) {
|
||||
media := api.Group("/media")
|
||||
media.GET("/files", GetAllFiles)
|
||||
media.POST("/files", AddFile)
|
||||
}
|
||||
|
||||
func GetAllFiles(c echo.Context) error {
|
||||
var files []schema.MediaFile
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = schema.GetDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = db.NewSelect().Model(&files).Relation("MediaActorFiles").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return echo.ErrInternalServerError
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, files)
|
||||
}
|
||||
|
||||
type Link struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
func AddFile(c echo.Context) error {
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
link := new(Link)
|
||||
if err = c.Bind(link); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("URL %s has been sent", link)
|
||||
|
||||
db, err = schema.GetDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
id := uuid.NewString()
|
||||
timestamp := time.Now()
|
||||
mediafile := &schema.MediaFile{ID: id, CreatedAt: timestamp, UpdatedAt: timestamp, WebLink: fmt.Sprintf("%s", link), Version: 1, ShouldDownload: true, Review: true}
|
||||
_, err = db.NewInsert().Model(mediafile).Exec(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.String(http.StatusCreated, "Link created")
|
||||
}
|
||||
Reference in New Issue
Block a user