models

package
v0.0.0-...-44a7979 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Models = []interface{}{
	Author{},
	Book{},
}

Collection of all database model structs.

View Source
var UUIDNotFound = uuid.MustParse("00000000-0000-0000-0000-000000000000")

UUID returned when an entity is not found.

Functions

func CreateBook

func CreateBook(book *Book) error

func DeleteBook

func DeleteBook(id string) error

func FindOrCreateAuthor

func FindOrCreateAuthor(author *Author, db *gorm.DB) error

Find Author by Name, creating if not exists. Accepts a DB conn or trx.

func IDFromString

func IDFromString(id string) uuid.UUID

Return a UUID from a string.

Types

type Author

type Author struct {
	Base
	Name string `gorm:"not null;unique" json:"name"`

	Books []*Book `gorm:"many2many:book_authors;" json:"-"`
}

func AllAuthors

func AllAuthors() ([]Author, error)

func FindAuthor

func FindAuthor(id string) (Author, error)

type Base

type Base struct {
	ID        uuid.UUID      `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id"`
	CreatedAt time.Time      `gorm:"autoCreateTime:milli" json:"created_at,omitempty"`
	UpdatedAt time.Time      `gorm:"autoUpdateTime:milli" json:"updated_at,omitempty"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}

Define core model fields.

type Book

type Book struct {
	Base
	Edition    string `json:"edition,omitempty"`
	ISBN       string `json:"isbn,omitempty"`
	ReadStatus bool   `json:"read_status,omitempty"`
	Title      string `gorm:"not null" json:"title"`

	Authors []*Author `gorm:"many2many:book_authors" json:"authors,omitempty"`
}

func AllBooks

func AllBooks() ([]Book, error)

func FindBook

func FindBook(id string) (Book, error)

func UpdateBook

func UpdateBook(id string, book *Book) (Book, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL