GoGronkh/database/database.go

273 lines
6.8 KiB
Go

package database
import (
"database/sql"
"log"
"time"
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
)
// Author database model
type Author struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
Slug sql.NullString `sql:"not null;unique_index"`
Name sql.NullString `sql:"not null"`
Youtube sql.NullString `sql:"not null"`
AvatarS sql.NullString
AvatarB sql.NullString
FanArtS sql.NullString
FanArtB sql.NullString
LetsPlays []LetsPlay
LetsTests []LetsTest
Episodes []Episode
}
// Let's Play database model
type LetsPlay struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
AuthorID uint `sql:"index"`
Slug sql.NullString `sql:"not null;unique_index"`
Name sql.NullString `sql:"not null"`
PosterS sql.NullString
PosterB sql.NullString
Aired time.Time `sql:"default:null"`
Episodes []Episode
}
// Let's Test database model
type LetsTest struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
AuthorID uint `sql:"index"`
Slug sql.NullString `sql:"not null;unique_index"`
Name sql.NullString `sql:"not null"`
PosterS sql.NullString
PosterB sql.NullString
ThumbS sql.NullString
ThumbB sql.NullString
Youtube sql.NullString `sql:"not null;unique"`
Descr sql.NullString `sql:"type:TEXT"`
Aired time.Time `sql:"not null"`
Rating sql.NullFloat64
Votes sql.NullInt64
Duration sql.NullInt64
}
// Episode database model
type Episode struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
AuthorID uint `sql:"index"`
LetsPlayID uint `sql:"index"`
Slug sql.NullString `sql:"not null;unique_index"`
Name sql.NullString `sql:"not null"`
Episode sql.NullInt64 `sql:"not null"`
ThumbS sql.NullString
ThumbB sql.NullString
Youtube sql.NullString `sql:"not null;unique"`
Descr sql.NullString `sql:"type:TEXT"`
Aired time.Time `sql:"not null"`
Rating sql.NullFloat64
Votes sql.NullInt64
Duration sql.NullInt64
}
var Db gorm.DB
func InitDb(connection string) (error) {
var err error
Db, err = gorm.Open("postgres", connection)
if err != nil {
log.Fatalf("FAT Database error: %+v", err)
return err
}
Db.LogMode(false)
Db.AutoMigrate(&Author{}, &LetsPlay{}, &LetsTest{}, &Episode{})
return err
}
// Empty string cleanup
func (a *Author) BeforeSave() (err error) {
if a.Slug.String == "" {
a.Slug.Valid = false
} else {
a.Slug.Valid = true
}
if a.Name.String == "" {
a.Name.Valid = false
} else {
a.Name.Valid = true
}
if a.Youtube.String == "" {
a.Youtube.Valid = false
} else {
a.Youtube.Valid = true
}
if a.AvatarS.String == "" {
a.AvatarS.Valid = false
} else {
a.AvatarS.Valid = true
}
if a.AvatarB.String == "" {
a.AvatarB.Valid = false
} else {
a.AvatarB.Valid = true
}
if a.FanArtS.String == "" {
a.FanArtS.Valid = false
} else {
a.FanArtS.Valid = true
}
if a.FanArtB.String == "" {
a.FanArtB.Valid = false
} else {
a.FanArtB.Valid = true
}
return
}
func (l *LetsPlay) BeforeSave() (err error) {
if l.Slug.String == "" {
l.Slug.Valid = false
} else {
l.Slug.Valid = true
}
if l.Name.String == "" {
l.Name.Valid = false
} else {
l.Name.Valid = true
}
if l.PosterS.String == "" {
l.PosterS.Valid = false
} else {
l.PosterS.Valid = true
}
if l.PosterB.String == "" {
l.PosterB.Valid = false
} else {
l.PosterB.Valid = true
}
return
}
func (l *LetsTest) BeforeSave() (err error) {
if l.Slug.String == "" {
l.Slug.Valid = false
} else {
l.Slug.Valid = true
}
if l.Name.String == "" {
l.Name.Valid = false
} else {
l.Name.Valid = true
}
if l.PosterS.String == "" {
l.PosterS.Valid = false
} else {
l.PosterS.Valid = true
}
if l.PosterB.String == "" {
l.PosterB.Valid = false
} else {
l.PosterB.Valid = true
}
if l.ThumbS.String == "" {
l.ThumbS.Valid = false
} else {
l.ThumbS.Valid = true
}
if l.ThumbB.String == "" {
l.ThumbB.Valid = false
} else {
l.ThumbB.Valid = true
}
if l.Youtube.String == "" {
l.Youtube.Valid = false
} else {
l.Youtube.Valid = true
}
if l.Descr.String == "" {
l.Descr.Valid = false
} else {
l.Descr.Valid = true
}
l.Votes.Valid = true
if l.Votes.Int64 == 0 {
l.Rating.Valid = false
} else {
l.Rating.Valid = true
}
if l.Duration.Int64 == 0 {
l.Duration.Valid = false
} else {
l.Duration.Valid = true
}
return
}
func (e *Episode) BeforeSave() (err error) {
if e.Slug.String == "" {
e.Slug.Valid = false
} else {
e.Slug.Valid = true
}
if e.Name.String == "" {
e.Name.Valid = false
} else {
e.Name.Valid = true
}
if e.ThumbS.String == "" {
e.ThumbS.Valid = false
} else {
e.ThumbS.Valid = true
}
if e.ThumbB.String == "" {
e.ThumbB.Valid = false
} else {
e.ThumbB.Valid = true
}
if e.Youtube.String == "" {
e.Youtube.Valid = false
} else {
e.Youtube.Valid = true
}
if e.Descr.String == "" {
e.Descr.Valid = false
} else {
e.Descr.Valid = true
}
e.Votes.Valid = true
e.Episode.Valid = true
if e.Votes.Int64 == 0 {
e.Rating.Valid = false
} else {
e.Rating.Valid = true
}
if e.Duration.Int64 == 0 {
e.Duration.Valid = false
} else {
e.Duration.Valid = true
}
return
}