add schema for MediaFile
This commit is contained in:
@@ -1,14 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import "github.com/gofiber/fiber/v2"
|
|
||||||
|
|
||||||
func SetupComics(app *fiber.App) {
|
|
||||||
comics := app.Group("/api/comics")
|
|
||||||
comics.Get("/comics", GetAllComics)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAllComics(c *fiber.Ctx) error {
|
|
||||||
return c.Status(200).JSON(&fiber.Map{
|
|
||||||
"comics": []string{},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -1,19 +1,29 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"kontor-api-go/cmd/kontor/handler"
|
"kontor-api-go/pkg/handler"
|
||||||
|
"kontor-api-go/pkg/schema"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Println("Kontor started")
|
log.Println("Kontor started")
|
||||||
|
|
||||||
|
if err := schema.Connect(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
|
||||||
|
// app.Use(logger.New())
|
||||||
app.Get("/health", handler.GetHealth)
|
app.Get("/health", handler.GetHealth)
|
||||||
|
|
||||||
handler.SetupComics(app)
|
api := app.Group("/api", logger.New())
|
||||||
|
handler.SetupComicRoutes(api)
|
||||||
|
handler.SetupMediaRoutes(api)
|
||||||
// Listen on port 8900
|
// Listen on port 8900
|
||||||
app.Listen(":8900")
|
app.Listen(":8900")
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"kontor-api-go/pkg/schema"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetupComicRoutes(api fiber.Router) {
|
||||||
|
comics := api.Group("/comics")
|
||||||
|
comics.Get("/comics", GetAllComics)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllComics(c *fiber.Ctx) error {
|
||||||
|
var comics []schema.Comic
|
||||||
|
var err error
|
||||||
|
var db *bun.DB
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
db, err = schema.GetDatabase()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.NewSelect().Model(&comics).Relation("Publisher").Scan(ctx)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(comics)
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"kontor-api-go/pkg/schema"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetupMediaRoutes(api fiber.Router) {
|
||||||
|
media := api.Group("/media")
|
||||||
|
media.Get("/files", GetAllFiles)
|
||||||
|
media.Post("/files", AddFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAllFiles(c *fiber.Ctx) 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 fiber.NewError(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(files)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddFile(c *fiber.Ctx) error {
|
||||||
|
var err error
|
||||||
|
var db *bun.DB
|
||||||
|
ctx := context.Background()
|
||||||
|
var payload map[string]interface{}
|
||||||
|
if err = json.Unmarshal(c.Body(), &payload); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
link := payload["url"]
|
||||||
|
log.Printf("URL %s has been sent", link)
|
||||||
|
|
||||||
|
db, err = schema.GetDatabase()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
mediafile := &schema.MediaFile{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.SendStatus(201)
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package schema
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Publisher struct {
|
||||||
|
bun.BaseModel `bun:"table:publisher"`
|
||||||
|
|
||||||
|
ID string `bun:"id,pk"`
|
||||||
|
CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
Version int `bun:"version,default:0"`
|
||||||
|
Name string `bun:"name,unique:name"`
|
||||||
|
WebLink string `bun:"weblink"`
|
||||||
|
|
||||||
|
ParentPublisherID *string `bun:"parent_publisher_id"`
|
||||||
|
ParentPublisher *Publisher `bun:"rel:belongs-to,join:parent_publisher_id=id"`
|
||||||
|
Imprints []Publisher `bun:"rel:has-many,join:id=parent_publisher_id"`
|
||||||
|
Comics []Comic `bun:"rel:has-many,join:id=publisher_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Comic struct {
|
||||||
|
bun.BaseModel `bun:"table:comic"`
|
||||||
|
|
||||||
|
ID string `bun:"id,pk"`
|
||||||
|
CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
Version int `bun:"version,default:0"`
|
||||||
|
Title string `bun:"title,unique:title,notnull"`
|
||||||
|
CurrentOrder bool `bun:"current_order"`
|
||||||
|
Completed bool `bun:"completed"`
|
||||||
|
WebLink string `bun:"weblink"`
|
||||||
|
|
||||||
|
PublisherID *string `bun:"publisher_id"`
|
||||||
|
Publisher *Publisher `bun:"rel:belongs-to,join:publisher_id=id"`
|
||||||
|
|
||||||
|
// Issues []Issue `bun:"rel:has-many,join:id=comic_id"`
|
||||||
|
// StoryArcs []StoryArc `bun:"rel:has-many,join:id=comic_id"`
|
||||||
|
// TradePaperbacks []TradePaperback `bun:"rel:has-many,join:id=comic_id"`
|
||||||
|
// Volumes []Volume `bun:"rel:has-many,join:id=comic_id"`
|
||||||
|
// ComicWorks []ComicWork `bun:"rel:has-many,join:id=comic_id"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package schema
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
"github.com/uptrace/bun/dialect/pgdialect"
|
||||||
|
"github.com/uptrace/bun/driver/pgdriver"
|
||||||
|
)
|
||||||
|
|
||||||
|
var DB *bun.DB
|
||||||
|
|
||||||
|
func Connect() error {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
dsn := "postgres://kontor:kontor@postgres:5432/kontor?sslmode=disable"
|
||||||
|
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
|
||||||
|
|
||||||
|
sqldb.SetMaxOpenConns(4)
|
||||||
|
sqldb.SetMaxIdleConns(4)
|
||||||
|
|
||||||
|
DB := bun.NewDB(sqldb, pgdialect.New())
|
||||||
|
|
||||||
|
if err = DB.Ping(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Connection Opened to Database")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetDatabase() (*bun.DB, error) {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
dsn := "postgres://kontor:kontor@postgres:5432/kontor?sslmode=disable"
|
||||||
|
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
|
||||||
|
|
||||||
|
sqldb.SetMaxOpenConns(4)
|
||||||
|
sqldb.SetMaxIdleConns(4)
|
||||||
|
|
||||||
|
DB := bun.NewDB(sqldb, pgdialect.New())
|
||||||
|
|
||||||
|
if err = DB.Ping(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Returned Database Connection")
|
||||||
|
return DB, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
package schema
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/uptrace/bun"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MediaFile struct {
|
||||||
|
bun.BaseModel `bun:"table:media_file"`
|
||||||
|
|
||||||
|
ID string `bun:"id,pk"`
|
||||||
|
CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
Version int `bun:"version,default:0"`
|
||||||
|
CloudLink string `bun:"cloud_link"`
|
||||||
|
FileName string `bun:"file_name"`
|
||||||
|
Path string `bun:"path"`
|
||||||
|
Review bool `bun:"review"`
|
||||||
|
Title string `bun:"title"`
|
||||||
|
WebLink string `bun:"url,unique:url"`
|
||||||
|
ShouldDownload bool `bun:"should_download"`
|
||||||
|
|
||||||
|
MediaActorFiles []MediaActorFile `bun:"rel:has-many,join:id=media_file_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MediaActor struct {
|
||||||
|
bun.BaseModel `bun:"table:media_actor"`
|
||||||
|
|
||||||
|
ID string `bun:"id,pk"`
|
||||||
|
CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
Version int `bun:"version,default:0"`
|
||||||
|
Name string `bun:"name,unique:name"`
|
||||||
|
WebLink string `bun:"url,unique:url"`
|
||||||
|
|
||||||
|
MediaActorFiles []MediaActorFile `bun:"rel:has-many,join:id=media_actor_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MediaActorFile struct {
|
||||||
|
bun.BaseModel `bun:"table:media_actor_file"`
|
||||||
|
|
||||||
|
ID string `bun:"id,pk"`
|
||||||
|
CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
Version int `bun:"version,default:0"`
|
||||||
|
MediaActorID *string `bun:"media_actor_id"`
|
||||||
|
MediaActor *MediaActor `bun:"rel:belongs-to,join:media_actor_id=id"`
|
||||||
|
MediaFileID *string `bun:"media_file_id"`
|
||||||
|
MediaFile *MediaFile `bun:"rel:belongs-to,join:media_file_id=id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MediaArticle struct {
|
||||||
|
bun.BaseModel `bun:"table:media_article"`
|
||||||
|
|
||||||
|
ID string `bun:"id,pk"`
|
||||||
|
CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
Version int `bun:"version,default:0"`
|
||||||
|
Review bool `bun:"review"`
|
||||||
|
Title string `bun:"title"`
|
||||||
|
WebLink string `bun:"url,unique:url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type MediaVideo struct {
|
||||||
|
bun.BaseModel `bun:"table:media_article"`
|
||||||
|
|
||||||
|
ID string `bun:"id,pk"`
|
||||||
|
CreatedAt time.Time `bun:"created_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
UpdatedAt time.Time `bun:"last_modified_date,nullzero,notnull,default:current_timestamp"`
|
||||||
|
Version int `bun:"version,default:0"`
|
||||||
|
CloudLink string `bun:"cloud_link"`
|
||||||
|
FileName string `bun:"file_name"`
|
||||||
|
Path string `bun:"path"`
|
||||||
|
Review bool `bun:"review"`
|
||||||
|
Title string `bun:"title"`
|
||||||
|
WebLink string `bun:"url,unique:url"`
|
||||||
|
ShouldDownload bool `bun:"should_download"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user