implement Comics schema and endpoints
This commit is contained in:
@@ -12,6 +12,8 @@ import (
|
||||
func SetupComicRoutes(api fiber.Router) {
|
||||
comics := api.Group("/comics")
|
||||
comics.Get("/comics", GetAllComics)
|
||||
comics.Get("/publishers", GetAllPublishers)
|
||||
comics.Get("/comicworks", GetAllComicWorks)
|
||||
}
|
||||
|
||||
func GetAllComics(c *fiber.Ctx) error {
|
||||
@@ -33,3 +35,43 @@ func GetAllComics(c *fiber.Ctx) error {
|
||||
|
||||
return c.JSON(comics)
|
||||
}
|
||||
|
||||
func GetAllPublishers(c *fiber.Ctx) error {
|
||||
var publishers []schema.Publisher
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = schema.GetDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.NewSelect().Model(&publishers).Relation("ParentPublisher").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return c.JSON(publishers)
|
||||
}
|
||||
|
||||
func GetAllComicWorks(c *fiber.Ctx) error {
|
||||
var comic_works []schema.ComicWork
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = schema.GetDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.NewSelect().Model(&comic_works).Relation("Comic").Relation("Artist").Relation("WorkType").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return fiber.NewError(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return c.JSON(comic_works)
|
||||
}
|
||||
|
||||
@@ -34,12 +34,107 @@ type Comic struct {
|
||||
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"`
|
||||
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"`
|
||||
}
|
||||
|
||||
type Artist struct {
|
||||
bun.BaseModel `bun:"table:artist"`
|
||||
|
||||
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:title,notnull"`
|
||||
WebLink string `bun:"weblink"`
|
||||
}
|
||||
|
||||
type Issue struct {
|
||||
bun.BaseModel `bun:"table:issue"`
|
||||
|
||||
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"`
|
||||
InStock bool `bun:"in_stock"`
|
||||
IsRead bool `bun:"is_read"`
|
||||
IssueNumber string `bu:"issue_number"`
|
||||
Title string `bun:"title"`
|
||||
PublishedOn time.Time `bun:"published_on"`
|
||||
ComicID *string `bun:"comic_id"`
|
||||
Comic *Comic `bun:"rel:belongs-to,join:comic_id=id"`
|
||||
StoryArcID *string `bun:"story_arc_id"`
|
||||
StoryArc *StoryArc `bun:"rel:belongs-to,join:story_arc_id=id"`
|
||||
VolumeID *string `bun:"volume_id"`
|
||||
Volume *Volume `bun:"rel:belongs-to,join:volume_id=id"`
|
||||
}
|
||||
|
||||
type StoryArc struct {
|
||||
bun.BaseModel `bun:"table:story_arc"`
|
||||
|
||||
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,notnull"`
|
||||
ComicID *string `bun:"comic_id"`
|
||||
Comic *Comic `bun:"rel:belongs-to,join:comic_id=id"`
|
||||
VolumeID *string `bun:"volume_id"`
|
||||
Volume *Volume `bun:"rel:belongs-to,join:volume_id=id"`
|
||||
}
|
||||
|
||||
type TradePaperback struct {
|
||||
bun.BaseModel `bun:"table:trade_paperback"`
|
||||
|
||||
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,notnull"`
|
||||
IssueStart int `bun:"issue_start"`
|
||||
IssueEnd int `bun:"issue_end"`
|
||||
ComicID *string `bun:"comic_id"`
|
||||
Comic *Comic `bun:"rel:belongs-to,join:comic_id=id"`
|
||||
}
|
||||
|
||||
type Volume struct {
|
||||
bun.BaseModel `bun:"table:volume"`
|
||||
|
||||
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,notnull"`
|
||||
ComicID *string `bun:"comic_id"`
|
||||
Comic *Comic `bun:"rel:belongs-to,join:comic_id=id"`
|
||||
}
|
||||
|
||||
type WorkType struct {
|
||||
bun.BaseModel `bun:"table:worktype"`
|
||||
|
||||
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,notnull"`
|
||||
}
|
||||
|
||||
type ComicWork struct {
|
||||
bun.BaseModel `bun:"table:comic_work"`
|
||||
|
||||
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"`
|
||||
ArtistID *string `bun:"artist_id"`
|
||||
Artist *Artist `bun:"rel:belongs-to,join:artist_id=id"`
|
||||
ComicID *string `bun:"comic_id"`
|
||||
Comic *Comic `bun:"rel:belongs-to,join:comic_id=id"`
|
||||
WorkTypeID *string `bun:"work_type_id"`
|
||||
WorkType *WorkType `bun:"rel:belongs-to,join:work_type_id=id"`
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"database/sql"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/extra/bundebug"
|
||||
|
||||
"github.com/uptrace/bun/dialect/pgdialect"
|
||||
"github.com/uptrace/bun/driver/pgdriver"
|
||||
)
|
||||
|
||||
func GetTestDatabase() (*bun.DB, error) {
|
||||
var err error
|
||||
|
||||
dsn := "postgres://kontor:kontor@localhost:5432/kontor?sslmode=disable"
|
||||
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
|
||||
|
||||
sqldb.SetMaxOpenConns(4)
|
||||
sqldb.SetMaxIdleConns(4)
|
||||
|
||||
DB := bun.NewDB(sqldb, pgdialect.New())
|
||||
DB.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
|
||||
|
||||
if err = DB.Ping(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Println("Returned Database Connection")
|
||||
return DB, nil
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
log.Println("Setup Test")
|
||||
exitCode := m.Run()
|
||||
os.Exit(exitCode)
|
||||
}
|
||||
|
||||
func TestSelectComics(t *testing.T) {
|
||||
var comics []Comic
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = GetTestDatabase()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = db.NewSelect().Model(&comics).Relation("Publisher").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 168, len(comics))
|
||||
}
|
||||
|
||||
func TestSelectPublishers(t *testing.T) {
|
||||
var publishers []Publisher
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = GetTestDatabase()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = db.NewSelect().Model(&publishers).Relation("ParentPublisher").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 19, len(publishers))
|
||||
}
|
||||
|
||||
func TestSelectWorkTypes(t *testing.T) {
|
||||
var comic_works []ComicWork
|
||||
var err error
|
||||
var db *bun.DB
|
||||
ctx := context.Background()
|
||||
|
||||
db, err = GetTestDatabase()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.NewSelect().Model(&comic_works).Relation("Comic").Relation("Artist").Relation("WorkType").Scan(ctx)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 59, len(comic_works))
|
||||
}
|
||||
Reference in New Issue
Block a user